Skip to content

Commit fd647ea

Browse files
committed
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
1 parent 2bff2f9 commit fd647ea

File tree

2 files changed

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

2 files changed

+249
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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 HashMap<String, Character> getFightHistory() {
65+
return fights;
66+
}
67+
68+
public int getHealth() {
69+
return health;
70+
}
71+
72+
public void rest() {
73+
health += 1;
74+
}
75+
76+
public void rollSkillSet() {
77+
int randomIndex = rand.nextInt(style.length);
78+
BoxerStyle newStyle = style[randomIndex];
79+
skillSet = newStyle;
80+
System.out.println("Random style: " + skillSet);
81+
}
82+
83+
public void addFights(String name, char d) throws BoxerIsRetiredException {
84+
if (ableToFight == false) {
85+
throw new BoxerIsRetiredException("Boxer is retired");
86+
}
87+
fights.put(name, d);
88+
}
89+
90+
public void retire() throws BoxerIsRetiredException {
91+
checkRetirementStatus();
92+
ableToFight = false;
93+
}
94+
95+
private void checkRetirementStatus() throws BoxerIsRetiredException {
96+
if (ableToFight == false) {
97+
throw new BoxerIsRetiredException("Boxer is already retired");
98+
}
99+
}
100+
101+
private void checkBoxerHasntFoughtAnyone() throws BoxerHasNoFightsException {
102+
if (fights.isEmpty()) {
103+
throw new BoxerHasNoFightsException("Boxer hasnt fought anyone");
104+
}
105+
}
106+
107+
public String getFights() throws BoxerHasNoFightsException {
108+
checkBoxerHasntFoughtAnyone();
109+
String fightsHad = "";
110+
for (Map.Entry<String, Character> entry : fights.entrySet()) {
111+
String key = entry.getKey();
112+
char value = entry.getValue();
113+
fightsHad += "You fought " + key + " " + value;
114+
}
115+
return fightsHad;
116+
}
117+
118+
public void bout(Boxer boxer) throws BoxerIsRetiredException {
119+
if (ableToFight == false) {
120+
throw new BoxerIsRetiredException("Boxer is retired");
121+
}
122+
String boxerName = boxer.getName();
123+
if (power < boxer.getPower()) {
124+
ableToFight = false;
125+
fights.put(boxerName, 'L');
126+
} else if (power == boxer.getPower()) {
127+
fights.put(boxerName, 'D');
128+
} else {
129+
fights.put(boxerName, 'W');
130+
}
131+
}
132+
133+
public void setName(String name) {
134+
this.name = name;
135+
}
136+
137+
public void setFights(HashMap<String, Character> fights) {
138+
this.fights = fights;
139+
}
140+
141+
public void setPower(int power) {
142+
this.power = power;
143+
}
144+
145+
public void setHealth(int health) {
146+
this.health = health;
147+
}
148+
149+
public BoxerStyle getSkillSet() {
150+
return skillSet;
151+
}
152+
153+
public void setSkillSet(BoxerStyle skillSet) {
154+
this.skillSet = skillSet;
155+
}
156+
157+
public void setAbleToFight(boolean ableToFight) {
158+
this.ableToFight = ableToFight;
159+
}
160+
161+
public BoxerStyle[] getStyle() {
162+
return style;
163+
}
164+
165+
public void setStyle(BoxerStyle[] style) {
166+
this.style = style;
167+
}
168+
169+
public static Random getRand() {
170+
return rand;
171+
}
172+
173+
public static void setRand(Random rand) {
174+
Boxer.rand = rand;
175+
}
176+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.codedifferently.lesson16.boxer;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
5+
6+
import com.codedifferently.lesson16.boxer.Boxer.BoxerHasNoFightsException;
7+
import com.codedifferently.lesson16.boxer.Boxer.BoxerIsRetiredException;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class BoxerTest {
12+
13+
Boxer boxer;
14+
15+
@BeforeEach
16+
void setUp() {
17+
boxer = new Boxer("Joseph", 100, 100);
18+
}
19+
20+
@Test
21+
void testWorkout() {
22+
boxer.workout(10);
23+
assertEquals(110, boxer.getPower());
24+
}
25+
26+
@Test
27+
void testRest() {
28+
int previousHealth = boxer.getHealth();
29+
boxer.rest();
30+
assertNotEquals(boxer.getHealth(), previousHealth);
31+
}
32+
33+
@Test
34+
void testGetHealth() {
35+
assertEquals(100, boxer.getHealth());
36+
}
37+
38+
@Test
39+
void testGetFightHistory() throws BoxerHasNoFightsException, BoxerIsRetiredException {
40+
Boxer mike = new Boxer("mike", 1000, 1000);
41+
boxer.bout(mike);
42+
assertEquals("You fought " + mike.getName() + " L", boxer.getFights());
43+
}
44+
45+
@Test
46+
void testIsAbleToFight_boxerIsNotRetiredOrInjured() {
47+
assertEquals(true, boxer.getAbleToFight());
48+
}
49+
50+
@Test
51+
void testIsAbleToFight_boxerIsNotAbleToFight() throws BoxerIsRetiredException {
52+
boxer.retire();
53+
assertEquals(false, boxer.getAbleToFight());
54+
}
55+
56+
@Test
57+
void testBout_makingNewBoxerToFightAnother() throws BoxerIsRetiredException {
58+
Boxer mike = new Boxer("Mike", 1000, 1000);
59+
boxer.bout(mike);
60+
assertEquals(false, boxer.getAbleToFight());
61+
}
62+
63+
@Test
64+
void testGetFightHistory_noFights() {
65+
assertEquals(boxer.getHealth(), 100);
66+
}
67+
68+
@Test
69+
void testRollSkillSet() {
70+
boxer.rollSkillSet();
71+
assertNotEquals(boxer.getStyle(), "BASIC");
72+
}
73+
}

0 commit comments

Comments
 (0)