Commit 0fcef7e8 authored by Nordine AZZA's avatar Nordine AZZA

Wip kata

parents
Pipeline #25117 failed with stages
in 44 seconds
.idea
target
*.iml
<?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>
<groupId>org.example</groupId>
<artifactId>HalloweenTrickOrTreat</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package kata;
import java.util.List;
public class HalloweenKata {
public static final String CANDY = "candy";
public static final String TRICK_OR_TREAT = "Trick or treat!";
public static final String THANK_YOU_STRANGE_UNCLE = "Thank you, strange uncle!";
public static final String BOMB = "bomb";
public String trickOrTreat(int nbChildren, List<List<String>> packets) {
if (nbChildren > packets.size()) {
return TRICK_OR_TREAT;
}
try {
areValid(packets);
} catch (TrickOrTreatException e) {
return TRICK_OR_TREAT;
}
return THANK_YOU_STRANGE_UNCLE;
}
private void areValid(List<List<String>> packets) throws TrickOrTreatException {
int firstCandiesAmount = -1;
for (List<String> packet : packets) {
int candiesNumber = 0;
for (String item : packet) {
if (BOMB.equals(item)) {
throw new TrickOrTreatException();
} else if(CANDY.equals(item)) {
candiesNumber++;
}
}
if (candiesNumber < 2) {
throw new TrickOrTreatException();
}
if (firstCandiesAmount < 0) {
firstCandiesAmount = candiesNumber;
} else if (firstCandiesAmount != candiesNumber) {
throw new TrickOrTreatException();
}
}
}
}
package kata;
public class TrickOrTreatException extends Exception {
}
package kata;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.List;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
class HalloweenKataTest {
@Test
@DisplayName("Children should say 'Trick or treat!' when there are no packets")
void noPackets() {
assertThat(new HalloweenKata().trickOrTreat(2, emptyList())).isEqualTo("Trick or treat!");
}
@Test
@DisplayName("Children should say 'Trick or treat!' when there are no enough packets")
void noEnoughPackets() {
List<String> packet = asList("candy", "candy");
assertThat(new HalloweenKata().trickOrTreat(2, singletonList(packet))).isEqualTo("Trick or treat!");
}
@Test
@DisplayName("Children should say 'Trick or treat!' when there are enough packets but candies are insufficient")
void enoughPacketsInsufficientCandies() {
List<String> packet1 = asList("candy", "apple");
List<String> packet2 = asList("candy", "apple");
assertThat(new HalloweenKata().trickOrTreat(2, asList(packet1, packet2))).isEqualTo("Trick or treat!");
}
@Test
@DisplayName("Children should say 'Trick or treat!' when one of them was given a bomb")
void bomb() {
List<String> packet1 = asList("candy", "candy", "bomb");
List<String> packet2 = asList("candy", "candy");
assertThat(new HalloweenKata().trickOrTreat(2, asList(packet1, packet2))).isEqualTo("Trick or treat!");
}
@Test
@DisplayName("Children should say 'Thank you, strange uncle!' when there are enough candies")
void onlyCandiesEnoughCandies() {
List<String> packet1 = asList("candy", "candy");
List<String> packet2 = asList("candy", "candy");
assertThat(new HalloweenKata().trickOrTreat(2, asList(packet1, packet2))).isEqualTo("Thank you, strange uncle!");
}
@Test
@DisplayName("Children should say 'Thank you, strange uncle!' when enough packets, no bomb and same amount of candies")
void enoughCandiesNoBomb() {
List<String> packet1 = asList("candy", "candy", "apple");
List<String> packet2 = asList("candy", "candy");
assertThat(new HalloweenKata().trickOrTreat(2, asList(packet1, packet2))).isEqualTo("Thank you, strange uncle!");
}
@Test
@DisplayName("Children should say 'Trick or treat!' when enough packets and not same amount of candies")
void enoughCandiesButNotSameAmount() {
List<String> packet1 = asList("candy", "candy", "candy");
List<String> packet2 = asList("candy", "candy", "apple");
assertThat(new HalloweenKata().trickOrTreat(2, asList(packet1, packet2))).isEqualTo("Trick or treat!");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment