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