Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions BerlinClock.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="packages\NUnit3TestAdapter.3.16.1\build\net35\NUnit3TestAdapter.props" Condition="Exists('packages\NUnit3TestAdapter.3.16.1\build\net35\NUnit3TestAdapter.props')" />
<Import Project="packages\NUnit.3.12.0\build\NUnit.props" Condition="Exists('packages\NUnit.3.12.0\build\NUnit.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -11,6 +13,8 @@
<AssemblyName>BerlinClock</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -36,6 +40,9 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="nunit.framework, Version=3.12.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>packages\NUnit.3.12.0\lib\net45\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -50,6 +57,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BDD\BerlinClockFeatureSteps.cs" />
<Compile Include="Classes\BerlinClock.cs" />
<Compile Include="Classes\IClock.cs" />
<Compile Include="Classes\ITimeParser.cs" />
<Compile Include="Classes\Time.cs" />
<Compile Include="Classes\TimeParser.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="BDD\BerlinClockFeatureSteps.feature.cs">
<AutoGen>True</AutoGen>
Expand All @@ -58,6 +70,7 @@
</Compile>
<Compile Include="Classes\ITimeConverter.cs" />
<Compile Include="Classes\TimeConverter.cs" />
<Compile Include="UnitTests\TimeParserTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand All @@ -71,6 +84,13 @@
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\NUnit.3.12.0\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\NUnit.3.12.0\build\NUnit.props'))" />
<Error Condition="!Exists('packages\NUnit3TestAdapter.3.16.1\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\NUnit3TestAdapter.3.16.1\build\net35\NUnit3TestAdapter.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
84 changes: 84 additions & 0 deletions Classes/BerlinClock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BerlinClock.Classes
{
public class BerlinClock : IClock
{
ITimeParser parser;
public BerlinClock(ITimeParser parser)
{
this.parser = parser;
}

public string ConvertTime(string aTime)
{
var time = this.parser.ParseTime(aTime);

var sb = new StringBuilder();
sb.AppendLine(GetYellowLampString(time));
sb.AppendLine(GetHourRow1String(time));
sb.AppendLine(GetHourRow2String(time));
sb.AppendLine(GetMinuteRow1String(time));
sb.Append(GetMinuteRow2String(time));

return sb.ToString();
}

private string GetYellowLampString(Time time)
{
return $"{(time.Seconds % 2 == 0 ? "Y" : "O")}";
}

private string GetHourRow1String(Time time)
{
var result = "OOOO".ToArray();

for (int i = 0; i < time.Hours / 5; i++)
{
result[i] = 'R';
}

return new string(result);
}

private string GetHourRow2String(Time time)
{
var result = "OOOO".ToArray();

for (int i = 0; i < time.Hours % 5; i++)
{
result[i] = 'R';
}

return new string(result);
}

private string GetMinuteRow1String(Time time)
{
var result = "OOOOOOOOOOO".ToArray();
int[] minuteQuarters = new int[] { 2, 5, 8 };
for (int i = 0; i < time.Minutes / 5; i++)
{
result[i] = minuteQuarters.Contains(i) ? 'R' : 'Y';
}

return new string(result);
}

private string GetMinuteRow2String(Time time)
{
var result = "OOOO".ToArray();

for (int i = 0; i < time.Minutes % 5; i++)
{
result[i] = 'Y';
}

return new string(result);
}
}
}
13 changes: 13 additions & 0 deletions Classes/IClock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BerlinClock.Classes
{
public interface IClock
{
string ConvertTime(string time);
}
}
13 changes: 13 additions & 0 deletions Classes/ITimeParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BerlinClock.Classes
{
public interface ITimeParser
{
Time ParseTime(string aTime);
}
}
15 changes: 15 additions & 0 deletions Classes/Time.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BerlinClock.Classes
{
public class Time
{
public int Hours { get; set; }
public int Minutes { get; set; }
public int Seconds { get; set; }
}
}
8 changes: 6 additions & 2 deletions Classes/TimeConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using BerlinClock.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -7,9 +8,12 @@ namespace BerlinClock
{
public class TimeConverter : ITimeConverter
{
IClock clock;
public string convertTime(string aTime)
{
throw new NotImplementedException();
var timeParser = new TimeParser();
clock = new Classes.BerlinClock(timeParser);
return clock.ConvertTime(aTime);
}
}
}
37 changes: 37 additions & 0 deletions Classes/TimeParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BerlinClock.Classes
{
public class TimeParser : ITimeParser
{
public Time ParseTime(string aTime)
{
var timeParts = aTime.Split(':');
if (timeParts.Length != 3)
{
throw new ArgumentException("Incorrect time format");
}
int hours;
if (!int.TryParse(timeParts[0], out hours) || hours < 0 || hours > 24)
{
throw new ArgumentException("Incorrect time format");
}
int minutes;
if (!int.TryParse(timeParts[1], out minutes) || minutes < 0 || minutes > 59)
{
throw new ArgumentException("Incorrect time format");
}
int seconds;
if (!int.TryParse(timeParts[2], out seconds) || seconds < 0 || seconds > 59)
{
throw new ArgumentException("Incorrect time format");
}

return new Time { Hours = hours, Minutes = minutes, Seconds = seconds };
}
}
}
81 changes: 81 additions & 0 deletions UnitTests/TimeParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using BerlinClock.Classes;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BerlinClock.UnitTests
{
[TestFixture]
public class TimeParserTests
{
private ITimeParser parser;
[SetUp]
public void SetUp()
{
parser = new TimeParser();
}

[Test]
[TestCase("22:59")]
[TestCase("120000")]
[TestCase("12.00.00")]
public void InValidFormat(string aTime)
{
Assert.Throws<ArgumentException>(() => parser.ParseTime(aTime));
}

[Test]
[TestCase("23:59:59", 23)]
[TestCase("24:00:00", 24)]
[TestCase("00:00:00", 00)]
public void HoursValidConversion(string aTime, int expectedValue)
{
Assert.AreEqual(parser.ParseTime(aTime).Hours, expectedValue);
}

[Test]
[TestCase("25:00:00")]
[TestCase("-1:00:00")]
public void HoursInValidConversion(string aTime)
{
Assert.Throws<ArgumentException>(() => parser.ParseTime(aTime));
}

[Test]
[TestCase("23:59:59", 59)]
[TestCase("24:00:00", 0)]
[TestCase("00:30:00", 30)]
public void MinutesValidConversion(string aTime, int expectedValue)
{
Assert.AreEqual(parser.ParseTime(aTime).Minutes, expectedValue);
}

[Test]
[TestCase("12:60:00")]
[TestCase("00:-1:00")]
public void MinutesInValidConversion(string aTime)
{
Assert.Throws<ArgumentException>(() => parser.ParseTime(aTime));
}

[Test]
[TestCase("23:59:59", 59)]
[TestCase("24:00:00", 0)]
[TestCase("00:00:30", 30)]
public void SecondsValidConversion(string aTime, int expectedValue)
{
Assert.AreEqual(parser.ParseTime(aTime).Seconds, expectedValue);
}

[Test]
[TestCase("12:30:60")]
[TestCase("00:00:-1")]
public void SecondsInValidConversion(string aTime)
{
Assert.Throws<ArgumentException>(() => parser.ParseTime(aTime));
}
}
}
2 changes: 2 additions & 0 deletions packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="3.12.0" targetFramework="net45" />
<package id="NUnit3TestAdapter" version="3.16.1" targetFramework="net45" developmentDependency="true" />
<package id="SpecFlow" version="1.9.0" targetFramework="net45" />
</packages>
Binary file added packages/NUnit.3.12.0/.signature.p7s
Binary file not shown.
Loading