Skip to content

Commit 0632a13

Browse files
authored
Feat: adds Pablo Limon-Paredes Saiyan Object and Test Lesson16 (#533)
* Feat: adds Pablo Limon-Paredes Saiyan Object Lesson16 * Feat: runs Spotlessapply * Feat: adds custom exception and updates both test and regular file
1 parent cb5442f commit 0632a13

File tree

3 files changed

+278
-0
lines changed

3 files changed

+278
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.saiyanoop;
2+
3+
public class InvalidPowerLevelCustomExcepetion extends Exception {
4+
public InvalidPowerLevelCustomExcepetion(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.codedifferently.lesson16.saiyanoop;
2+
3+
import java.util.ArrayList;
4+
5+
public class Saiyan {
6+
enum SaiyanForms {
7+
BASE, // 0 - 1999
8+
SSJ1, // 2000 - 2999
9+
SSJ2, // 3000 - 3999
10+
SSJ3 // 4000
11+
}
12+
13+
private int powerLevel;
14+
private String name;
15+
private ArrayList<String> accessories;
16+
private SaiyanForms saiyanForms;
17+
private boolean hasATail;
18+
19+
public Saiyan(
20+
int powerLevel,
21+
String name,
22+
ArrayList<String> accessories,
23+
SaiyanForms saiyanForms,
24+
boolean hasATail) {
25+
this.powerLevel = powerLevel;
26+
this.name = name;
27+
this.accessories = accessories;
28+
this.saiyanForms = saiyanForms;
29+
this.hasATail = hasATail;
30+
}
31+
32+
public int getPowerLevel() {
33+
return powerLevel;
34+
}
35+
36+
public void setPowerLevel(int powerLevel) throws InvalidPowerLevelCustomExcepetion {
37+
this.powerLevel = powerLevel;
38+
if (powerLevel <= 0) {
39+
throw new InvalidPowerLevelCustomExcepetion("Power Level can not be zero or less!");
40+
}
41+
42+
SaiyanForms form = SaiyanForms.BASE;
43+
if (powerLevel > 2000 && powerLevel < 2999) {
44+
form = SaiyanForms.SSJ1;
45+
} else if (powerLevel > 3000 && powerLevel < 3999) {
46+
form = SaiyanForms.SSJ2;
47+
} else if (powerLevel > 4000) {
48+
form = SaiyanForms.SSJ3;
49+
}
50+
setSaiyanForms(form);
51+
}
52+
53+
public String getName() {
54+
return name;
55+
}
56+
57+
public void setName(String name) {
58+
this.name = name;
59+
}
60+
61+
public ArrayList<String> getAccessories() {
62+
return accessories;
63+
}
64+
65+
public void addAccessories(String addingAcc) {
66+
this.accessories.add(addingAcc);
67+
}
68+
69+
public void removeAccessories(String removeAcc) {
70+
accessories.remove(removeAcc);
71+
}
72+
73+
public void removeAllAccessories() {
74+
accessories.removeAll(accessories);
75+
for (int i = accessories.size() - 1; i >= 0; i--) {
76+
accessories.remove(i);
77+
}
78+
}
79+
80+
public SaiyanForms getSaiyanForms() {
81+
return saiyanForms;
82+
}
83+
84+
public void setSaiyanForms(SaiyanForms saiyanForms) {
85+
this.saiyanForms = saiyanForms;
86+
}
87+
88+
public boolean isHasATail() {
89+
return hasATail;
90+
}
91+
92+
public void setHasATail(boolean hasATail) {
93+
this.hasATail = hasATail;
94+
}
95+
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package com.codedifferently.lesson16.saiyanoop;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.fail;
5+
6+
import com.codedifferently.lesson16.saiyanoop.Saiyan.SaiyanForms;
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class SaiyanTest {
12+
@Test
13+
void testgetPowerLevel() {
14+
Saiyan saiyan =
15+
new Saiyan(
16+
1200,
17+
"Goku",
18+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
19+
SaiyanForms.BASE,
20+
false);
21+
assertEquals(1200, saiyan.getPowerLevel());
22+
}
23+
24+
@Test
25+
void testpowerLevelDroppingAndFormDown() throws InvalidPowerLevelCustomExcepetion {
26+
Saiyan saiyan =
27+
new Saiyan(
28+
4200,
29+
"Goku",
30+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
31+
SaiyanForms.SSJ3,
32+
false);
33+
saiyan.setPowerLevel(3300);
34+
assertEquals(SaiyanForms.SSJ2, saiyan.getSaiyanForms());
35+
saiyan.setPowerLevel(2300);
36+
assertEquals(SaiyanForms.SSJ1, saiyan.getSaiyanForms());
37+
saiyan.setPowerLevel(1300);
38+
assertEquals(SaiyanForms.BASE, saiyan.getSaiyanForms());
39+
}
40+
41+
@Test
42+
void testpowerLevelRisingAndFormUp() throws InvalidPowerLevelCustomExcepetion {
43+
Saiyan saiyan =
44+
new Saiyan(
45+
1200,
46+
"Goku",
47+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
48+
SaiyanForms.BASE,
49+
false);
50+
saiyan.setPowerLevel(2600);
51+
assertEquals(SaiyanForms.SSJ1, saiyan.getSaiyanForms());
52+
saiyan.setPowerLevel(3600);
53+
assertEquals(SaiyanForms.SSJ2, saiyan.getSaiyanForms());
54+
saiyan.setPowerLevel(4600);
55+
assertEquals(SaiyanForms.SSJ3, saiyan.getSaiyanForms());
56+
}
57+
58+
@Test
59+
void testdoesHaveATail() {
60+
Saiyan saiyan =
61+
new Saiyan(
62+
1200,
63+
"Goku",
64+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
65+
SaiyanForms.BASE,
66+
false);
67+
saiyan.setHasATail(true);
68+
assertEquals(true, saiyan.isHasATail());
69+
}
70+
71+
@Test
72+
void testdoesNotHaveATail() {
73+
Saiyan saiyan =
74+
new Saiyan(
75+
1200,
76+
"Goku",
77+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
78+
SaiyanForms.BASE,
79+
false);
80+
assertEquals(false, saiyan.isHasATail());
81+
}
82+
83+
@Test
84+
void testgetName() {
85+
Saiyan saiyan =
86+
new Saiyan(
87+
1200,
88+
"Goku",
89+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
90+
SaiyanForms.BASE,
91+
false);
92+
assertEquals("Goku", saiyan.getName());
93+
}
94+
95+
@Test
96+
void testsetName() {
97+
Saiyan saiyan =
98+
new Saiyan(
99+
1200,
100+
"Goku",
101+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
102+
SaiyanForms.BASE,
103+
false);
104+
saiyan.setName("Kakarot");
105+
assertEquals("Kakarot", saiyan.getName());
106+
}
107+
108+
@Test
109+
void testgetAnAccessory() {
110+
Saiyan saiyan =
111+
new Saiyan(
112+
1200,
113+
"Goku",
114+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
115+
SaiyanForms.BASE,
116+
false);
117+
assertEquals("Power Pole", saiyan.getAccessories().get(0));
118+
}
119+
120+
@Test
121+
void testaddingAnAccessory() {
122+
Saiyan saiyan =
123+
new Saiyan(
124+
1200,
125+
"Goku",
126+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
127+
SaiyanForms.BASE,
128+
false);
129+
saiyan.addAccessories("Senzu Bean");
130+
assertEquals(4, saiyan.getAccessories().size());
131+
}
132+
133+
@Test
134+
void testremovingAllAccessories() {
135+
Saiyan saiyan =
136+
new Saiyan(
137+
1200,
138+
"Goku",
139+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
140+
SaiyanForms.BASE,
141+
false);
142+
saiyan.removeAllAccessories();
143+
assertEquals(0, saiyan.getAccessories().size());
144+
}
145+
146+
@Test
147+
void testremoveAccessories() {
148+
Saiyan saiyan =
149+
new Saiyan(
150+
1200,
151+
"Goku",
152+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
153+
SaiyanForms.BASE,
154+
false);
155+
saiyan.removeAccessories("Power Pole");
156+
assertEquals(2, saiyan.getAccessories().size());
157+
}
158+
159+
@Test
160+
void testcanNotGetZeroOrNegativePowerLevel() throws InvalidPowerLevelCustomExcepetion {
161+
Saiyan saiyan =
162+
new Saiyan(
163+
1200,
164+
"Goku",
165+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
166+
SaiyanForms.BASE,
167+
false);
168+
169+
try {
170+
saiyan.setPowerLevel(-10);
171+
fail("Expected InvalidPowerLevelCustomException to be thrown");
172+
} catch (InvalidPowerLevelCustomExcepetion e) {
173+
assertEquals("Power Level can not be zero or less!", e.getMessage());
174+
}
175+
}
176+
}

0 commit comments

Comments
 (0)