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.180 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.179 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.178 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.177 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.176 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.175 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.174 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.173 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.172 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.171 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.170 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.169 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.167 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.166 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.165 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.164 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.163 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.162 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.161 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.160 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java