Skip to content

Berlin clock exercise - Dev Test #58

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

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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

.vs/
bin
obj
packages
2 changes: 1 addition & 1 deletion BDD/BerlinClockFeatureSteps.feature
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ RRRR
RRRR
OOOOOOOOOOO
OOOO
"""
"""
3 changes: 3 additions & 0 deletions BerlinClock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BDD\BerlinClockFeatureSteps.cs" />
<Compile Include="Classes\Constants.cs" />
<Compile Include="Utils\StringUtils.cs" />
<Compile Include="Utils\TimeUtils.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="BDD\BerlinClockFeatureSteps.feature.cs">
<AutoGen>True</AutoGen>
Expand Down
9 changes: 9 additions & 0 deletions Classes/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace BerlinClock.Classes
{
public static class Constants
{
public const char SwitchedOffCharacter = 'O';
public const char SwitchedOnRedCharacter = 'R';
public const char SwitchedOnYellowCharacter = 'Y';
}
}
3 changes: 0 additions & 3 deletions Classes/ITimeConverter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BerlinClock
{
Expand Down
70 changes: 68 additions & 2 deletions Classes/TimeConverter.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BerlinClock.Classes;
using BerlinClock.Utils;

namespace BerlinClock
{
public class TimeConverter : ITimeConverter
{
public string convertTime(string aTime)
{
throw new NotImplementedException();
var result = new List<string>();

var timeForBerlinClock = TimeUtils.ConvertTimeStringTo24Format(aTime);

result.Add(GetLampForSeconds(timeForBerlinClock.Seconds));
result.Add(GetLampsForHours((int)timeForBerlinClock.TotalHours, 1));
result.Add(GetLampsForHours((int)timeForBerlinClock.TotalHours, 2));
result.Add(GetLampsForMinutesThirdRow(timeForBerlinClock.Minutes));
result.Add(GetLampsForMinutesFourthRow(timeForBerlinClock.Minutes));

return StringUtils.JoinStringLines(result.ToArray());
}

private string GetLampForSeconds(int seconds)
{
if ((seconds < 0) || (seconds > 59))
throw new ArgumentException("Parameter seconds must be between 0 and 59");

return seconds % 2 == 0 ? Constants.SwitchedOnYellowCharacter.ToString()
: Constants.SwitchedOffCharacter.ToString();
}

private string GetLampsForHours(int hours, int nRow)
{
if ((hours < 0) || (hours > 24))
throw new ArgumentException("Parameter hours must be between 0 and 24");

if ((nRow != 1) && (nRow != 2))
throw new ArgumentException("Parameter nRow must be 1 or 2");

// every switched on lamp count 5 hours
var lampsArray = Enumerable.Repeat(Constants.SwitchedOffCharacter, 4).ToArray();
var numberOfSwitchedOnLamps = nRow == 1 ? hours / 5 : hours % 5;
for (var nLamp = 0; nLamp < numberOfSwitchedOnLamps; nLamp++)
lampsArray[nLamp] = Constants.SwitchedOnRedCharacter;

return string.Join("", lampsArray);
}

private string GetLampsForMinutesThirdRow(int minutes)
{
if ((minutes < 0) || (minutes > 59))
throw new ArgumentException("Parameter minutes must be between 0 and 59");

// every switched on lamp count 5 minutes
var lampsArray = Enumerable.Repeat(Constants.SwitchedOffCharacter, 11).ToArray();
var numberOfSwitchedOnLamps = minutes / 5;
for (var nLamp = 0; nLamp < numberOfSwitchedOnLamps; nLamp++)
lampsArray[nLamp] = ((nLamp == 2) || (nLamp == 5) || (nLamp == 8)) ?
Constants.SwitchedOnRedCharacter : Constants.SwitchedOnYellowCharacter;

return string.Join("", lampsArray);
}

private string GetLampsForMinutesFourthRow(int minutes)
{
if ((minutes < 0) || (minutes > 59))
throw new ArgumentException("Parameter minutes must be between 0 and 59");

// every switched on lamp count 1 minute
var lampsArray = Enumerable.Repeat(Constants.SwitchedOffCharacter, 4).ToArray();
var numberOfSwitchedOnLamps = minutes % 5;
for (var nLamp = 0; nLamp < numberOfSwitchedOnLamps; nLamp++)
lampsArray[nLamp] = Constants.SwitchedOnYellowCharacter;

return string.Join("", lampsArray);
}
}
}
15 changes: 15 additions & 0 deletions Utils/StringUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace BerlinClock.Utils
{
public static class StringUtils
{
public static string JoinStringLines(string[] lines)
{
if (lines.Length <= 0)
return String.Empty;

return String.Join(Environment.NewLine, lines);
}
}
}
18 changes: 18 additions & 0 deletions Utils/TimeUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace BerlinClock.Utils
{
public static class TimeUtils
{
public static TimeSpan ConvertTimeStringTo24Format(string time)
{
if (time.Equals("24:00:00")) // an exception (There is no "24th hour" support in the DateTime class)
return new TimeSpan(24, 0, 0);

if (!DateTime.TryParse(time, out DateTime dateConverted))
throw new ArgumentException("Parameter time can not be converted, because it is not valid");

return TimeSpan.Parse(dateConverted.ToString("HH:mm:ss"));
}
}
}