Просмотр исходного кода

Clock: add to track (#260)

* dibs: I will implement exercise Clock

* Implemented clock exercise
FridaTveit 9 лет назад
Родитель
Сommit
077f563cc5

+ 6
- 0
config.json Просмотреть файл

@@ -58,6 +58,7 @@
58 58
     "sublist",
59 59
     "rectangles",
60 60
     "matrix",
61
+    "clock",
61 62
     "diamond",
62 63
     "secret-handshake",
63 64
     "flatten-array"
@@ -334,6 +335,11 @@
334 335
       "topics": []
335 336
     },
336 337
     {
338
+      "slug": "clock",
339
+      "difficulty": 1,
340
+      "topics": []
341
+    },
342
+    {
337 343
       "slug": "diamond",
338 344
       "difficulty": 1,
339 345
       "topics": []

+ 17
- 0
exercises/clock/build.gradle Просмотреть файл

@@ -0,0 +1,17 @@
1
+apply plugin: "java"
2
+apply plugin: "eclipse"
3
+apply plugin: "idea"
4
+
5
+repositories {
6
+    mavenCentral()
7
+}
8
+
9
+dependencies {
10
+    testCompile "junit:junit:4.12"
11
+}
12
+test {
13
+    testLogging {
14
+        exceptionFormat = 'full'
15
+        events = ["passed", "failed", "skipped"]
16
+    }
17
+}

+ 49
- 0
exercises/clock/src/example/java/Clock.java Просмотреть файл

@@ -0,0 +1,49 @@
1
+public class Clock {
2
+    private static final int MINUTES_IN_AN_HOUR = 60;
3
+    private static final int HOURS_IN_A_DAY = 24;
4
+
5
+    private int hours;
6
+    private int minutes;
7
+
8
+    public Clock(int hours, int minutes) {
9
+        this.hours = hours;
10
+        this.minutes = minutes;
11
+        sanitiseTime();
12
+    }
13
+
14
+    public void add(int minutes) {
15
+        this.minutes += minutes;
16
+        sanitiseTime();
17
+    }
18
+
19
+    private void sanitiseTime() {
20
+        while (minutes < 0) {
21
+            minutes += MINUTES_IN_AN_HOUR;
22
+            hours--;
23
+        }
24
+        while (hours < 0) {
25
+            hours += HOURS_IN_A_DAY;
26
+        }
27
+        int minutesOverflow = minutes / MINUTES_IN_AN_HOUR;
28
+        minutes = minutes % MINUTES_IN_AN_HOUR;
29
+        hours = (hours + minutesOverflow) % HOURS_IN_A_DAY;
30
+    }
31
+
32
+    @Override
33
+    public String toString() {
34
+        return toTimeString(hours) + ":" + toTimeString(minutes);
35
+    }
36
+
37
+    private String toTimeString(int number) {
38
+        if (number < 10) return "0" + number;
39
+        return String.valueOf(number);
40
+    }
41
+
42
+    @Override
43
+    public boolean equals(Object obj) {
44
+        if (!(obj instanceof Clock)) return false;
45
+
46
+        Clock clock = (Clock) obj;
47
+        return hours == clock.hours && minutes == clock.minutes;
48
+    }
49
+}

+ 0
- 0
exercises/clock/src/main/java/.keep Просмотреть файл


+ 135
- 0
exercises/clock/src/test/java/ClockAddTest.java Просмотреть файл

@@ -0,0 +1,135 @@
1
+import org.junit.Ignore;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.assertEquals;
5
+
6
+public class ClockAddTest {
7
+
8
+    @Ignore
9
+    @Test
10
+    public void addMinutes() {
11
+        Clock clock = new Clock(10, 0);
12
+        clock.add(3);
13
+        assertEquals("10:03", clock.toString());
14
+    }
15
+
16
+    @Ignore
17
+    @Test
18
+    public void addNoMinutes() {
19
+        Clock clock = new Clock(6, 41);
20
+        clock.add(0);
21
+        assertEquals("06:41", clock.toString());
22
+    }
23
+
24
+    @Ignore
25
+    @Test
26
+    public void addToNextHour() {
27
+        Clock clock = new Clock(0, 45);
28
+        clock.add(40);
29
+        assertEquals("01:25", clock.toString());
30
+    }
31
+
32
+    @Ignore
33
+    @Test
34
+    public void addMoreThanOneHour() {
35
+        Clock clock = new Clock(10, 0);
36
+        clock.add(61);
37
+        assertEquals("11:01", clock.toString());
38
+    }
39
+
40
+    @Ignore
41
+    @Test
42
+    public void addMoreThanTwoHoursWithCarry() {
43
+        Clock clock = new Clock(0, 45);
44
+        clock.add(160);
45
+        assertEquals("03:25", clock.toString());
46
+    }
47
+
48
+    @Ignore
49
+    @Test
50
+    public void addAcrossMidnight() {
51
+        Clock clock = new Clock(23, 59);
52
+        clock.add(2);
53
+        assertEquals("00:01", clock.toString());
54
+    }
55
+
56
+    @Ignore
57
+    @Test
58
+    public void addMoreThanOneDay() {
59
+        Clock clock = new Clock(5, 32);
60
+        clock.add(1500);
61
+        assertEquals("06:32", clock.toString());
62
+    }
63
+
64
+    @Ignore
65
+    @Test
66
+    public void addMoreThanTwoDays() {
67
+        Clock clock = new Clock(1, 1);
68
+        clock.add(3500);
69
+        assertEquals("11:21", clock.toString());
70
+    }
71
+
72
+    @Ignore
73
+    @Test
74
+    public void subtractMinutes() {
75
+        Clock clock = new Clock(10, 3);
76
+        clock.add(-3);
77
+        assertEquals("10:00", clock.toString());
78
+    }
79
+
80
+    @Ignore
81
+    @Test
82
+    public void subtractToPreviousHour() {
83
+        Clock clock = new Clock(10, 3);
84
+        clock.add(-30);
85
+        assertEquals("09:33", clock.toString());
86
+    }
87
+
88
+    @Ignore
89
+    @Test
90
+    public void subtractMoreThanAnHour() {
91
+        Clock clock = new Clock(10, 3);
92
+        clock.add(-70);
93
+        assertEquals("08:53", clock.toString());
94
+    }
95
+
96
+    @Ignore
97
+    @Test
98
+    public void subtractAcrossMidnight() {
99
+        Clock clock = new Clock(0, 3);
100
+        clock.add(-4);
101
+        assertEquals("23:59", clock.toString());
102
+    }
103
+
104
+    @Ignore
105
+    @Test
106
+    public void subtractMoreThanTwoHours() {
107
+        Clock clock = new Clock(0, 0);
108
+        clock.add(-160);
109
+        assertEquals("21:20", clock.toString());
110
+    }
111
+
112
+    @Ignore
113
+    @Test
114
+    public void subtractMoreThanTwoHoursWithBorrow() {
115
+        Clock clock = new Clock(6, 15);
116
+        clock.add(-160);
117
+        assertEquals("03:35", clock.toString());
118
+    }
119
+
120
+    @Ignore
121
+    @Test
122
+    public void subtractMoreThanOneDay() {
123
+        Clock clock = new Clock(5, 32);
124
+        clock.add(-1500);
125
+        assertEquals("04:32", clock.toString());
126
+    }
127
+
128
+    @Ignore
129
+    @Test
130
+    public void subtractMoreThanTwoDays() {
131
+        Clock clock = new Clock(2, 20);
132
+        clock.add(-3000);
133
+        assertEquals("00:20", clock.toString());
134
+    }
135
+}

+ 120
- 0
exercises/clock/src/test/java/ClockCreationTest.java Просмотреть файл

@@ -0,0 +1,120 @@
1
+import org.junit.Ignore;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.assertEquals;
5
+
6
+public class ClockCreationTest {
7
+
8
+    @Test
9
+    public void canPrintTimeOnTheHour() {
10
+        assertEquals("08:00", new Clock(8, 0).toString());
11
+    }
12
+
13
+    @Ignore
14
+    @Test
15
+    public void canPrintTimeWithMinutes() {
16
+        assertEquals("11:09", new Clock(11, 9).toString());
17
+    }
18
+
19
+    @Ignore
20
+    @Test
21
+    public void midnightPrintsAsZero() {
22
+        assertEquals("00:00", new Clock(24, 0).toString());
23
+    }
24
+
25
+    @Ignore
26
+    @Test
27
+    public void hourRollsOver() {
28
+        assertEquals("01:00", new Clock(25, 0).toString());
29
+    }
30
+
31
+    @Ignore
32
+    @Test
33
+    public void hourRollsOverContinuously() {
34
+        assertEquals("04:00", new Clock(100, 0).toString());
35
+    }
36
+
37
+    @Ignore
38
+    @Test
39
+    public void sixtyMinutesIsNextHour() {
40
+        assertEquals("02:00", new Clock(1, 60).toString());
41
+    }
42
+
43
+    @Ignore
44
+    @Test
45
+    public void minutesRollOver() {
46
+        assertEquals("02:40", new Clock(0, 160).toString());
47
+    }
48
+
49
+    @Ignore
50
+    @Test
51
+    public void minutesRollOverContinuously() {
52
+        assertEquals("04:43", new Clock(0, 1723).toString());
53
+    }
54
+
55
+    @Ignore
56
+    @Test
57
+    public void hourAndMinutesRollOver() {
58
+        assertEquals("03:40", new Clock(25, 160).toString());
59
+    }
60
+
61
+    @Ignore
62
+    @Test
63
+    public void hourAndMinutesRollOverContinuously() {
64
+        assertEquals("11:01", new Clock(201, 3001).toString());
65
+    }
66
+
67
+    @Ignore
68
+    @Test
69
+    public void hourAndMinutesRollOverToExactlyMidnight() {
70
+        assertEquals("00:00", new Clock(72, 8640).toString());
71
+    }
72
+
73
+    @Ignore
74
+    @Test
75
+    public void negativeHour() {
76
+        assertEquals("23:15", new Clock(-1, 15).toString());
77
+    }
78
+
79
+    @Ignore
80
+    @Test
81
+    public void negativeHourRollsOver() {
82
+        assertEquals("23:00", new Clock(-25, 0).toString());
83
+    }
84
+
85
+    @Ignore
86
+    @Test
87
+    public void negativeHourRollsOverContinuously() {
88
+        assertEquals("05:00", new Clock(-91, 0).toString());
89
+    }
90
+
91
+    @Ignore
92
+    @Test
93
+    public void negativeMinutes() {
94
+        assertEquals("00:20", new Clock(1, -40).toString());
95
+    }
96
+
97
+    @Ignore
98
+    @Test
99
+    public void negativeMinutesRollOver() {
100
+        assertEquals("22:20", new Clock(1, -160).toString());
101
+    }
102
+
103
+    @Ignore
104
+    @Test
105
+    public void negativeMinutesRollOverContinuously() {
106
+        assertEquals("16:40", new Clock(1, -4820).toString());
107
+    }
108
+
109
+    @Ignore
110
+    @Test
111
+    public void negativeHourAndMinutesBothRollOver() {
112
+        assertEquals("20:20", new Clock(-25, -160).toString());
113
+    }
114
+
115
+    @Ignore
116
+    @Test
117
+    public void negativeHourAndMinutesBothRollOverContinuously() {
118
+        assertEquals("22:10", new Clock(-121, -5810).toString());
119
+    }
120
+}

+ 98
- 0
exercises/clock/src/test/java/ClockEqualTest.java Просмотреть файл

@@ -0,0 +1,98 @@
1
+import org.junit.Ignore;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.assertFalse;
5
+import static org.junit.Assert.assertTrue;
6
+
7
+public class ClockEqualTest {
8
+
9
+    @Ignore
10
+    @Test
11
+    public void clocksWithSameTimeAreEqual() {
12
+        assertTrue(new Clock(15, 37).equals(new Clock(15, 37)));
13
+    }
14
+
15
+    @Ignore
16
+    @Test
17
+    public void clocksAMinuteApartAreNotEqual() {
18
+        assertFalse(new Clock(15, 36).equals(new Clock(15, 37)));
19
+    }
20
+
21
+    @Ignore
22
+    @Test
23
+    public void clocksAnHourApartAreNotEqual() {
24
+        assertFalse(new Clock(14, 37).equals(new Clock(15, 37)));
25
+    }
26
+
27
+    @Ignore
28
+    @Test
29
+    public void clocksWithHourOverflow() {
30
+        assertTrue(new Clock(10, 37).equals(new Clock(34, 37)));
31
+    }
32
+
33
+    @Ignore
34
+    @Test
35
+    public void clocksWithHourOverflowBySeveralDays() {
36
+        assertTrue(new Clock(3, 11).equals(new Clock(99, 11)));
37
+    }
38
+
39
+    @Ignore
40
+    @Test
41
+    public void clocksWithNegateHour() {
42
+        assertTrue(new Clock(22, 40).equals(new Clock(-2, 40)));
43
+    }
44
+
45
+    @Ignore
46
+    @Test
47
+    public void clocksWithNegativeHourThatWraps() {
48
+        assertTrue(new Clock(17, 3).equals(new Clock(-31, 3)));
49
+    }
50
+
51
+    @Ignore
52
+    @Test
53
+    public void clocksWithNegativeHourThatWrapsMultipleTimes() {
54
+        assertTrue(new Clock(13, 49).equals(new Clock(-83, 49)));
55
+    }
56
+
57
+    @Ignore
58
+    @Test
59
+    public void clocksWithMinuteOverflow() {
60
+        assertTrue(new Clock(0, 1).equals(new Clock(0, 1441)));
61
+    }
62
+
63
+    @Ignore
64
+    @Test
65
+    public void clocksWithMinuteOverflowBySeveralDays() {
66
+        assertTrue(new Clock(2, 2).equals(new Clock(2, 4322)));
67
+    }
68
+
69
+    @Ignore
70
+    @Test
71
+    public void clocksWithNegativeMinutes() {
72
+        assertTrue(new Clock(2, 40).equals(new Clock(3, -20)));
73
+    }
74
+
75
+    @Ignore
76
+    @Test
77
+    public void clocksWithNegativeMinutesThatWraps() {
78
+        assertTrue(new Clock(4, 10).equals(new Clock(5, -1490)));
79
+    }
80
+
81
+    @Ignore
82
+    @Test
83
+    public void clocksWithNegativeMinutesThatWrapsMultipleTimes() {
84
+        assertTrue(new Clock(6, 15).equals(new Clock(6, -4305)));
85
+    }
86
+
87
+    @Ignore
88
+    @Test
89
+    public void clocksWithNegativeHoursAndMinutes() {
90
+        assertTrue(new Clock(7, 32).equals(new Clock(-12, -268)));
91
+    }
92
+
93
+    @Ignore
94
+    @Test
95
+    public void clocksWithNegativeHoursAndMinutesThatWrap() {
96
+        assertTrue(new Clock(18, 7).equals(new Clock(-54, -11513)));
97
+    }
98
+}

+ 1
- 0
exercises/settings.gradle Просмотреть файл

@@ -10,6 +10,7 @@ include 'binary-search'
10 10
 include 'binary-search-tree'
11 11
 include 'bob'
12 12
 include 'bracket-push'
13
+include 'clock'
13 14
 include 'crypto-square'
14 15
 include 'custom-set'
15 16
 include 'diamond'