From 7037e0c78ad46477515a0630c187f192f95c98b0 Mon Sep 17 00:00:00 2001 From: brus77 Date: Sat, 28 Dec 2019 13:53:34 +0100 Subject: [PATCH 1/2] Berlin clock exercise --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..e9150297 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +################################################################################ +# This .gitignore file was automatically created by Microsoft(R) Visual Studio. +################################################################################ + +/.vs From 938813e6ac216c61210c843380a1bf1813537abd Mon Sep 17 00:00:00 2001 From: brus77 Date: Sat, 28 Dec 2019 13:54:39 +0100 Subject: [PATCH 2/2] TimeConverter class implementation --- .gitignore | 4 ++ Classes/TimeConverter.cs | 134 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 135 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index e9150297..962b63d4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,7 @@ ################################################################################ /.vs +/obj/Debug/BerlinClock.csprojAssemblyReference.cache +/bin/Debug +/obj/Debug +/packages diff --git a/Classes/TimeConverter.cs b/Classes/TimeConverter.cs index dd5bf4e0..3fdc5726 100644 --- a/Classes/TimeConverter.cs +++ b/Classes/TimeConverter.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Text; namespace BerlinClock @@ -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(); } } }