Skip to content

Commit 0d8ce16

Browse files
authored
Feat: added Boxer.java and BoxerTest.java to create and test a class object. lesson_16 - JosephCaballero (#532)
* feat: added Boxer.java to create a real world object and BoxerTest.java to test it inside of a created folder boxer for lesson_16 - JosephCaballero * feat: modified tests more in BoxerTest.java and modified code in Boxer.java. lesson_16 -JosephCaballero * fix: adds spotlessApply. lesson_16 -JosephCaballero * chore:fixing basic spacing to ensure usability. lesson_16 - JosephCaballero * chore: applied spotlessApply and adadjusted tests. lesson_16 -JosephCaballero * fix: adds an enum to check aginst another enum. lesson_16 -JosephCaballero
1 parent 70f0765 commit 0d8ce16

File tree

2 files changed

+303
-0
lines changed
  • lesson_16/objects/objects_app/src

2 files changed

+303
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package com.codedifferently.lesson16.boxer;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Random;
6+
7+
public class Boxer {
8+
public enum BoxerStyle {
9+
SWITCHHITTER,
10+
BOXERPUNCHER,
11+
SOUTHPAW,
12+
INFIGHTER,
13+
SLUGGER,
14+
OUTFIGHTER,
15+
ORTHODOX;
16+
}
17+
18+
public class BoxerIsRetiredException extends Exception {
19+
public BoxerIsRetiredException(String message) {
20+
super(message);
21+
}
22+
}
23+
24+
public class BoxerHasNoFightsException extends Exception {
25+
public BoxerHasNoFightsException(String message) {
26+
super(message);
27+
}
28+
}
29+
30+
private String name;
31+
private HashMap<String, Character> fights;
32+
private int power;
33+
private int health;
34+
private BoxerStyle skillSet;
35+
private boolean ableToFight;
36+
private BoxerStyle[] style = BoxerStyle.values();
37+
private static Random rand = new Random();
38+
39+
public Boxer(String name, int power, int health) {
40+
this.name = name;
41+
this.power = power;
42+
this.health = health;
43+
this.ableToFight = true;
44+
this.skillSet = BoxerStyle.ORTHODOX;
45+
fights = new HashMap<>();
46+
}
47+
48+
public String getName() {
49+
return name;
50+
}
51+
52+
public boolean getAbleToFight() {
53+
return ableToFight;
54+
}
55+
56+
public void workout(int additional) {
57+
power += additional;
58+
}
59+
60+
public int getPower() {
61+
return power;
62+
}
63+
64+
public int getHealth() {
65+
return health;
66+
}
67+
68+
public void rest() {
69+
health += 1;
70+
}
71+
72+
public void rollSkillSet() {
73+
int randomIndex = rand.nextInt(style.length);
74+
BoxerStyle newStyle = style[randomIndex];
75+
skillSet = newStyle;
76+
System.out.println("Random style: " + skillSet);
77+
}
78+
79+
public void addFights(String name, char d) throws BoxerIsRetiredException {
80+
if (ableToFight == false) {
81+
throw new BoxerIsRetiredException("Boxer is retired");
82+
}
83+
fights.put(name, d);
84+
}
85+
86+
public void retire() throws BoxerIsRetiredException {
87+
checkRetirementStatus();
88+
ableToFight = false;
89+
}
90+
91+
private void checkRetirementStatus() throws BoxerIsRetiredException {
92+
if (ableToFight == false) {
93+
throw new BoxerIsRetiredException("Boxer is already retired");
94+
}
95+
}
96+
97+
private void checkBoxerHasntFoughtAnyone() throws BoxerHasNoFightsException {
98+
if (fights.isEmpty()) {
99+
throw new BoxerHasNoFightsException("Boxer hasnt fought anyone");
100+
}
101+
}
102+
103+
public String getFights() throws BoxerHasNoFightsException {
104+
checkBoxerHasntFoughtAnyone();
105+
String fightsHad = "";
106+
for (Map.Entry<String, Character> entry : fights.entrySet()) {
107+
String key = entry.getKey();
108+
char value = entry.getValue();
109+
fightsHad += "fought: " + key + " " + value + ".";
110+
}
111+
return fightsHad;
112+
}
113+
114+
public void bout(Boxer boxer) throws BoxerIsRetiredException {
115+
if (ableToFight == false) {
116+
throw new BoxerIsRetiredException("Boxer is retired");
117+
}
118+
String boxerName = boxer.getName();
119+
if (power < boxer.getPower()) {
120+
ableToFight = false;
121+
fights.put(boxerName, 'L');
122+
} else if (power == boxer.getPower()) {
123+
fights.put(boxerName, 'D');
124+
} else {
125+
fights.put(boxerName, 'W');
126+
}
127+
}
128+
129+
public void setName(String name) {
130+
this.name = name;
131+
}
132+
133+
public void setHealth(int health) {
134+
this.health = health;
135+
}
136+
137+
public BoxerStyle getSkillSet() {
138+
return skillSet;
139+
}
140+
141+
public void setAbleToFight(boolean ableToFight) {
142+
this.ableToFight = ableToFight;
143+
}
144+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package com.codedifferently.lesson16.boxer;
2+
3+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
7+
8+
import com.codedifferently.lesson16.boxer.Boxer.BoxerHasNoFightsException;
9+
import com.codedifferently.lesson16.boxer.Boxer.BoxerIsRetiredException;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
public class BoxerTest {
14+
public enum testStyle {
15+
BASIC
16+
}
17+
18+
Boxer boxer;
19+
20+
@BeforeEach
21+
void setUp() {
22+
boxer = new Boxer("Joseph", 99, 99);
23+
}
24+
25+
@Test
26+
void testWorkout_addingTenPointsOfPower() {
27+
boxer.workout(10);
28+
assertEquals(109, boxer.getPower());
29+
}
30+
31+
@Test
32+
void testRest_restingToIncreaseHealth() {
33+
int previousHealth = boxer.getHealth();
34+
boxer.rest();
35+
assertNotEquals(boxer.getHealth(), previousHealth);
36+
}
37+
38+
@Test
39+
void testGetFightHistory_makingAFighterToHaveABout()
40+
throws BoxerHasNoFightsException, BoxerIsRetiredException {
41+
Boxer mike = new Boxer("mike", 1000, 1000);
42+
boxer.bout(mike);
43+
assertEquals("fought: " + mike.getName() + " L.", boxer.getFights());
44+
}
45+
46+
@Test
47+
void testIsAbleToFight_boxerIsNotRetiredOrInjured() {
48+
assertEquals(true, boxer.getAbleToFight());
49+
}
50+
51+
@Test
52+
void testIsAbleToFight_boxerIsNotAbleToFight() throws BoxerIsRetiredException {
53+
boxer.retire();
54+
assertEquals(false, boxer.getAbleToFight());
55+
}
56+
57+
@Test
58+
void testBout_makingNewBoxerToFightAnotherAndSeeTheResults() throws BoxerIsRetiredException {
59+
Boxer mike = new Boxer("Mike", 1000, 1000);
60+
boxer.bout(mike);
61+
assertEquals(false, boxer.getAbleToFight());
62+
}
63+
64+
@Test
65+
void testBout_makingNewBoxerToFightAndDraw()
66+
throws BoxerIsRetiredException, BoxerHasNoFightsException {
67+
Boxer ryan = new Boxer("Ryan Garcia", 99, 99);
68+
boxer.bout(ryan);
69+
assertEquals(boxer.getFights(), "fought: Ryan Garcia D.");
70+
}
71+
72+
@Test
73+
void testBout_makingNewBoxerToWinAgainstWithBoxer()
74+
throws BoxerIsRetiredException, BoxerHasNoFightsException {
75+
Boxer hitman = new Boxer("Thomas Hearns", 98, 98);
76+
boxer.bout(hitman);
77+
assertEquals(boxer.getFights(), "fought: " + hitman.getName() + " W.");
78+
}
79+
80+
@Test
81+
void testGetHealth() {
82+
assertEquals(boxer.getHealth(), 99);
83+
}
84+
85+
@Test
86+
void testRollSkillSet_testingBasicIsNotWhatIsRolled() {
87+
boxer.rollSkillSet();
88+
assertNotEquals(boxer.getSkillSet(), testStyle.BASIC);
89+
}
90+
91+
@Test
92+
void testAddFights() throws BoxerIsRetiredException, BoxerHasNoFightsException {
93+
boxer.addFights("Marvin Hagler", 'L');
94+
assertEquals(boxer.getFights(), "fought: Marvin Hagler L.");
95+
}
96+
97+
@Test
98+
void testAddFights_whenRetired() throws BoxerIsRetiredException {
99+
boxer.retire();
100+
assertThatThrownBy(
101+
() -> {
102+
boxer.addFights("lomachenko", 'W');
103+
})
104+
.isInstanceOf(BoxerIsRetiredException.class)
105+
.hasMessage("Boxer is retired");
106+
}
107+
108+
@Test
109+
void testRetire_whenRetired() throws BoxerIsRetiredException {
110+
boxer.retire();
111+
assertThatThrownBy(
112+
() -> {
113+
boxer.retire();
114+
})
115+
.isInstanceOf(BoxerIsRetiredException.class)
116+
.hasMessage("Boxer is already retired");
117+
}
118+
119+
@Test
120+
void testSetName() {
121+
boxer.setName("Pablo");
122+
assertEquals(boxer.getName(), "Pablo");
123+
}
124+
125+
@Test
126+
void testSetAbleToFight() {
127+
boxer.setAbleToFight(false);
128+
assertFalse(boxer.getAbleToFight());
129+
}
130+
131+
@Test
132+
void testSetHealth() {
133+
boxer.setHealth(2);
134+
assertEquals(boxer.getHealth(), 2);
135+
}
136+
137+
@Test
138+
void testBoxerIsRetiredException_retiringBoxerThenThrowingException()
139+
throws BoxerIsRetiredException {
140+
Boxer leaonard = new Boxer("Sugar ray", 200, 200);
141+
boxer.retire();
142+
assertThatThrownBy(
143+
() -> {
144+
boxer.bout(leaonard);
145+
})
146+
.isInstanceOf(BoxerIsRetiredException.class)
147+
.hasMessageContaining("Boxer is retired");
148+
}
149+
150+
@Test
151+
void testBoxerHasNoFightsException_gettingFightsWhenNoFightsHaveBeenHad() {
152+
assertThatThrownBy(
153+
() -> {
154+
boxer.getFights();
155+
})
156+
.isInstanceOf(BoxerHasNoFightsException.class)
157+
.hasMessageContaining("Boxer hasnt fought anyone");
158+
}
159+
}

0 commit comments

Comments
 (0)