Skip to content

Feat: added Boxer.java and BoxerTest.java to create and test a class object. lesson_16 - JosephCaballero #532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 23, 2024
Merged
Original file line number Diff line number Diff line change
@@ -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<String, Character> 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<String, Character> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion in this test compares the SkillSet to the string "BASIC", but there are two issues:

  • The BoxerStyle enum does not have a BASIC value.
  • You’re comparing an enum to a string, which isn’t valid. If BASIC were an enum value, it should be compared as BoxerStyle.BASIC.

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");
}
}