From 149c28bfe22d5cdca00879121af931c0a897d9a2 Mon Sep 17 00:00:00 2001 From: oevtuh Date: Sun, 23 Feb 2020 11:14:57 +0200 Subject: [PATCH 1/2] Implemented ConvertTime for TimeConverter. --- BDD/BerlinClockFeatureSteps.cs | 2 +- Classes/ITimeConverter.cs | 2 +- Classes/TimeConverter.cs | 126 +++++++++++++++++++++++++++++++-- 3 files changed, 124 insertions(+), 6 deletions(-) diff --git a/BDD/BerlinClockFeatureSteps.cs b/BDD/BerlinClockFeatureSteps.cs index 4390f3cb..045a33cd 100644 --- a/BDD/BerlinClockFeatureSteps.cs +++ b/BDD/BerlinClockFeatureSteps.cs @@ -21,7 +21,7 @@ public void WhenTheTimeIs(string time) [Then(@"the clock should look like")] public void ThenTheClockShouldLookLike(string theExpectedBerlinClockOutput) { - Assert.AreEqual(berlinClock.convertTime(theTime), theExpectedBerlinClockOutput); + Assert.AreEqual(berlinClock.ConvertTime(theTime), theExpectedBerlinClockOutput); } } diff --git a/Classes/ITimeConverter.cs b/Classes/ITimeConverter.cs index 1d9e4c27..58eadd55 100644 --- a/Classes/ITimeConverter.cs +++ b/Classes/ITimeConverter.cs @@ -7,6 +7,6 @@ namespace BerlinClock { public interface ITimeConverter { - String convertTime(String aTime); + String ConvertTime(String aTime); } } diff --git a/Classes/TimeConverter.cs b/Classes/TimeConverter.cs index dd5bf4e0..d3ca6330 100644 --- a/Classes/TimeConverter.cs +++ b/Classes/TimeConverter.cs @@ -1,15 +1,133 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Text; +using System.Text.RegularExpressions; namespace BerlinClock { public class TimeConverter : ITimeConverter { - public string convertTime(string aTime) + private const string RED_LIGHT = "R"; + private const string YELLOW_LIGTH = "Y"; + private const string EMPTY_LIGHT = "O"; + + private const int TOP_HOURS_BLOCK_LENGTH = 4; + private const int BOTTOM_HOURS_BLOCK_LENGTH = 4; + + private const int TOP_MINUTES_BLOCK_LENGTH = 11; + private const int BOTTOM_MINUTES_BLOCK_LENGTH = 4; + + private const int SECONDS_INTERVAL = 2; + private const int MINUTES_INTERVAL = 5; + private const int HOURS_INTERVAL = 5; + + /// + /// Converts Time from original format HH:mm:ss to BerlinClock. + /// + /// + /// + public string ConvertTime(string aTime) + { + if (!IsTimeFormatValid(aTime)) + { + throw new AggregateException("Invalid time format.Time format must be HH:mm:ss"); + } + int hours = int.Parse(aTime.Split(':')[0]); + int minutes = int.Parse(aTime.Split(':')[1]); + int seconds = int.Parse(aTime.Split(':')[2]); + + return string.Format("{0}\r\n{1}\r\n{2}", GetSeconds(seconds), GetHours(hours), GetMinutes(minutes)); + } + + /// + /// Returns the light for seconds. + /// + /// + /// + private string GetSeconds(int sec) + { + return sec % SECONDS_INTERVAL == 0 ? YELLOW_LIGTH : EMPTY_LIGHT; + } + + /// + /// Returns the light for minutes + /// + /// + /// + private string GetMinutes(int minutes) + { + int topMinutes = minutes / MINUTES_INTERVAL; + int buttonMinutes = minutes % MINUTES_INTERVAL; + + var topRow = GetMinutesRow(TOP_MINUTES_BLOCK_LENGTH, topMinutes, i=> (i + 1) % 3 == 0); + var bottomRow = GetMinutesRow(BOTTOM_MINUTES_BLOCK_LENGTH, buttonMinutes, + i => false); + + return String.Format("{0}\r\n{1}", topRow, bottomRow); + } + + /// + /// Returns the light for minute row. + /// + /// + /// + /// Defines different func for getting red lamp and indicate the first quarter, half and last quarter of an hour. If the minutes line is not related to quarter and should return yellow light - pass i=> true + /// + private string GetMinutesRow(int blockLength, int activeLights, Func topRowDefiner) { - throw new NotImplementedException(); + StringBuilder row = new StringBuilder(); + + for (var i = 0; i < blockLength; i++) + { + row.Append(i < activeLights ? (topRowDefiner(i) ? RED_LIGHT : YELLOW_LIGTH) : EMPTY_LIGHT); + } + + return row.ToString(); + } + /// + /// Returns the light for hours. + /// + /// + /// + private string GetHours(int hours) + { + int topHours = hours / HOURS_INTERVAL; + int buttonHours = hours % HOURS_INTERVAL; + + String topRow = GetHoursRow(TOP_HOURS_BLOCK_LENGTH, topHours); + String bottomRow = GetHoursRow(BOTTOM_HOURS_BLOCK_LENGTH, buttonHours); + + return String.Format("{0}\r\n{1}", topRow, bottomRow); + } + + /// + /// Returns the light for hour row. + /// + /// + /// + /// + + private string GetHoursRow(int blockLength, int activeLights) + { + StringBuilder row = new StringBuilder(); + + for (var i = 0; i < blockLength; i++) + { + row.Append(i < activeLights ? RED_LIGHT : EMPTY_LIGHT); + } + + return row.ToString(); + } + + /// + /// Validates input time string. Format should be HH:mm:ss. Because .Net DateTime doesn't support 24:00:00 - the additional check added. + /// + /// + /// + private bool IsTimeFormatValid(string input) + { + var match = Regex.Match(input, @"(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)", RegexOptions.IgnoreCase); + + return match.Success || input == "24:00:00"; } } } From e0ae93973c43bd677eef379c8413aec15043700f Mon Sep 17 00:00:00 2001 From: oevtuh Date: Tue, 25 Feb 2020 14:16:27 +0200 Subject: [PATCH 2/2] Refactored: added classes and interfaces. --- BDD/BerlinClockFeatureSteps.cs | 3 +- BerlinClock.csproj | 14 ++ Classes/BerlinClock/BerlinClock.cs | 30 +++++ .../BerlinClockHoursProvider.cs | 39 ++++++ .../BerlinClockMinutesProvider.cs | 44 +++++++ .../BerlinClockSecondsProvider.cs | 15 +++ .../ITimeBlockProvider.cs | 7 + .../ITimeBlockProviderFactory.cs | 9 ++ .../TimeBlockProviderFactory.cs | 20 +++ Classes/BerlinClock/Constants.cs | 19 +++ Classes/TimeConverter.cs | 121 ++---------------- 11 files changed, 211 insertions(+), 110 deletions(-) create mode 100644 Classes/BerlinClock/BerlinClock.cs create mode 100644 Classes/BerlinClock/BerlinClockProviders/BerlinClockHoursProvider.cs create mode 100644 Classes/BerlinClock/BerlinClockProviders/BerlinClockMinutesProvider.cs create mode 100644 Classes/BerlinClock/BerlinClockProviders/BerlinClockSecondsProvider.cs create mode 100644 Classes/BerlinClock/BerlinClockProviders/ITimeBlockProvider.cs create mode 100644 Classes/BerlinClock/BerlinClockProviders/ITimeBlockProviderFactory.cs create mode 100644 Classes/BerlinClock/BerlinClockProviders/TimeBlockProviderFactory.cs create mode 100644 Classes/BerlinClock/Constants.cs diff --git a/BDD/BerlinClockFeatureSteps.cs b/BDD/BerlinClockFeatureSteps.cs index 045a33cd..b089bef6 100644 --- a/BDD/BerlinClockFeatureSteps.cs +++ b/BDD/BerlinClockFeatureSteps.cs @@ -2,13 +2,14 @@ using TechTalk.SpecFlow; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Linq; +using BerlinClock.Classes.BerlinClock.BerlinClockProviders; namespace BerlinClock { [Binding] public class TheBerlinClockSteps { - private ITimeConverter berlinClock = new TimeConverter(); + private ITimeConverter berlinClock = new TimeConverter(new TimeStringValidator(), new BerlinClockConverter(new TimeBlockProviderFactory())); private String theTime; diff --git a/BerlinClock.csproj b/BerlinClock.csproj index ac8af99d..e874b8c5 100644 --- a/BerlinClock.csproj +++ b/BerlinClock.csproj @@ -50,6 +50,19 @@ + + + + + + + + + + + + + True @@ -70,6 +83,7 @@ +