Sha256: 67d8c5aff3c0d4ac02fc8c36664056b75cc31a48d5707e16af983b1d08169b28

Contents?: true

Size: 1.24 KB

Versions: 208

Compression:

Stored size: 1.24 KB

Contents

public 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;

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

    public 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

208 entries across 208 versions & 1 rubygems

Version Path
trackler-2.2.1.71 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.70 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.69 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.68 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.67 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.66 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.65 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.64 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.63 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.62 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.61 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.60 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.59 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.58 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.57 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.56 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.55 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.54 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.53 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java
trackler-2.2.1.52 tracks/java/exercises/clock/.meta/src/reference/java/Clock.java