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
5 changes: 3 additions & 2 deletions BDD/BerlinClockFeatureSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using TechTalk.SpecFlow;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using BerlinClock.Classes.BerlinClock.BerlinClockProviders;

namespace BerlinClock
{
[Binding]
public class TheBerlinClockSteps
{
private ITimeConverter berlinClock = new TimeConverter();
private ITimeConverter berlinClock = new TimeConverter(new TimeStringValidator(), new BerlinClockConverter(new TimeBlockProviderFactory()));
private String theTime;


Expand All @@ -21,7 +22,7 @@ public void WhenTheTimeIs(string time)
[Then(@"the clock should look like")]
public void ThenTheClockShouldLookLike(string theExpectedBerlinClockOutput)
{
Assert.AreEqual(berlinClock.convertTime(theTime), theExpectedBerlinClockOutput);
Assert.AreEqual(berlinClock.ConvertTime(theTime), theExpectedBerlinClockOutput);
}

}
Expand Down
14 changes: 14 additions & 0 deletions BerlinClock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BDD\BerlinClockFeatureSteps.cs" />
<Compile Include="Classes\BerlinClock\BerlinClock.cs" />
<Compile Include="Classes\BerlinClock\BerlinClockProviders\BerlinClockHoursProvider.cs" />
<Compile Include="Classes\BerlinClock\BerlinClockProviders\BerlinClockMinutesProvider.cs" />
<Compile Include="Classes\BerlinClock\BerlinClockProviders\BerlinClockSecondsProvider.cs" />
<Compile Include="Classes\BerlinClock\BerlinClockProviders\ITimeBlockProvider.cs" />
<Compile Include="Classes\BerlinClock\BerlinClockProviders\ITimeBlockProviderFactory.cs" />
<Compile Include="Classes\BerlinClock\BerlinClockProviders\TimeBlockProviderFactory.cs" />
<Compile Include="Classes\BerlinClock\Constants.cs" />
<Compile Include="Classes\BerlinClock\IBerlinClock.cs" />
<Compile Include="Classes\ITimeStringValidator.cs" />
<Compile Include="Classes\Responces\ValidationTimeResponce.cs" />
<Compile Include="Classes\Time.cs" />
<Compile Include="Classes\TimeStringValidator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="BDD\BerlinClockFeatureSteps.feature.cs">
<AutoGen>True</AutoGen>
Expand All @@ -70,6 +83,7 @@
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- 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.
Expand Down
30 changes: 30 additions & 0 deletions Classes/BerlinClock/BerlinClock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using BerlinClock.Classes.BerlinClock.BerlinClockProviders;

namespace BerlinClock
{
public class BerlinClockConverter : IBerlinClock
{
private ITimeBlockProvider _secondsTimeBlockProviders;
private ITimeBlockProvider _minutesTimeBlockProviders;
private ITimeBlockProvider _hoursTimeBlockProviders;
private ITimeBlockProviderFactory _timeBlockProviderFactory;

public BerlinClockConverter(ITimeBlockProviderFactory timeBlockProviderFactory)
{
_timeBlockProviderFactory = timeBlockProviderFactory;

_timeBlockProviderFactory = timeBlockProviderFactory;
_secondsTimeBlockProviders = _timeBlockProviderFactory.CreateSecondsTimeBlockProvider();
_minutesTimeBlockProviders = _timeBlockProviderFactory.CreateMinutesTimeBlockProvider();
_hoursTimeBlockProviders = _timeBlockProviderFactory.CreateHoursTimeBlockProvider();
}
public string ConvertTime(Time time)
{
int hours = time.Hours;
int minutes = time.Minutes;
int seconds = time.Seconds;

return string.Format("{0}\r\n{1}\r\n{2}", _secondsTimeBlockProviders.GetTimeBlock(seconds), _hoursTimeBlockProviders.GetTimeBlock(hours), _minutesTimeBlockProviders.GetTimeBlock(minutes));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BerlinClock.Classes.BerlinClock.BerlinClockProviders
{
public class BerlinClockHoursProvider : ITimeBlockProvider
{
public string GetTimeBlock(int hours)
{
int topHours = hours / Constants.HOURS_INTERVAL;
int buttonHours = hours % Constants.HOURS_INTERVAL;

String topRow = GetHoursRow(Constants.TOP_HOURS_BLOCK_LENGTH, topHours);
String bottomRow = GetHoursRow(Constants.BOTTOM_HOURS_BLOCK_LENGTH, buttonHours);

return String.Format("{0}\r\n{1}", topRow, bottomRow);
}

/// <summary>
/// Returns the light for hour row.
/// </summary>
/// <param name="blockLength"></param>
/// <param name="activeLights"></param>
/// <returns></returns>

private string GetHoursRow(int blockLength, int activeLights)
{
StringBuilder row = new StringBuilder();

for (var i = 0; i < blockLength; i++)
{
row.Append(i < activeLights ? Constants.RED_LIGHT : Constants.EMPTY_LIGHT);
}

return row.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Text;

namespace BerlinClock.Classes.BerlinClock.BerlinClockProviders
{
public class BerlinClockMinutesProvider : ITimeBlockProvider
{
/// <summary>
/// Returns the light for minutes
/// </summary>
/// <param name="minutes"></param>
/// <returns></returns>
public string GetTimeBlock(int minutes)
{
int topMinutes = minutes / Constants.MINUTES_INTERVAL;
int buttonMinutes = minutes % Constants.MINUTES_INTERVAL;

var topRow = GetMinutesRow(Constants.TOP_MINUTES_BLOCK_LENGTH, topMinutes, i => (i + 1) % 3 == 0);
var bottomRow = GetMinutesRow(Constants.BOTTOM_MINUTES_BLOCK_LENGTH, buttonMinutes,
i => false);

return String.Format("{0}\r\n{1}", topRow, bottomRow);
}

/// <summary>
/// Returns the light for minute row.
/// </summary>
/// <param name="blockLength"></param>
/// <param name="activeLights"></param>
/// <param name="topRowDefiner">Defines different func for getting red lamp and indicate the first quarter, half and last quarter of an hour. If the minutes line is not related to quarter and should return yellow light - pass i=> true</param>
/// <returns></returns>
private string GetMinutesRow(int blockLength, int activeLights, Func<int, bool> topRowDefiner)
{
StringBuilder row = new StringBuilder();

for (var i = 0; i < blockLength; i++)
{
row.Append(i < activeLights ? (topRowDefiner(i) ? Constants.RED_LIGHT : Constants.YELLOW_LIGTH) : Constants.EMPTY_LIGHT);
}

return row.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace BerlinClock.Classes.BerlinClock.BerlinClockProviders
{
public class BerlinClockSecondsProvider : ITimeBlockProvider
{
/// <summary>
/// Returns the light for seconds.
/// </summary>
/// <param name="sec"></param>
/// <returns></returns>
public string GetTimeBlock(int sec)
{
return sec % Constants.SECONDS_INTERVAL == 0 ? Constants.YELLOW_LIGTH : Constants.EMPTY_LIGHT;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BerlinClock.Classes.BerlinClock.BerlinClockProviders
{
public interface ITimeBlockProvider
{
string GetTimeBlock(int timeAmount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace BerlinClock.Classes.BerlinClock.BerlinClockProviders
{
public interface ITimeBlockProviderFactory
{
ITimeBlockProvider CreateSecondsTimeBlockProvider();
ITimeBlockProvider CreateMinutesTimeBlockProvider();
ITimeBlockProvider CreateHoursTimeBlockProvider();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace BerlinClock.Classes.BerlinClock.BerlinClockProviders
{
public class TimeBlockProviderFactory : ITimeBlockProviderFactory
{
public ITimeBlockProvider CreateSecondsTimeBlockProvider()
{
return new BerlinClockSecondsProvider();
}

public ITimeBlockProvider CreateMinutesTimeBlockProvider()
{
return new BerlinClockMinutesProvider();
}

public ITimeBlockProvider CreateHoursTimeBlockProvider()
{
return new BerlinClockHoursProvider();
}
}
}
19 changes: 19 additions & 0 deletions Classes/BerlinClock/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace BerlinClock
{
public static class Constants
{
public const string RED_LIGHT = "R";
public const string YELLOW_LIGTH = "Y";
public const string EMPTY_LIGHT = "O";

public const int TOP_HOURS_BLOCK_LENGTH = 4;
public const int BOTTOM_HOURS_BLOCK_LENGTH = 4;

public const int TOP_MINUTES_BLOCK_LENGTH = 11;
public const int BOTTOM_MINUTES_BLOCK_LENGTH = 4;

public const int SECONDS_INTERVAL = 2;
public const int MINUTES_INTERVAL = 5;
public const int HOURS_INTERVAL = 5;
}
}
2 changes: 1 addition & 1 deletion Classes/ITimeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace BerlinClock
{
public interface ITimeConverter
{
String convertTime(String aTime);
String ConvertTime(String aTime);
}
}
29 changes: 25 additions & 4 deletions Classes/TimeConverter.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using BerlinClock.Classes.BerlinClock.BerlinClockProviders;

namespace BerlinClock
{
public class TimeConverter : ITimeConverter
{
public string convertTime(string aTime)
private ITimeStringValidator _timeValidator;
private IBerlinClock _clock;

public TimeConverter(ITimeStringValidator timeValidator, IBerlinClock clock)
{
_timeValidator = timeValidator;
_clock = clock;

}

/// <summary>
/// Converts Time from original format HH:mm:ss to BerlinClock.
/// </summary>
/// <param name="aTime"></param>
/// <returns></returns>
public string ConvertTime(string aTime)
{
throw new NotImplementedException();
var validation = _timeValidator.ValidateTimeString(aTime);
if (!validation.Valid)
{
throw new ArgumentException("Invalid time format. Time format must be HH:mm:ss");
}

return _clock.ConvertTime(validation.Time);
}
}
}