Skip to content

Commit ad7b0b7

Browse files
committed
Feat: adds custom exception and updates both test and regular file
1 parent 24a8108 commit ad7b0b7

File tree

3 files changed

+90
-14
lines changed

3 files changed

+90
-14
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+
}

lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/saiyanoop/Saiyan.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ public int getPowerLevel() {
3333
return powerLevel;
3434
}
3535

36-
public void setPowerLevel(int powerLevel) {
36+
public void setPowerLevel(int powerLevel) throws InvalidPowerLevelCustomExcepetion {
3737
this.powerLevel = powerLevel;
38+
if (powerLevel <= 0) {
39+
throw new InvalidPowerLevelCustomExcepetion("Power Level can not be zero or less!");
40+
}
41+
3842
SaiyanForms form = SaiyanForms.BASE;
3943
if (powerLevel > 2000 && powerLevel < 2999) {
4044
form = SaiyanForms.SSJ1;
@@ -68,6 +72,9 @@ public void removeAccessories(String removeAcc) {
6872

6973
public void removeAllAccessories() {
7074
accessories.removeAll(accessories);
75+
for (int i = accessories.size() - 1; i >= 0; i--) {
76+
accessories.remove(i);
77+
}
7178
}
7279

7380
public SaiyanForms getSaiyanForms() {

lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/saiyanoop/SaiyanTest.java

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.codedifferently.lesson16.saiyanoop;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.fail;
45

56
import com.codedifferently.lesson16.saiyanoop.Saiyan.SaiyanForms;
67
import java.util.ArrayList;
@@ -14,20 +15,19 @@ void testgetPowerLevel() {
1415
new Saiyan(
1516
1200,
1617
"Goku",
17-
new ArrayList<String>(Arrays.asList("WristBands, Boots, Orange Gi, Blue Belt")),
18+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
1819
SaiyanForms.BASE,
1920
false);
2021
assertEquals(1200, saiyan.getPowerLevel());
2122
}
2223

2324
@Test
24-
void testpowerLevelDroppingAndFormDown() {
25+
void testpowerLevelDroppingAndFormDown() throws InvalidPowerLevelCustomExcepetion {
2526
Saiyan saiyan =
2627
new Saiyan(
2728
4200,
2829
"Goku",
29-
new ArrayList<String>(
30-
Arrays.asList("WristBands, Boots, Orange Gi, Blue Belt, Blue Undershirt")),
30+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
3131
SaiyanForms.SSJ3,
3232
false);
3333
saiyan.setPowerLevel(3300);
@@ -39,13 +39,12 @@ void testpowerLevelDroppingAndFormDown() {
3939
}
4040

4141
@Test
42-
void testpowerLevelRisingAndFormUp() {
42+
void testpowerLevelRisingAndFormUp() throws InvalidPowerLevelCustomExcepetion {
4343
Saiyan saiyan =
4444
new Saiyan(
4545
1200,
4646
"Goku",
47-
new ArrayList<String>(
48-
Arrays.asList("WristBands, Boots, Orange Gi, Blue Belt, Blue Undershirt")),
47+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
4948
SaiyanForms.BASE,
5049
false);
5150
saiyan.setPowerLevel(2600);
@@ -62,7 +61,7 @@ void testdoesHaveATail() {
6261
new Saiyan(
6362
1200,
6463
"Goku",
65-
new ArrayList<String>(Arrays.asList("WristBands, Boots, Orange Gi, Blue Belt")),
64+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
6665
SaiyanForms.BASE,
6766
false);
6867
saiyan.setHasATail(true);
@@ -75,7 +74,7 @@ void testdoesNotHaveATail() {
7574
new Saiyan(
7675
1200,
7776
"Goku",
78-
new ArrayList<String>(Arrays.asList("WristBands, Boots, Orange Gi, Blue Belt")),
77+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
7978
SaiyanForms.BASE,
8079
false);
8180
assertEquals(false, saiyan.isHasATail());
@@ -87,7 +86,7 @@ void testgetName() {
8786
new Saiyan(
8887
1200,
8988
"Goku",
90-
new ArrayList<String>(Arrays.asList("WristBands, Boots, Orange Gi, Blue Belt")),
89+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
9190
SaiyanForms.BASE,
9291
false);
9392
assertEquals("Goku", saiyan.getName());
@@ -99,16 +98,79 @@ void testsetName() {
9998
new Saiyan(
10099
1200,
101100
"Goku",
102-
new ArrayList<String>(Arrays.asList("WristBands, Boots, Orange Gi, Blue Belt")),
101+
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
103102
SaiyanForms.BASE,
104103
false);
105104
saiyan.setName("Kakarot");
106105
assertEquals("Kakarot", saiyan.getName());
107106
}
108107

109108
@Test
110-
void testwhatAccessoriesTheyHaveOn() {}
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+
}
111145

112146
@Test
113-
void 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+
}
114176
}

0 commit comments

Comments
 (0)