-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Day 14: Throwing exceptions out the window!
- Loading branch information
1 parent
f432880
commit 17f2483
Showing
8 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package games; | ||
|
||
public class OutOfRangeException extends Exception { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters