diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/onepiece/HasNoDreamException.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/onepiece/HasNoDreamException.java new file mode 100644 index 000000000..4aba2c4cc --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/onepiece/HasNoDreamException.java @@ -0,0 +1,7 @@ +package com.codedifferently.lesson16.onepiece; + +public class HasNoDreamException extends Exception { + public HasNoDreamException(String message) { + super(message); + } +} diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/onepiece/Pirate.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/onepiece/Pirate.java new file mode 100644 index 000000000..791baca39 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/onepiece/Pirate.java @@ -0,0 +1,90 @@ +package com.codedifferently.lesson16.onepiece; + +import java.util.Random; + +public class Pirate { + public enum HakiType { + OBSERVATION, + ARMANENT, + CONQUERORS, + ADVANCED_OBSERVATION, + ADVANCED_ARMANENT, + ADVANCED_CONQUERORS, + WILL_OF_D; + } + + private String name; + private String crew; + private Long bounty; + private String role; + private Boolean hasDream; + private HakiType powers; + + private final HakiType[] haki = HakiType.values(); + private static final Random cflip = new Random(); + + public Pirate(String name, String crew, Long bounty, String role, Boolean hasDream) { + this.name = name; + this.crew = crew; + this.bounty = bounty; + this.role = role; + this.hasDream = true; + this.powers = HakiType.WILL_OF_D; + } + + // Getters and setters + public String getName() { + return name; + } + + public String getCrew() { + return crew; + } + + public long getBounty() { + return bounty; + } + + public String getRole() { + return role; + } + + public boolean getHasDream() throws HasNoDreamException { + if (!hasDream) { + throw new HasNoDreamException(name + " has no dream!"); + } + System.out.println("Has Dream"); + return true; + } + + public HakiType getPowers() { + return powers; + } + + public void rollPowers() { + int randomIndex = cflip.nextInt(haki.length); + HakiType newHaki = haki[randomIndex]; + powers = newHaki; + System.out.println("Random Haki: " + powers); + } + + public void setName(String name) { + this.name = name; + } + + public void setCrew(String crew) { + this.crew = crew; + } + + public void setBounty(Long bounty) { + this.bounty = bounty; + } + + public void setRole(String role) { + this.role = role; + } + + public void setHasDream(boolean hasDream) { + this.hasDream = hasDream; + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/onepiece/PirateTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/onepiece/PirateTest.java new file mode 100644 index 000000000..5eae379c6 --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/onepiece/PirateTest.java @@ -0,0 +1,127 @@ +package com.codedifferently.lesson16.onepiece; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.codedifferently.lesson16.onepiece.Pirate.HakiType; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class PirateTest { + + Pirate pirate; + + @BeforeEach + void setUp() { + pirate = new Pirate("Luffy", "StrawHatPirates", 3000000000L, "Captain", true); + } + + @Test + void testGetName() { + String actual = pirate.getName(); + + assertThat(actual).isEqualTo("Luffy"); + } + + @Test + void testSetName() { + pirate.setName("BlackBeard"); + String actual = pirate.getName(); + + assertThat(actual).isEqualTo("BlackBeard"); + } + + @Test + void testGetCrew() { + String actual = pirate.getCrew(); + + assertThat(actual).isEqualTo("StrawHatPirates"); + } + + @Test + void testSetCrew() { + pirate.setCrew("BlackBeardPirates"); + String actual = pirate.getCrew(); + + assertThat(actual).isEqualTo("BlackBeardPirates"); + } + + @Test + void testGetBounty() { + Long actual = pirate.getBounty(); + + assertThat(actual).isEqualTo(3000000000L); + } + + @Test + void testSetBounty() { + pirate.setBounty(3996000000L); + Long actual = pirate.getBounty(); + + assertThat(actual).isEqualTo(3996000000L); + } + + @Test + void testGetRole() { + String actual = pirate.getRole(); + + assertThat(actual).isEqualTo("Captain"); + } + + @Test + void testSetRole() { + pirate.setRole("Captain"); + String actual = pirate.getRole(); + + assertThat(actual).isEqualTo("Captain"); + } + + @Test + void testgetHasDream() { + try { + pirate.getHasDream(); + } catch (HasNoDreamException e) { + System.out.println(e.getMessage()); + } + } + + @Test + void testRollPowers() { + pirate.rollPowers(); + assertNotNull(pirate.getPowers(), "Powers should not be null after rolling"); + boolean isValid = false; + for (HakiType h : HakiType.values()) { + if (pirate.getPowers() == h) { + isValid = true; + break; + } + } + assertTrue(isValid, "Powers should be a valid HakiType"); + } + + @Test + void testRollPowersOutput() { + + // Capture console output + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + + pirate.rollPowers(); + + String output = outputStream.toString().trim(); + boolean matches = false; + for (HakiType h : HakiType.values()) { + if (output.equals("Random Haki: " + h)) { + matches = true; + break; + } + } + + assertTrue(matches, "Haki Options"); + + System.setOut(System.out); + } +}