Sha256: a051b740fb0a3479d4e1c6916f3ce6fd848ac102b7a0400672e917e1e1878eae

Contents?: true

Size: 1.22 KB

Versions: 107

Compression:

Stored size: 1.22 KB

Contents

class Clock {
    private static final int MINUTES_IN_AN_HOUR = 60;
    private static final int HOURS_IN_A_DAY = 24;

    private int hours;
    private int minutes;

    Clock(int hours, int minutes) {
        this.hours = hours;
        this.minutes = minutes;
        sanitiseTime();
    }

    void add(int minutes) {
        this.minutes += minutes;
        sanitiseTime();
    }

    private void sanitiseTime() {
        while (minutes < 0) {
            minutes += MINUTES_IN_AN_HOUR;
            hours--;
        }
        while (hours < 0) {
            hours += HOURS_IN_A_DAY;
        }
        int minutesOverflow = minutes / MINUTES_IN_AN_HOUR;
        minutes = minutes % MINUTES_IN_AN_HOUR;
        hours = (hours + minutesOverflow) % HOURS_IN_A_DAY;
    }

    @Override
    public String toString() {
        return toTimeString(hours) + ":" + toTimeString(minutes);
    }

    private String toTimeString(int number) {
        if (number < 10) return "0" + number;
        return String.valueOf(number);
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Clock)) return false;

        Clock clock = (Clock) obj;
        return hours == clock.hours && minutes == clock.minutes;
    }
}

Version data entries

107 entries across 107 versions & 1 rubygems

Version Path
trackler-2.2.1.78 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.77 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.76 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.75 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.74 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.73 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.72 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java