|
| 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 | +} |
0 commit comments