Avatar of artemkorsakov

artemkorsakov's solution

to Clock in the C# Track

Published at Apr 27 2019 · 0 comments
Instructions
Test suite
Solution

Implement a clock that handles times without dates.

You should be able to add and subtract minutes to it.

Two clocks that represent the same time should be equal to each other.

Hints

This exercise requires you to implement a type-specific method for determining equality of instances. For more information, see this page.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit Clock.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

Pairing session with Erin Drummond https://twitter.com/ebdrummond

ClockTest.cs

// This file was auto-generated based on version 2.4.0 of the canonical data.

using Xunit;

public class ClockTest
{
    [Fact]
    public void On_the_hour()
    {
        var sut = new Clock(8, 0);
        Assert.Equal("08:00", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Past_the_hour()
    {
        var sut = new Clock(11, 9);
        Assert.Equal("11:09", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Midnight_is_zero_hours()
    {
        var sut = new Clock(24, 0);
        Assert.Equal("00:00", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Hour_rolls_over()
    {
        var sut = new Clock(25, 0);
        Assert.Equal("01:00", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Hour_rolls_over_continuously()
    {
        var sut = new Clock(100, 0);
        Assert.Equal("04:00", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Sixty_minutes_is_next_hour()
    {
        var sut = new Clock(1, 60);
        Assert.Equal("02:00", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Minutes_roll_over()
    {
        var sut = new Clock(0, 160);
        Assert.Equal("02:40", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Minutes_roll_over_continuously()
    {
        var sut = new Clock(0, 1723);
        Assert.Equal("04:43", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Hour_and_minutes_roll_over()
    {
        var sut = new Clock(25, 160);
        Assert.Equal("03:40", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Hour_and_minutes_roll_over_continuously()
    {
        var sut = new Clock(201, 3001);
        Assert.Equal("11:01", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Hour_and_minutes_roll_over_to_exactly_midnight()
    {
        var sut = new Clock(72, 8640);
        Assert.Equal("00:00", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Negative_hour()
    {
        var sut = new Clock(-1, 15);
        Assert.Equal("23:15", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Negative_hour_rolls_over()
    {
        var sut = new Clock(-25, 0);
        Assert.Equal("23:00", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Negative_hour_rolls_over_continuously()
    {
        var sut = new Clock(-91, 0);
        Assert.Equal("05:00", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Negative_minutes()
    {
        var sut = new Clock(1, -40);
        Assert.Equal("00:20", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Negative_minutes_roll_over()
    {
        var sut = new Clock(1, -160);
        Assert.Equal("22:20", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Negative_minutes_roll_over_continuously()
    {
        var sut = new Clock(1, -4820);
        Assert.Equal("16:40", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Negative_sixty_minutes_is_previous_hour()
    {
        var sut = new Clock(2, -60);
        Assert.Equal("01:00", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Negative_hour_and_minutes_both_roll_over()
    {
        var sut = new Clock(-25, -160);
        Assert.Equal("20:20", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Negative_hour_and_minutes_both_roll_over_continuously()
    {
        var sut = new Clock(-121, -5810);
        Assert.Equal("22:10", sut.ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Add_minutes()
    {
        var sut = new Clock(10, 0);
        Assert.Equal("10:03", sut.Add(3).ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Add_no_minutes()
    {
        var sut = new Clock(6, 41);
        Assert.Equal("06:41", sut.Add(0).ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Add_to_next_hour()
    {
        var sut = new Clock(0, 45);
        Assert.Equal("01:25", sut.Add(40).ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Add_more_than_one_hour()
    {
        var sut = new Clock(10, 0);
        Assert.Equal("11:01", sut.Add(61).ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Add_more_than_two_hours_with_carry()
    {
        var sut = new Clock(0, 45);
        Assert.Equal("03:25", sut.Add(160).ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Add_across_midnight()
    {
        var sut = new Clock(23, 59);
        Assert.Equal("00:01", sut.Add(2).ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Add_more_than_one_day_1500_min_25_hrs_()
    {
        var sut = new Clock(5, 32);
        Assert.Equal("06:32", sut.Add(1500).ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Add_more_than_two_days()
    {
        var sut = new Clock(1, 1);
        Assert.Equal("11:21", sut.Add(3500).ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Subtract_minutes()
    {
        var sut = new Clock(10, 3);
        Assert.Equal("10:00", sut.Subtract(3).ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Subtract_to_previous_hour()
    {
        var sut = new Clock(10, 3);
        Assert.Equal("09:33", sut.Subtract(30).ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Subtract_more_than_an_hour()
    {
        var sut = new Clock(10, 3);
        Assert.Equal("08:53", sut.Subtract(70).ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Subtract_across_midnight()
    {
        var sut = new Clock(0, 3);
        Assert.Equal("23:59", sut.Subtract(4).ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Subtract_more_than_two_hours()
    {
        var sut = new Clock(0, 0);
        Assert.Equal("21:20", sut.Subtract(160).ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Subtract_more_than_two_hours_with_borrow()
    {
        var sut = new Clock(6, 15);
        Assert.Equal("03:35", sut.Subtract(160).ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Subtract_more_than_one_day_1500_min_25_hrs_()
    {
        var sut = new Clock(5, 32);
        Assert.Equal("04:32", sut.Subtract(1500).ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Subtract_more_than_two_days()
    {
        var sut = new Clock(2, 20);
        Assert.Equal("00:20", sut.Subtract(3000).ToString());
    }

    [Fact(Skip = "Remove to run test")]
    public void Clocks_with_same_time()
    {
        var sut = new Clock(15, 37);
        Assert.Equal(new Clock(15, 37), sut);
    }

    [Fact(Skip = "Remove to run test")]
    public void Clocks_a_minute_apart()
    {
        var sut = new Clock(15, 37);
        Assert.NotEqual(new Clock(15, 36), sut);
    }

    [Fact(Skip = "Remove to run test")]
    public void Clocks_an_hour_apart()
    {
        var sut = new Clock(15, 37);
        Assert.NotEqual(new Clock(14, 37), sut);
    }

    [Fact(Skip = "Remove to run test")]
    public void Clocks_with_hour_overflow()
    {
        var sut = new Clock(34, 37);
        Assert.Equal(new Clock(10, 37), sut);
    }

    [Fact(Skip = "Remove to run test")]
    public void Clocks_with_hour_overflow_by_several_days()
    {
        var sut = new Clock(99, 11);
        Assert.Equal(new Clock(3, 11), sut);
    }

    [Fact(Skip = "Remove to run test")]
    public void Clocks_with_negative_hour()
    {
        var sut = new Clock(-2, 40);
        Assert.Equal(new Clock(22, 40), sut);
    }

    [Fact(Skip = "Remove to run test")]
    public void Clocks_with_negative_hour_that_wraps()
    {
        var sut = new Clock(-31, 3);
        Assert.Equal(new Clock(17, 3), sut);
    }

    [Fact(Skip = "Remove to run test")]
    public void Clocks_with_negative_hour_that_wraps_multiple_times()
    {
        var sut = new Clock(-83, 49);
        Assert.Equal(new Clock(13, 49), sut);
    }

    [Fact(Skip = "Remove to run test")]
    public void Clocks_with_minute_overflow()
    {
        var sut = new Clock(0, 1441);
        Assert.Equal(new Clock(0, 1), sut);
    }

    [Fact(Skip = "Remove to run test")]
    public void Clocks_with_minute_overflow_by_several_days()
    {
        var sut = new Clock(2, 4322);
        Assert.Equal(new Clock(2, 2), sut);
    }

    [Fact(Skip = "Remove to run test")]
    public void Clocks_with_negative_minute()
    {
        var sut = new Clock(3, -20);
        Assert.Equal(new Clock(2, 40), sut);
    }

    [Fact(Skip = "Remove to run test")]
    public void Clocks_with_negative_minute_that_wraps()
    {
        var sut = new Clock(5, -1490);
        Assert.Equal(new Clock(4, 10), sut);
    }

    [Fact(Skip = "Remove to run test")]
    public void Clocks_with_negative_minute_that_wraps_multiple_times()
    {
        var sut = new Clock(6, -4305);
        Assert.Equal(new Clock(6, 15), sut);
    }

    [Fact(Skip = "Remove to run test")]
    public void Clocks_with_negative_hours_and_minutes()
    {
        var sut = new Clock(-12, -268);
        Assert.Equal(new Clock(7, 32), sut);
    }

    [Fact(Skip = "Remove to run test")]
    public void Clocks_with_negative_hours_and_minutes_that_wrap()
    {
        var sut = new Clock(-54, -11513);
        Assert.Equal(new Clock(18, 7), sut);
    }

    [Fact(Skip = "Remove to run test")]
    public void Full_clock_and_zeroed_clock()
    {
        var sut = new Clock(0, 0);
        Assert.Equal(new Clock(24, 0), sut);
    }

    [Fact(Skip = "Remove to run test")]
    public void Clocks_are_immutable()
    {
        var sut = new Clock(0, 0);
        var sutPlus1 = sut.Add(1);
        Assert.NotEqual(sutPlus1, sut);
    }
}
public struct Clock
{
    public Clock(int hours, int minutes)
    {
        int totalMinutes = (hours * 60 + minutes) % 1440;
        if (totalMinutes < 0)
        {
            totalMinutes += 1440;
        }

        Minutes = totalMinutes % 60;
        Hours = (totalMinutes / 60) % 24;
    }

    public int Hours { get; }

    public int Minutes { get; }

    public Clock Add(int minutesToAdd) => new Clock(Hours, Minutes + minutesToAdd);

    public Clock Subtract(int minutesToSubtract) => new Clock(Hours, Minutes - minutesToSubtract);

    public override string ToString() => $"{Hours.ToString("D2")}:{Minutes.ToString("D2")}";
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?