Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 🔗

Expand Down
22 changes: 22 additions & 0 deletions src/main/java/pl/mperor/interview/tasks/exception/Sheep.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pl.mperor.interview.tasks.exception;

class SomethingExceptionallyGood extends RuntimeException {

SomethingExceptionallyGood(String message) {
super(message);
}
}
21 changes: 21 additions & 0 deletions src/main/java/pl/mperor/interview/tasks/exception/Wolf.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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();
}
}

}
Loading