Skip to content

Commit 17f2483

Browse files
committed
Day 14: Throwing exceptions out the window!
1 parent f432880 commit 17f2483

File tree

8 files changed

+148
-0
lines changed

8 files changed

+148
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Here are the different challenges :
5959
- [Day 11: Gather a dependency freshness metric.](exercise/day11/docs/challenge.md)
6060
- [Day 12: Make your code open for extension.](exercise/day12/docs/challenge.md)
6161
- [Day 13: Find a way to eliminate the irrelevant, and amplify the essentials of those tests.](exercise/day13/docs/challenge.md)
62+
- [Day 14: Do not use exceptions anymore.](exercise/day14/docs/challenge.md)
6263

6364
### Solutions
6465
A solution proposal will be published here every day during the `Advent Of Craft` containing `the code` and a `step by step` guide.

exercise/day14/docs/challenge.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Day 14: Throwing exceptions out the window!
2+
3+
Another day in your crafting journey.
4+
Today the weather is fair and nice.
5+
6+
This is a perfect day for inspiration.
7+
Today you are taking another step to higher levels.
8+
9+
The exercise of today seems simple, yet it is not.
10+
You will look at this code base from the previous days.
11+
12+
You can read it.
13+
14+
You can understand it.
15+
16+
_But can you still improve on it?_
17+
18+
While refactoring this code, always remember this:
19+
**You shall not break the tests.**
20+
21+
> **Challenge of day 14: Do not use exceptions anymore.**
22+
23+
May your crafting journey continue!
24+
25+
- <u>💡HINT:</u> Think of how you can return information about a faulty state.
26+
27+
![snippet of the day](snippet.png)

exercise/day14/docs/snippet.png

1.37 MB
Loading

exercise/day14/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.advent-of-craft</groupId>
9+
<artifactId>advent-of-craft2023</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>fizzbuzz-part4</artifactId>
14+
<version>1.0-SNAPSHOT</version>
15+
16+
17+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package games;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
import java.util.function.Predicate;
6+
7+
public class FizzBuzz {
8+
public static final int MIN = 0;
9+
public static final int MAX = 100;
10+
public static final int FIZZ = 3;
11+
public static final int BUZZ = 5;
12+
public static final int FIZZBUZZ = 15;
13+
14+
private static final Map<Predicate<Integer>, String> mapping;
15+
16+
static {
17+
mapping = new LinkedHashMap<>();
18+
mapping.put(i -> is(FIZZBUZZ, i), "FizzBuzz");
19+
mapping.put(i -> is(FIZZ, i), "Fizz");
20+
mapping.put(i -> is(BUZZ, i), "Buzz");
21+
}
22+
23+
private static boolean is(Integer divisor, Integer input) {
24+
return input % divisor == 0;
25+
}
26+
27+
private static boolean isOutOfRange(Integer input) {
28+
return input <= MIN || input > MAX;
29+
}
30+
31+
public static String convert(Integer input) throws OutOfRangeException {
32+
if (isOutOfRange(input)) {
33+
throw new OutOfRangeException();
34+
}
35+
return convertSafely(input);
36+
}
37+
38+
private static String convertSafely(Integer input) {
39+
return mapping.entrySet()
40+
.stream()
41+
.filter(f -> f.getKey().test(input))
42+
.findFirst()
43+
.map(Map.Entry::getValue)
44+
.orElseGet(input::toString);
45+
}
46+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package games;
2+
3+
public class OutOfRangeException extends Exception {
4+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package games;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.Arguments;
5+
import org.junit.jupiter.params.provider.MethodSource;
6+
7+
import java.util.stream.Stream;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
11+
12+
class FizzBuzzTests {
13+
14+
public static Stream<Arguments> validInputs() {
15+
return Stream.of(
16+
Arguments.of(1, "1"),
17+
Arguments.of(67, "67"),
18+
Arguments.of(82, "82"),
19+
Arguments.of(3, "Fizz"),
20+
Arguments.of(66, "Fizz"),
21+
Arguments.of(99, "Fizz"),
22+
Arguments.of(5, "Buzz"),
23+
Arguments.of(50, "Buzz"),
24+
Arguments.of(85, "Buzz"),
25+
Arguments.of(15, "FizzBuzz"),
26+
Arguments.of(30, "FizzBuzz"),
27+
Arguments.of(45, "FizzBuzz")
28+
);
29+
}
30+
31+
public static Stream<Arguments> invalidInputs() {
32+
return Stream.of(
33+
Arguments.of(0),
34+
Arguments.of(-1),
35+
Arguments.of(101)
36+
);
37+
}
38+
39+
@ParameterizedTest
40+
@MethodSource("validInputs")
41+
void returns_number_representation(int input, String expectedResult) throws OutOfRangeException {
42+
assertThat(FizzBuzz.convert(input))
43+
.isEqualTo(expectedResult);
44+
}
45+
46+
@ParameterizedTest
47+
@MethodSource("invalidInputs")
48+
void throws_an_exception_for_numbers_out_of_range(int input) {
49+
assertThatThrownBy(() -> FizzBuzz.convert(input))
50+
.isInstanceOf(OutOfRangeException.class);
51+
}
52+
}

exercise/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<module>day11</module>
2323
<module>day12</module>
2424
<module>day13</module>
25+
<module>day14</module>
2526
</modules>
2627

2728
<properties>

0 commit comments

Comments
 (0)