From 4cbd53aa8a111724c8763ff6693318363afeafe9 Mon Sep 17 00:00:00 2001 From: mperor Date: Fri, 21 Feb 2025 15:37:39 +0100 Subject: [PATCH] Implement "wolf and sheep" task solution --- README.md | 1 + .../interview/tasks/exception/Sheep.java | 22 ++++++++++ .../exception/SomethingExceptionallyGood.java | 8 ++++ .../interview/tasks/exception/Wolf.java | 21 ++++++++++ .../exception/WolfFullAndSheepWholeTest.java | 40 +++++++++++++++++++ 5 files changed, 92 insertions(+) create mode 100644 src/main/java/pl/mperor/interview/tasks/exception/Sheep.java create mode 100644 src/main/java/pl/mperor/interview/tasks/exception/SomethingExceptionallyGood.java create mode 100644 src/main/java/pl/mperor/interview/tasks/exception/Wolf.java create mode 100644 src/test/java/pl/mperor/interview/tasks/exception/WolfFullAndSheepWholeTest.java diff --git a/README.md b/README.md index d157365..718e0e7 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ are not the only possible solutions to these cases. * [String Challenges 🔡](src/test/java/pl/mperor/interview/tasks/challenge/StringChallengeTest.java) * [Number Challenges 🔢](src/test/java/pl/mperor/interview/tasks/challenge/NumberChallengeTest.java) 7. [Quiz Questions ❔](src/test/java/pl/mperor/interview/tasks/QuizQuestionsTest.java) +8. [Wolf 🐕 & Sheep 🐑](src/test/java/pl/mperor/interview/tasks/exception/WolfFullAndSheepWholeTest.java) ## Sources 🔗 diff --git a/src/main/java/pl/mperor/interview/tasks/exception/Sheep.java b/src/main/java/pl/mperor/interview/tasks/exception/Sheep.java new file mode 100644 index 0000000..1ad4a1a --- /dev/null +++ b/src/main/java/pl/mperor/interview/tasks/exception/Sheep.java @@ -0,0 +1,22 @@ +package pl.mperor.interview.tasks.exception; + +class Sheep { + + private boolean alive = true; + + boolean isAlive() { + return alive; + } + + void setAlive(boolean alive) { + this.alive = alive; + } + + /** + * No matter what the sheep says, the wolf will eat it anyway. + * Unless the sheep offers something exceptionally good :) + */ + boolean doesWantToBeEaten() { + return false; + } +} \ No newline at end of file diff --git a/src/main/java/pl/mperor/interview/tasks/exception/SomethingExceptionallyGood.java b/src/main/java/pl/mperor/interview/tasks/exception/SomethingExceptionallyGood.java new file mode 100644 index 0000000..6e6db89 --- /dev/null +++ b/src/main/java/pl/mperor/interview/tasks/exception/SomethingExceptionallyGood.java @@ -0,0 +1,8 @@ +package pl.mperor.interview.tasks.exception; + +class SomethingExceptionallyGood extends RuntimeException { + + SomethingExceptionallyGood(String message) { + super(message); + } +} diff --git a/src/main/java/pl/mperor/interview/tasks/exception/Wolf.java b/src/main/java/pl/mperor/interview/tasks/exception/Wolf.java new file mode 100644 index 0000000..eef62a3 --- /dev/null +++ b/src/main/java/pl/mperor/interview/tasks/exception/Wolf.java @@ -0,0 +1,21 @@ +package pl.mperor.interview.tasks.exception; + +class Wolf { + + private boolean hunger = true; + + boolean isHunger() { + return hunger; + } + + void eat(Sheep sheep) { + sheep.setAlive(false); + hunger = false; + System.out.println("The wolf devoured the sheep 🐑!"); + } + + void eat(SomethingExceptionallyGood something) { + hunger = false; + System.out.printf("The wolf has eaten %s%n", something.getMessage()); + } +} diff --git a/src/test/java/pl/mperor/interview/tasks/exception/WolfFullAndSheepWholeTest.java b/src/test/java/pl/mperor/interview/tasks/exception/WolfFullAndSheepWholeTest.java new file mode 100644 index 0000000..5d931ba --- /dev/null +++ b/src/test/java/pl/mperor/interview/tasks/exception/WolfFullAndSheepWholeTest.java @@ -0,0 +1,40 @@ +package pl.mperor.interview.tasks.exception; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class WolfFullAndSheepWholeTest { + + @Test + public void shouldFeedWolfAndKeepSheepAlive() { + Wolf wolf = new Wolf(); + Sheep sheep = new Sheep(); + feedTheWolf(wolf, sheep, () -> { + // Todo: Try to save sheep! + throw new SomethingExceptionallyGood("candies 🍬"); + }); + Assertions.assertTrue(!wolf.isHunger() && sheep.isAlive(), "The wolf should not be hungry and the sheep alive!"); + } + + /** + * The wolf should eat the sheep. However, if we offer something exceptionally good, + * the wolf may choose to go for it instead. + *

+ * Handle this exceptional situation appropriately. + * Do not modify the existing code. + */ + private static void feedTheWolf(Wolf wolf, Sheep sheep, Runnable doSomethingToSaveSheep) { + try { + boolean willEatSheep = sheep.doesWantToBeEaten(); + if (willEatSheep || !willEatSheep) { // actually, the sheep has no say + doSomethingToSaveSheep.run(); + wolf.eat(sheep); + } + } catch (SomethingExceptionallyGood something) { + wolf.eat(something); + } finally { + assert !wolf.isHunger(); + } + } + +}