Skip to content

Commit 462f46c

Browse files
committed
few updates to 99 bottles implementations while prepping for workshop
1 parent d30dd93 commit 462f46c

10 files changed

+136
-90
lines changed
+4-35
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,10 @@
11
package lars.katas.bottles;
22

3-
import java.util.stream.Collectors;
4-
import java.util.stream.Stream;
3+
public interface Bottles {
54

6-
public class Bottles {
5+
public String verse(int n);
76

8-
public String verse(int n) {
9-
if (n == 0) {
10-
return "No more bottles of beer on the wall, no more bottles of beer.\n"
11-
+ "Go to the store and buy some more, 99 bottles of beer on the wall.\n";
12-
}
7+
public String verses(int start, int end);
138

14-
String result = "";
15-
16-
String bottles = n == 1 ? "bottle" : "bottles";
17-
result += String.format("%d %s of beer on the wall, %d %s of beer.\n", n, bottles, n, bottles);
18-
19-
int afterBottleCount = n - 1;
20-
String afterBottles = n - 1 == 1 ? "bottle" : "bottles";
21-
22-
if (afterBottleCount == 0) {
23-
result += "Take it down and pass it around, ";
24-
result += "no more bottles of beer on the wall.\n";
25-
} else {
26-
result += "Take one down and pass it around, ";
27-
result += String.format("%d %s of beer on the wall.\n", afterBottleCount, afterBottles);
28-
}
29-
return result;
30-
}
31-
32-
public String verses(int start, int end) {
33-
return Stream.iterate(start, i -> i >= end, i -> i - 1)
34-
.map(this::verse)
35-
.collect(Collectors.joining("\n"));
36-
}
37-
38-
public String song() {
39-
return verses(99, 0);
40-
}
9+
public String song();
4110
}

src/main/java/lars/katas/bottles/OpenBottles.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,7 @@
33
import java.util.stream.Collectors;
44
import java.util.stream.Stream;
55

6-
public class OpenBottles extends Bottles {
7-
8-
@Override
9-
public String song() {
10-
return verses(99, 0);
11-
}
12-
13-
@Override
14-
public String verses(int start, int end) {
15-
return Stream.iterate(start, i -> i >= end, i -> i - 1)
16-
.map(this::verse)
17-
.collect(Collectors.joining("\n"));
18-
}
6+
public class OpenBottles implements Bottles {
197

208
@Override
219
public String verse(int number) {
@@ -30,6 +18,18 @@ public String verse(int number) {
3018
+ " of beer on the wall.\n";
3119
}
3220

21+
@Override
22+
public String verses(int start, int end) {
23+
return Stream.iterate(start, i -> i >= end, i -> i - 1)
24+
.map(this::verse)
25+
.collect(Collectors.joining("\n"));
26+
}
27+
28+
@Override
29+
public String song() {
30+
return verses(99, 0);
31+
}
32+
3333
private String capitalize(String text) {
3434
return text.substring(0, 1).toUpperCase() + text.substring(1);
3535
}

src/main/java/lars/katas/bottles/ShamelessGreenBottles.java

-33
This file was deleted.

src/main/java/lars/katas/bottles/ConciseBottles.java src/main/java/lars/katas/bottles/Solution_1_IncomprehensiblyConciseBottles.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.stream.Collectors;
44
import java.util.stream.Stream;
55

6-
public class ConciseBottles extends Bottles {
6+
public class Solution_1_IncomprehensiblyConciseBottles implements Bottles {
77

88
@Override
99
public String verse(int n) {
@@ -29,4 +29,9 @@ public String verses(int start, int end) {
2929
.map(this::verse)
3030
.collect(Collectors.joining("\n"));
3131
}
32+
33+
@Override
34+
public String song() {
35+
return verses(99, 0);
36+
}
3237
}

src/main/java/lars/katas/bottles/SpeculativelyGeneralBottles.java src/main/java/lars/katas/bottles/Solution_2_SpeculativelyGeneralBottles.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.stream.Collectors;
55
import java.util.stream.Stream;
66

7-
public class SpeculativelyGeneralBottles extends Bottles {
7+
public class Solution_2_SpeculativelyGeneralBottles implements Bottles {
88

99
private Function<Verse, String> noMore =
1010
(verse) ->
@@ -50,6 +50,11 @@ public String verses(int start, int end) {
5050
.collect(Collectors.joining("\n"));
5151
}
5252

53+
@Override
54+
public String song() {
55+
return verses(99, 0);
56+
}
57+
5358
private Verse verseFor(int number) {
5459
switch (number) {
5560
case 0:

src/main/java/lars/katas/bottles/ConcretelyAbstractBottles.java src/main/java/lars/katas/bottles/Solution_3_ConcretelyAbstractBottles.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
package lars.katas.bottles;
22

3-
public class ConcretelyAbstractBottles extends Bottles {
3+
import java.util.stream.IntStream;
4+
5+
public class Solution_3_ConcretelyAbstractBottles implements Bottles {
46

57
@Override
68
public String verse(int bottles) {
79
return new Round(bottles).toString();
810
}
911

12+
@Override
13+
public String verses(int start, int end) {
14+
return IntStream.iterate(start, i -> i >= end, i -> i - 1)
15+
.mapToObj(this::verse)
16+
.reduce((v1, v2) -> v1 + "\n" + v2)
17+
.orElse("");
18+
}
19+
20+
@Override
21+
public String song() {
22+
return verses(99, 0);
23+
}
24+
1025
private class Round {
1126

1227
private int bottles;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package lars.katas.bottles;
2+
3+
import java.util.stream.IntStream;
4+
5+
public class Solution_4_ShamelessGreenBottles implements Bottles {
6+
7+
@Override
8+
public String verse(int number) {
9+
return switch (number) {
10+
case 0 -> "No more bottles of beer on the wall, "
11+
+ "no more bottles of beer.\n"
12+
+ "Go to the store and buy some more, "
13+
+ "99 bottles of beer on the wall.\n";
14+
case 1 -> "1 bottle of beer on the wall, "
15+
+ "1 bottle of beer.\n"
16+
+ "Take it down and pass it around, "
17+
+ "no more bottles of beer on the wall.\n";
18+
case 2 -> "2 bottles of beer on the wall, "
19+
+ "2 bottles of beer.\n"
20+
+ "Take one down and pass it around, "
21+
+ "1 bottle of beer on the wall.\n";
22+
default -> number
23+
+ " bottles of beer on the wall, "
24+
+ number
25+
+ " bottles of beer.\n"
26+
+ "Take one down and pass it around, "
27+
+ (number - 1)
28+
+ " bottles of beer on the wall.\n";
29+
};
30+
}
31+
32+
@Override
33+
public String verses(int start, int end) {
34+
return IntStream.iterate(start, i -> i >= end, i -> i - 1)
35+
.mapToObj(this::verse)
36+
.reduce((v1, v2) -> v1 + "\n" + v2)
37+
.orElse("");
38+
}
39+
40+
@Override
41+
public String song() {
42+
return verses(99, 0);
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package lars.katas.bottles;
2+
3+
import java.util.stream.Collectors;
4+
import java.util.stream.Stream;
5+
6+
public class Solution_5_LarsFirstAttemptBottles implements Bottles {
7+
8+
public String verse(int n) {
9+
if (n == 0) {
10+
return "No more bottles of beer on the wall, no more bottles of beer.\n"
11+
+ "Go to the store and buy some more, 99 bottles of beer on the wall.\n";
12+
}
13+
14+
String result = "";
15+
16+
String bottles = n == 1 ? "bottle" : "bottles";
17+
result += String.format("%d %s of beer on the wall, %d %s of beer.\n", n, bottles, n, bottles);
18+
19+
int afterBottleCount = n - 1;
20+
String afterBottles = n - 1 == 1 ? "bottle" : "bottles";
21+
22+
if (afterBottleCount == 0) {
23+
result += "Take it down and pass it around, ";
24+
result += "no more bottles of beer on the wall.\n";
25+
} else {
26+
result += "Take one down and pass it around, ";
27+
result += String.format("%d %s of beer on the wall.\n", afterBottleCount, afterBottles);
28+
}
29+
return result;
30+
}
31+
32+
public String verses(int start, int end) {
33+
return Stream.iterate(start, i -> i >= end, i -> i - 1)
34+
.map(this::verse)
35+
.collect(Collectors.joining("\n"));
36+
}
37+
38+
public String song() {
39+
return verses(99, 0);
40+
}
41+
}

src/test/java/lars/katas/bottles/BottlesTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
1111
class BottlesTest {
1212

13-
private final Bottles bottles = new ShamelessGreenBottles();
13+
private final Bottles bottles = new Solution_4_ShamelessGreenBottles();
1414

1515
@Test
1616
void test_the_first_verse() {

src/test/java/lars/katas/bottles/ParameterizedBottlesTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ class ParameterizedBottlesTest {
1111

1212
private static Stream<Arguments> bottles() {
1313
return Stream.of(
14-
Arguments.of(new Bottles()),
15-
Arguments.of(new ConciseBottles()),
16-
Arguments.of(new SpeculativelyGeneralBottles()),
17-
Arguments.of(new ConcretelyAbstractBottles()),
18-
Arguments.of(new ShamelessGreenBottles()));
14+
Arguments.of(new Solution_5_LarsFirstAttemptBottles()),
15+
Arguments.of(new Solution_1_IncomprehensiblyConciseBottles()),
16+
Arguments.of(new Solution_2_SpeculativelyGeneralBottles()),
17+
Arguments.of(new Solution_3_ConcretelyAbstractBottles()),
18+
Arguments.of(new Solution_4_ShamelessGreenBottles()));
1919
}
2020

2121
@ParameterizedTest

0 commit comments

Comments
 (0)