Skip to content

Commit 71029bb

Browse files
committed
Implement "wolf and sheep" task solution (#8)
1 parent d9aaa79 commit 71029bb

5 files changed

Lines changed: 92 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ are not the only possible solutions to these cases.
2424
* [String Challenges 🔡](src/test/java/pl/mperor/interview/tasks/challenge/StringChallengeTest.java)
2525
* [Number Challenges 🔢](src/test/java/pl/mperor/interview/tasks/challenge/NumberChallengeTest.java)
2626
7. [Quiz Questions ❔](src/test/java/pl/mperor/interview/tasks/QuizQuestionsTest.java)
27+
8. [Wolf 🐺 & Sheep 🐑](src/test/java/pl/mperor/interview/tasks/exception/WolfFullAndSheepWholeTest.java)
2728

2829
## Sources 🔗
2930

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package pl.mperor.interview.tasks.exception;
2+
3+
class Sheep {
4+
5+
private boolean alive = true;
6+
7+
boolean isAlive() {
8+
return alive;
9+
}
10+
11+
void setAlive(boolean alive) {
12+
this.alive = alive;
13+
}
14+
15+
/**
16+
* No matter what the sheep says, the wolf will eat it anyway.
17+
* Unless the sheep offers something exceptionally good :)
18+
*/
19+
boolean doesWantToBeEaten() {
20+
return false;
21+
}
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package pl.mperor.interview.tasks.exception;
2+
3+
class SomethingExceptionallyGood extends RuntimeException {
4+
5+
SomethingExceptionallyGood(String message) {
6+
super(message);
7+
}
8+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package pl.mperor.interview.tasks.exception;
2+
3+
class Wolf {
4+
5+
private boolean hungry = true;
6+
7+
boolean isHungry() {
8+
return hungry;
9+
}
10+
11+
void eat(Sheep sheep) {
12+
sheep.setAlive(false);
13+
hungry = false;
14+
System.out.println("The wolf 🐺 devoured the sheep 🐑!");
15+
}
16+
17+
void eat(SomethingExceptionallyGood something) {
18+
hungry = false;
19+
System.out.printf("The wolf 🐺 has eaten %s%n", something.getMessage());
20+
}
21+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package pl.mperor.interview.tasks.exception;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class WolfFullAndSheepWholeTest {
7+
8+
@Test
9+
public void shouldFeedWolfAndKeepSheepAlive() {
10+
Wolf wolf = new Wolf();
11+
Sheep sheep = new Sheep();
12+
feedTheWolf(wolf, sheep, () -> {
13+
// Todo: Try to save sheep!
14+
throw new SomethingExceptionallyGood("candies 🍬");
15+
});
16+
Assertions.assertTrue(!wolf.isHungry() && sheep.isAlive(), "The wolf should not be hungry and the sheep alive!");
17+
}
18+
19+
/**
20+
* The wolf should eat the sheep. However, if we offer something exceptionally good,
21+
* the wolf may choose to go for it instead.
22+
* <p>
23+
* Handle this exceptional situation appropriately.
24+
* Do not modify the existing code.
25+
*/
26+
private static void feedTheWolf(Wolf wolf, Sheep sheep, Runnable doSomethingToSaveSheep) {
27+
try {
28+
boolean willEatSheep = sheep.doesWantToBeEaten();
29+
if (willEatSheep || !willEatSheep) { // actually, the sheep has no say
30+
doSomethingToSaveSheep.run();
31+
wolf.eat(sheep);
32+
}
33+
} catch (SomethingExceptionallyGood something) {
34+
wolf.eat(something);
35+
} finally {
36+
assert !wolf.isHungry();
37+
}
38+
}
39+
40+
}

0 commit comments

Comments
 (0)