Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added implementation for ConvertTime and all related classes #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/.vs/BerlinClock/v16
/.vs/VSProjects/v16
/bin/Debug
/.vs
/obj/Debug
/packages/SpecFlow.1.9.0
4 changes: 4 additions & 0 deletions BerlinClock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BDD\BerlinClockFeatureSteps.cs" />
<Compile Include="Classes\Clock.cs" />
<Compile Include="Classes\ClockConfig.cs" />
<Compile Include="Classes\ClockRow.cs" />
<Compile Include="Classes\DateTimeUtilities.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="BDD\BerlinClockFeatureSteps.feature.cs">
<AutoGen>True</AutoGen>
Expand Down
75 changes: 75 additions & 0 deletions Classes/Clock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BerlinClock
{
/// <summary>
/// clock model
/// </summary>
public class Clock
{
/// <summary>
/// time that should be converted
/// </summary>
private readonly TimeSpan _time;
private readonly ClockConfig _clockConfig;

/// <summary>
/// clock state
/// </summary>
private List<ClockRow> Rows => new List<ClockRow>
{
new ClockRow
{
ComputeLampChar = x => ClockConfig.YellowLamp,
Length = _clockConfig.BlinkLamps,
LampsOn = _time.Seconds % _clockConfig.NumberOfBlinksStates == 0 ? 1 : 0
},
new ClockRow
{
ComputeLampChar = x => ClockConfig.RedLamp,
Length = _clockConfig.HourLampsPerRow,
LampsOn = (int)(_time.TotalHours - _time.TotalHours % _clockConfig.HoursPerLampRow1) / _clockConfig.HoursPerLampRow1
},
new ClockRow
{
ComputeLampChar = x => ClockConfig.RedLamp,
Length = _clockConfig.HourLampsPerRow,
LampsOn = (int)_time.TotalHours % _clockConfig.HoursPerLampRow1
},
new ClockRow
{
ComputeLampChar = x => (x + 1) % 3 == 0 ? ClockConfig.RedLamp : ClockConfig.YellowLamp,
Length = _clockConfig.MinuteLampsRow1,
LampsOn = (_time.Minutes - _time.Minutes % _clockConfig.MinutesPerLampRow1) / _clockConfig.MinutesPerLampRow1
},
new ClockRow
{
ComputeLampChar = x => ClockConfig.YellowLamp,
Length = _clockConfig.MinuteLampsRow2,
LampsOn = _time.Minutes % _clockConfig.MinutesPerLampRow1
}
};

/// <summary>
/// clock model
/// </summary>
/// <param name="time">time that should be converted</param>
public Clock(TimeSpan time, ClockConfig config)
{
_time = time;
_clockConfig = config;
}

public Clock(string serializedTime, ClockConfig config)
: this(serializedTime.ParseTimeString(), config)
{

}

public override string ToString() => string.Join(Environment.NewLine, Rows.Select(x => x.ToString()));
}
}
52 changes: 52 additions & 0 deletions Classes/ClockConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BerlinClock
{
public class ClockConfig
{
#region characters to represent lamps

public const char RedLamp = 'R';
public const char YellowLamp = 'Y';

#endregion

#region number of lamps in clock rows

public int BlinkLamps { get; private set; }
public int HourLampsPerRow { get; private set; }
public int MinuteLampsRow1 { get; private set; }
public int MinuteLampsRow2 { get; private set; }

#endregion

#region number of lamps in clock rows

public int NumberOfBlinksStates { get; private set; }
public int HoursPerLampRow1 { get; private set; }
public int MinutesPerLampRow1 { get; private set; }

#endregion

public ClockConfig(int blinkLamps,
int hourLampsPerRow,
int minuteLampsRow1,
int minuteLampsRow2,
int numberOfBlinkStates,
int hoursPerLampRow1,
int minutesPerLampRow1)
{
BlinkLamps = blinkLamps;
HourLampsPerRow = hourLampsPerRow;
MinuteLampsRow1 = minuteLampsRow1;
MinuteLampsRow2 = minuteLampsRow2;
NumberOfBlinksStates = numberOfBlinkStates;
HoursPerLampRow1 = hoursPerLampRow1;
MinutesPerLampRow1 = minutesPerLampRow1;
}
}
}
41 changes: 41 additions & 0 deletions Classes/ClockRow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Linq;

namespace BerlinClock
{
/// <summary>
/// clock row model
/// </summary>
public class ClockRow
{
/// <summary>
/// filler for empty position in clock row
/// </summary>
const char EmptyValChar = 'O';

/// <summary>
/// lambda to compute appropriate lamp char
/// </summary>
public Func<int, char> ComputeLampChar { get; set; }

/// <summary>
/// defines whole length of clock row
/// </summary>
public int Length { private get; set; }

/// <summary>
/// defines how many lamps should be highlighted
/// </summary>
public int LampsOn { private get; set; }

public override string ToString() =>
string
.Join(
string.Empty,
new string(
Enumerable.Range(0, LampsOn).Select(ComputeLampChar).ToArray()
)
)
.PadRight(Length, EmptyValChar);
}
}
49 changes: 49 additions & 0 deletions Classes/DateTimeUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Globalization;
using System.Text.RegularExpressions;

namespace BerlinClock
{
/// <summary>
/// helper that convert time string time stamp
/// </summary>
public static class DateTimeUtilities
{
/// <summary>
/// expected time string format
/// </summary>
private const string TimeFormat = "HH:mm:ss";

/// <summary>
/// template to check if string defines 24th hour
/// </summary>
private const string TimeTemplate = @"24(:[0-5][0-9]:[0-5][0-9])$";

/// <summary>
/// adjusts time string so it could be converted to timespan
/// </summary>
/// <param name="val">time string</param>
/// <returns></returns>
private static string Adjust(this string val) =>
Regex.Replace(val, TimeTemplate, "00$1");

/// <summary>
/// parses formatted time string to timestamp
/// </summary>
/// <param name="val">time string</param>
/// <returns></returns>
private static TimeSpan ParseFormattedTimeString(this string val) =>
DateTime.ParseExact(val, TimeFormat, CultureInfo.CurrentCulture).TimeOfDay;

/// <summary>
/// parses time string to timespan.
/// hours overflow is taken into consideration.
/// </summary>
/// <param name="val">time string</param>
/// <returns></returns>
public static TimeSpan ParseTimeString(this string val) =>
Regex.IsMatch(val, TimeTemplate)
? val.Adjust().ParseFormattedTimeString().Add(TimeSpan.FromDays(1))
: val.ParseFormattedTimeString();
}
}
20 changes: 13 additions & 7 deletions Classes/TimeConverter.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BerlinClock
namespace BerlinClock
{
public class TimeConverter : ITimeConverter
{
private ClockConfig _config = new ClockConfig(
blinkLamps: 1,
hourLampsPerRow: 4,
minuteLampsRow1: 11,
minuteLampsRow2: 4,
numberOfBlinkStates: 2,
hoursPerLampRow1: 5,
minutesPerLampRow1: 5);

public string convertTime(string aTime)
{
throw new NotImplementedException();
var clock = new Clock(aTime, _config);

return clock.ToString();
}
}
}