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
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/.vs
/obj/Debug/BerlinClock.csprojAssemblyReference.cache
/bin/Debug
/obj/Debug
/packages
134 changes: 131 additions & 3 deletions Classes/TimeConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BerlinClock
Expand All @@ -9,7 +7,137 @@ public class TimeConverter : ITimeConverter
{
public string convertTime(string aTime)
{
throw new NotImplementedException();
TimeSpan time = ParseTimeString(aTime);
string clockLampsString = BuildBerlinClockString(time);
return clockLampsString;
}

private TimeSpan ParseTimeString(string timeString)
{
var invariantCultureInfo = System.Globalization.CultureInfo.InvariantCulture;

if (string.IsNullOrWhiteSpace(timeString))
{
throw new ArgumentException("Time string parameter is null or empty", "timeString");
}

string[] timeParts = timeString.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
if (timeParts.Length != 3)
{
throw new ArgumentException("Time string format is not valid", "timeString");
}

int hours, minutes, seconds;
if (!int.TryParse(timeParts[0], System.Globalization.NumberStyles.Integer, invariantCultureInfo.NumberFormat, out hours)
|| !int.TryParse(timeParts[1], System.Globalization.NumberStyles.Integer, invariantCultureInfo.NumberFormat, out minutes)
|| !int.TryParse(timeParts[2], System.Globalization.NumberStyles.Integer, invariantCultureInfo.NumberFormat, out seconds)
)
{
throw new ArgumentException("Time string format is not valid", "timeString");
}

if (hours < 0 || hours > 24)
{
throw new ArgumentOutOfRangeException("Hours");
}
if (minutes < 0 || hours > 59)
{
throw new ArgumentOutOfRangeException("Minutes");
}
if (seconds < 0 || seconds > 59)
{
throw new ArgumentOutOfRangeException("Seconds");
}

return new TimeSpan(hours, minutes, seconds);
}

private string BuildBerlinClockString(TimeSpan time)
{
int hours = (int)time.TotalHours;
int minutes = time.Minutes;
int seconds = time.Seconds;

StringBuilder result = new StringBuilder();
result.AppendFormat("{0}\r\n", TwoSecondsLamp(seconds));
result.AppendFormat("{0}\r\n", FiveHoursLamps(hours));
result.AppendFormat("{0}\r\n", OneHourLamps(hours));
result.AppendFormat("{0}\r\n", FiveMinutesLamps(minutes));
result.AppendFormat("{0}", OneMinuteLamps(minutes));
return result.ToString();
}

private string TwoSecondsLamp(int seconds)
{
return seconds % 2 == 0 ? "Y" : "O";
}

private string FiveHoursLamps(int hours)
{
StringBuilder result = new StringBuilder();

int lampsOn = hours / 5;
int lampsOff = 4 - lampsOn;
for (int l = 0; l < lampsOn; l++)
{
result.Append("R");
}
for (int l = 0; l < lampsOff; l++)
{
result.Append("O");
}
return result.ToString();
}

private string OneHourLamps(int hours)
{
StringBuilder result = new StringBuilder();

int lampsOn = hours % 5;
int lampsOff = 4 - lampsOn;
for (int l = 0; l < lampsOn; l++)
{
result.Append("R");
}
for (int l = 0; l < lampsOff; l++)
{
result.Append("O");
}
return result.ToString();
}

private string FiveMinutesLamps(int minutes)
{
StringBuilder result = new StringBuilder();

int lampsOn = minutes / 5;
int lampsOff = 11 - lampsOn;
for (int l = 1; l <= lampsOn; l++)
{
result.Append(l % 3 == 0 ? "R" : "Y");
}
for (int l = 1; l <= lampsOff; l++)
{
result.Append("O");
}
return result.ToString();
}

private string OneMinuteLamps(int minutes)
{
StringBuilder result = new StringBuilder();

int lampsOn = minutes % 5;
int lampsOff = 4 - lampsOn;
for (int l = 1; l <= lampsOn; l++)
{
result.Append("Y");
}
for (int l = 1; l <= lampsOff; l++)
{
result.Append("O");
}
return result.ToString();
}
}
}