Skip to content

Commit

Permalink
Day 14: Throwing exceptions out the window!
Browse files Browse the repository at this point in the history
  • Loading branch information
yanncourtel committed Dec 14, 2023
1 parent f432880 commit 17f2483
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Here are the different challenges :
- [Day 11: Gather a dependency freshness metric.](exercise/day11/docs/challenge.md)
- [Day 12: Make your code open for extension.](exercise/day12/docs/challenge.md)
- [Day 13: Find a way to eliminate the irrelevant, and amplify the essentials of those tests.](exercise/day13/docs/challenge.md)
- [Day 14: Do not use exceptions anymore.](exercise/day14/docs/challenge.md)

### Solutions
A solution proposal will be published here every day during the `Advent Of Craft` containing `the code` and a `step by step` guide.
Expand Down
27 changes: 27 additions & 0 deletions exercise/day14/docs/challenge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## Day 14: Throwing exceptions out the window!

Another day in your crafting journey.
Today the weather is fair and nice.

This is a perfect day for inspiration.
Today you are taking another step to higher levels.

The exercise of today seems simple, yet it is not.
You will look at this code base from the previous days.

You can read it.

You can understand it.

_But can you still improve on it?_

While refactoring this code, always remember this:
**You shall not break the tests.**

> **Challenge of day 14: Do not use exceptions anymore.**
May your crafting journey continue!

- <u>💡HINT:</u> Think of how you can return information about a faulty state.

![snippet of the day](snippet.png)
Binary file added exercise/day14/docs/snippet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions exercise/day14/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.advent-of-craft</groupId>
<artifactId>advent-of-craft2023</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>fizzbuzz-part4</artifactId>
<version>1.0-SNAPSHOT</version>


</project>
46 changes: 46 additions & 0 deletions exercise/day14/src/main/java/games/FizzBuzz.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package games;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Predicate;

public class FizzBuzz {
public static final int MIN = 0;
public static final int MAX = 100;
public static final int FIZZ = 3;
public static final int BUZZ = 5;
public static final int FIZZBUZZ = 15;

private static final Map<Predicate<Integer>, String> mapping;

static {
mapping = new LinkedHashMap<>();
mapping.put(i -> is(FIZZBUZZ, i), "FizzBuzz");
mapping.put(i -> is(FIZZ, i), "Fizz");
mapping.put(i -> is(BUZZ, i), "Buzz");
}

private static boolean is(Integer divisor, Integer input) {
return input % divisor == 0;
}

private static boolean isOutOfRange(Integer input) {
return input <= MIN || input > MAX;
}

public static String convert(Integer input) throws OutOfRangeException {
if (isOutOfRange(input)) {
throw new OutOfRangeException();
}
return convertSafely(input);
}

private static String convertSafely(Integer input) {
return mapping.entrySet()
.stream()
.filter(f -> f.getKey().test(input))
.findFirst()
.map(Map.Entry::getValue)
.orElseGet(input::toString);
}
}
4 changes: 4 additions & 0 deletions exercise/day14/src/main/java/games/OutOfRangeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package games;

public class OutOfRangeException extends Exception {
}
52 changes: 52 additions & 0 deletions exercise/day14/src/test/java/games/FizzBuzzTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package games;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class FizzBuzzTests {

public static Stream<Arguments> validInputs() {
return Stream.of(
Arguments.of(1, "1"),
Arguments.of(67, "67"),
Arguments.of(82, "82"),
Arguments.of(3, "Fizz"),
Arguments.of(66, "Fizz"),
Arguments.of(99, "Fizz"),
Arguments.of(5, "Buzz"),
Arguments.of(50, "Buzz"),
Arguments.of(85, "Buzz"),
Arguments.of(15, "FizzBuzz"),
Arguments.of(30, "FizzBuzz"),
Arguments.of(45, "FizzBuzz")
);
}

public static Stream<Arguments> invalidInputs() {
return Stream.of(
Arguments.of(0),
Arguments.of(-1),
Arguments.of(101)
);
}

@ParameterizedTest
@MethodSource("validInputs")
void returns_number_representation(int input, String expectedResult) throws OutOfRangeException {
assertThat(FizzBuzz.convert(input))
.isEqualTo(expectedResult);
}

@ParameterizedTest
@MethodSource("invalidInputs")
void throws_an_exception_for_numbers_out_of_range(int input) {
assertThatThrownBy(() -> FizzBuzz.convert(input))
.isInstanceOf(OutOfRangeException.class);
}
}
1 change: 1 addition & 0 deletions exercise/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<module>day11</module>
<module>day12</module>
<module>day13</module>
<module>day14</module>
</modules>

<properties>
Expand Down

0 comments on commit 17f2483

Please sign in to comment.