Skip to content

Commit c807679

Browse files
authored
Update bird-watcher to use best practices (exercism#1985)
* Update bird-watcher to use best practices * Fix indentation level for BirdWatcherTest
1 parent e3c4f94 commit c807679

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

exercises/concept/bird-watcher/.meta/src/reference/java/BirdWatcher.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

22
class BirdWatcher {
3-
private int[] birdsPerDay;
3+
private final int[] birdsPerDay;
44

55
public BirdWatcher(int[] birdsPerDay) {
6-
this.birdsPerDay = birdsPerDay;
6+
this.birdsPerDay = birdsPerDay.clone();
77
}
88

99
public int[] getLastWeek() {
10-
return birdsPerDay;
10+
return birdsPerDay.clone();
1111
}
1212

1313
public int getToday() {
@@ -17,9 +17,8 @@ public int getToday() {
1717
return birdsPerDay[birdsPerDay.length - 1];
1818
}
1919

20-
public int incrementTodaysCount() {
20+
public void incrementTodaysCount() {
2121
birdsPerDay[birdsPerDay.length - 1] = birdsPerDay[birdsPerDay.length - 1] + 1;
22-
return birdsPerDay[birdsPerDay.length - 1];
2322
}
2423

2524
public boolean hasDayWithoutBirds() {

exercises/concept/bird-watcher/src/main/java/BirdWatcher.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11

22
class BirdWatcher {
3-
private int[] birdsPerDay;
3+
private final int[] birdsPerDay;
44

55
public BirdWatcher(int[] birdsPerDay) {
6-
this.birdsPerDay = birdsPerDay;
6+
this.birdsPerDay = birdsPerDay.clone();
77
}
8+
89
public int[] getLastWeek() {
910
throw new UnsupportedOperationException("Please implement the BirdCount.getLastWeek() method");
1011
}
@@ -13,7 +14,7 @@ public int getToday() {
1314
throw new UnsupportedOperationException("Please implement the BirdCount.getToday() method");
1415
}
1516

16-
public int incrementTodaysCount() {
17+
public void incrementTodaysCount() {
1718
throw new UnsupportedOperationException("Please implement the BirdCount.incrementTodaysCount() method");
1819
}
1920

exercises/concept/bird-watcher/src/test/java/BirdWatcherTest.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77

88
public class BirdWatcherTest {
99

10+
private static final int DAY1 = 0;
11+
private static final int DAY2 = 2;
12+
private static final int DAY3 = 5;
13+
private static final int DAY4 = 3;
14+
private static final int DAY5 = 7;
15+
private static final int DAY6 = 8;
16+
private static final int TODAY = 4;
17+
1018
private BirdWatcher birdWatcher;
11-
private int lastWeek[] = {0, 2, 5, 3, 7, 8, 4};
19+
private int lastWeek[] = {DAY1, DAY2, DAY3, DAY4, DAY5, DAY6, TODAY};
1220

1321
@Before
1422
public void setUp() {
@@ -17,13 +25,14 @@ public void setUp() {
1725

1826
@Test
1927
public void itTestGetLastWeek() {
20-
assertThat(birdWatcher.getLastWeek().equals(lastWeek)).isTrue();
28+
assertThat(birdWatcher.getLastWeek())
29+
.containsExactly(DAY1, DAY2, DAY3, DAY4, DAY5, DAY6, TODAY);
2130
}
2231

2332
@Test
2433
@Ignore("Remove to run test")
2534
public void itTestGetToday() {
26-
assertThat(birdWatcher.getToday()).isEqualTo(lastWeek[lastWeek.length - 1]);
35+
assertThat(birdWatcher.getToday()).isEqualTo(TODAY);
2736
}
2837

2938
@Test
@@ -39,7 +48,7 @@ public void itShouldReturnZeroIfBirdWatcherLastWeekIsEmpty() {
3948
public void itIncrementTodaysCount() {
4049
int currentTodayCount = birdWatcher.getToday();
4150
birdWatcher.incrementTodaysCount();
42-
assertThat(birdWatcher.getToday()).isEqualTo(currentTodayCount + 1);
51+
assertThat(birdWatcher.getToday()).isEqualTo(TODAY + 1);
4352
}
4453

4554
@Test
@@ -59,18 +68,20 @@ public void itShouldNotHaveDaysWithoutBirds() {
5968
@Test
6069
@Ignore("Remove to run test")
6170
public void itTestGetCountForFirstDays() {
62-
assertThat(birdWatcher.getCountForFirstDays(4)).isEqualTo(10);
71+
assertThat(birdWatcher.getCountForFirstDays(4)).isEqualTo(DAY1 + DAY2 + DAY3 + DAY4);
6372
}
6473

6574
@Test
6675
@Ignore("Remove to run test")
6776
public void itTestGetCountForMoreDaysThanTheArraySize() {
68-
assertThat(birdWatcher.getCountForFirstDays(10)).isEqualTo(29);
77+
assertThat(birdWatcher.getCountForFirstDays(10))
78+
.isEqualTo(DAY1 + DAY2 + DAY3 + DAY4 + DAY5 + DAY6 + TODAY);
6979
}
7080

7181
@Test
7282
@Ignore("Remove to run test")
7383
public void itTestGetCountForBusyDays() {
84+
// DAY3, DAY5 and DAY6 are all >= 5 birds
7485
assertThat(birdWatcher.getBusyDays()).isEqualTo(3);
7586
}
7687

0 commit comments

Comments
 (0)