diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..b2cd462e --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +.vs/ +bin +obj +packages \ No newline at end of file diff --git a/BDD/BerlinClockFeatureSteps.feature b/BDD/BerlinClockFeatureSteps.feature index 67669587..4b81d50f 100644 --- a/BDD/BerlinClockFeatureSteps.feature +++ b/BDD/BerlinClockFeatureSteps.feature @@ -48,4 +48,4 @@ RRRR RRRR OOOOOOOOOOO OOOO -""" +""" \ No newline at end of file diff --git a/BerlinClock.csproj b/BerlinClock.csproj index ac8af99d..ae2ba03c 100644 --- a/BerlinClock.csproj +++ b/BerlinClock.csproj @@ -50,6 +50,9 @@ + + + True diff --git a/Classes/Constants.cs b/Classes/Constants.cs new file mode 100644 index 00000000..75ff0f30 --- /dev/null +++ b/Classes/Constants.cs @@ -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'; + } +} diff --git a/Classes/ITimeConverter.cs b/Classes/ITimeConverter.cs index 1d9e4c27..e3e45785 100644 --- a/Classes/ITimeConverter.cs +++ b/Classes/ITimeConverter.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace BerlinClock { diff --git a/Classes/TimeConverter.cs b/Classes/TimeConverter.cs index dd5bf4e0..595f6ff5 100644 --- a/Classes/TimeConverter.cs +++ b/Classes/TimeConverter.cs @@ -1,7 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; +using BerlinClock.Classes; +using BerlinClock.Utils; namespace BerlinClock { @@ -9,7 +10,72 @@ public class TimeConverter : ITimeConverter { public string convertTime(string aTime) { - throw new NotImplementedException(); + var result = new List(); + + 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); } } } diff --git a/Utils/StringUtils.cs b/Utils/StringUtils.cs new file mode 100644 index 00000000..442e491b --- /dev/null +++ b/Utils/StringUtils.cs @@ -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); + } + } +} diff --git a/Utils/TimeUtils.cs b/Utils/TimeUtils.cs new file mode 100644 index 00000000..05d541b3 --- /dev/null +++ b/Utils/TimeUtils.cs @@ -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")); + } + } +}