diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/boxer/Boxer.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/boxer/Boxer.java new file mode 100644 index 000000000..e71b18b0a --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/boxer/Boxer.java @@ -0,0 +1,144 @@ +package com.codedifferently.lesson16.boxer; + +import java.util.HashMap; +import java.util.Map; +import java.util.Random; + +public class Boxer { + public enum BoxerStyle { + SWITCHHITTER, + BOXERPUNCHER, + SOUTHPAW, + INFIGHTER, + SLUGGER, + OUTFIGHTER, + ORTHODOX; + } + + public class BoxerIsRetiredException extends Exception { + public BoxerIsRetiredException(String message) { + super(message); + } + } + + public class BoxerHasNoFightsException extends Exception { + public BoxerHasNoFightsException(String message) { + super(message); + } + } + + private String name; + private HashMap fights; + private int power; + private int health; + private BoxerStyle skillSet; + private boolean ableToFight; + private BoxerStyle[] style = BoxerStyle.values(); + private static Random rand = new Random(); + + public Boxer(String name, int power, int health) { + this.name = name; + this.power = power; + this.health = health; + this.ableToFight = true; + this.skillSet = BoxerStyle.ORTHODOX; + fights = new HashMap<>(); + } + + public String getName() { + return name; + } + + public boolean getAbleToFight() { + return ableToFight; + } + + public void workout(int additional) { + power += additional; + } + + public int getPower() { + return power; + } + + public int getHealth() { + return health; + } + + public void rest() { + health += 1; + } + + public void rollSkillSet() { + int randomIndex = rand.nextInt(style.length); + BoxerStyle newStyle = style[randomIndex]; + skillSet = newStyle; + System.out.println("Random style: " + skillSet); + } + + public void addFights(String name, char d) throws BoxerIsRetiredException { + if (ableToFight == false) { + throw new BoxerIsRetiredException("Boxer is retired"); + } + fights.put(name, d); + } + + public void retire() throws BoxerIsRetiredException { + checkRetirementStatus(); + ableToFight = false; + } + + private void checkRetirementStatus() throws BoxerIsRetiredException { + if (ableToFight == false) { + throw new BoxerIsRetiredException("Boxer is already retired"); + } + } + + private void checkBoxerHasntFoughtAnyone() throws BoxerHasNoFightsException { + if (fights.isEmpty()) { + throw new BoxerHasNoFightsException("Boxer hasnt fought anyone"); + } + } + + public String getFights() throws BoxerHasNoFightsException { + checkBoxerHasntFoughtAnyone(); + String fightsHad = ""; + for (Map.Entry entry : fights.entrySet()) { + String key = entry.getKey(); + char value = entry.getValue(); + fightsHad += "fought: " + key + " " + value + "."; + } + return fightsHad; + } + + public void bout(Boxer boxer) throws BoxerIsRetiredException { + if (ableToFight == false) { + throw new BoxerIsRetiredException("Boxer is retired"); + } + String boxerName = boxer.getName(); + if (power < boxer.getPower()) { + ableToFight = false; + fights.put(boxerName, 'L'); + } else if (power == boxer.getPower()) { + fights.put(boxerName, 'D'); + } else { + fights.put(boxerName, 'W'); + } + } + + public void setName(String name) { + this.name = name; + } + + public void setHealth(int health) { + this.health = health; + } + + public BoxerStyle getSkillSet() { + return skillSet; + } + + public void setAbleToFight(boolean ableToFight) { + this.ableToFight = ableToFight; + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/boxer/BoxerTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/boxer/BoxerTest.java new file mode 100644 index 000000000..2fe3b169e --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/boxer/BoxerTest.java @@ -0,0 +1,159 @@ +package com.codedifferently.lesson16.boxer; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import com.codedifferently.lesson16.boxer.Boxer.BoxerHasNoFightsException; +import com.codedifferently.lesson16.boxer.Boxer.BoxerIsRetiredException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class BoxerTest { + public enum testStyle { + BASIC + } + + Boxer boxer; + + @BeforeEach + void setUp() { + boxer = new Boxer("Joseph", 99, 99); + } + + @Test + void testWorkout_addingTenPointsOfPower() { + boxer.workout(10); + assertEquals(109, boxer.getPower()); + } + + @Test + void testRest_restingToIncreaseHealth() { + int previousHealth = boxer.getHealth(); + boxer.rest(); + assertNotEquals(boxer.getHealth(), previousHealth); + } + + @Test + void testGetFightHistory_makingAFighterToHaveABout() + throws BoxerHasNoFightsException, BoxerIsRetiredException { + Boxer mike = new Boxer("mike", 1000, 1000); + boxer.bout(mike); + assertEquals("fought: " + mike.getName() + " L.", boxer.getFights()); + } + + @Test + void testIsAbleToFight_boxerIsNotRetiredOrInjured() { + assertEquals(true, boxer.getAbleToFight()); + } + + @Test + void testIsAbleToFight_boxerIsNotAbleToFight() throws BoxerIsRetiredException { + boxer.retire(); + assertEquals(false, boxer.getAbleToFight()); + } + + @Test + void testBout_makingNewBoxerToFightAnotherAndSeeTheResults() throws BoxerIsRetiredException { + Boxer mike = new Boxer("Mike", 1000, 1000); + boxer.bout(mike); + assertEquals(false, boxer.getAbleToFight()); + } + + @Test + void testBout_makingNewBoxerToFightAndDraw() + throws BoxerIsRetiredException, BoxerHasNoFightsException { + Boxer ryan = new Boxer("Ryan Garcia", 99, 99); + boxer.bout(ryan); + assertEquals(boxer.getFights(), "fought: Ryan Garcia D."); + } + + @Test + void testBout_makingNewBoxerToWinAgainstWithBoxer() + throws BoxerIsRetiredException, BoxerHasNoFightsException { + Boxer hitman = new Boxer("Thomas Hearns", 98, 98); + boxer.bout(hitman); + assertEquals(boxer.getFights(), "fought: " + hitman.getName() + " W."); + } + + @Test + void testGetHealth() { + assertEquals(boxer.getHealth(), 99); + } + + @Test + void testRollSkillSet_testingBasicIsNotWhatIsRolled() { + boxer.rollSkillSet(); + assertNotEquals(boxer.getSkillSet(), testStyle.BASIC); + } + + @Test + void testAddFights() throws BoxerIsRetiredException, BoxerHasNoFightsException { + boxer.addFights("Marvin Hagler", 'L'); + assertEquals(boxer.getFights(), "fought: Marvin Hagler L."); + } + + @Test + void testAddFights_whenRetired() throws BoxerIsRetiredException { + boxer.retire(); + assertThatThrownBy( + () -> { + boxer.addFights("lomachenko", 'W'); + }) + .isInstanceOf(BoxerIsRetiredException.class) + .hasMessage("Boxer is retired"); + } + + @Test + void testRetire_whenRetired() throws BoxerIsRetiredException { + boxer.retire(); + assertThatThrownBy( + () -> { + boxer.retire(); + }) + .isInstanceOf(BoxerIsRetiredException.class) + .hasMessage("Boxer is already retired"); + } + + @Test + void testSetName() { + boxer.setName("Pablo"); + assertEquals(boxer.getName(), "Pablo"); + } + + @Test + void testSetAbleToFight() { + boxer.setAbleToFight(false); + assertFalse(boxer.getAbleToFight()); + } + + @Test + void testSetHealth() { + boxer.setHealth(2); + assertEquals(boxer.getHealth(), 2); + } + + @Test + void testBoxerIsRetiredException_retiringBoxerThenThrowingException() + throws BoxerIsRetiredException { + Boxer leaonard = new Boxer("Sugar ray", 200, 200); + boxer.retire(); + assertThatThrownBy( + () -> { + boxer.bout(leaonard); + }) + .isInstanceOf(BoxerIsRetiredException.class) + .hasMessageContaining("Boxer is retired"); + } + + @Test + void testBoxerHasNoFightsException_gettingFightsWhenNoFightsHaveBeenHad() { + assertThatThrownBy( + () -> { + boxer.getFights(); + }) + .isInstanceOf(BoxerHasNoFightsException.class) + .hasMessageContaining("Boxer hasnt fought anyone"); + } +}