Skip to content

Commit 39f3b03

Browse files
committed
chore: fixed incomplete code as well made sure foramt was correct
1 parent 2fb00a4 commit 39f3b03

File tree

3 files changed

+107
-60
lines changed

3 files changed

+107
-60
lines changed

lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/onepiece/NotPirateException.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/onepiece/Pirate.java

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,32 @@
44

55
public class Pirate {
66
public enum HakiType {
7-
Observation,
8-
Armament,
9-
Conquerors,
10-
Advanced_Observation,
11-
Advanced_Armament,
12-
Advanced_Conquerors,
13-
Will_of_D;
7+
OBSERVATION,
8+
ARMANENT,
9+
CONQUERORS,
10+
ADVANCED_OBSERVATION,
11+
ADVANCED_ARMANENT,
12+
ADVANCED_CONQUERORS,
13+
WILL_OF_D;
1414
}
1515

1616
private String name;
1717
private String crew;
18-
private long bounty;
19-
private boolean hasDream;
20-
private boolean isPirate;
18+
private Long bounty;
19+
private String role;
20+
private Boolean hasDream;
2121
private HakiType powers;
22+
2223
private final HakiType[] haki = HakiType.values();
2324
private static final Random cflip = new Random();
2425

25-
public Pirate(String name, String crew, long bounty) {
26+
public Pirate(String name, String crew, Long bounty, String role, Boolean hasDream) {
2627
this.name = name;
2728
this.crew = crew;
2829
this.bounty = bounty;
30+
this.role = role;
2931
this.hasDream = true;
30-
this.isPirate = true;
31-
this.powers = HakiType.Will_of_D;
32+
this.powers = HakiType.WILL_OF_D;
3233
}
3334

3435
// Getters and setters
@@ -44,12 +45,20 @@ public long getBounty() {
4445
return bounty;
4546
}
4647

47-
public boolean getHasDream() {
48-
return hasDream;
48+
public String getRole() {
49+
return role;
50+
}
51+
52+
public boolean getHasDream() throws HasNoDreamException {
53+
if (!hasDream) {
54+
throw new HasNoDreamException(name + " has no dream!");
55+
}
56+
System.out.println("Has Dream");
57+
return true;
4958
}
5059

51-
public boolean getIsPirate() {
52-
return isPirate;
60+
public HakiType getPowers() {
61+
return powers;
5362
}
5463

5564
public void rollPowers() {
@@ -71,11 +80,11 @@ public void setBounty(Long bounty) {
7180
this.bounty = bounty;
7281
}
7382

74-
public void setHasDream(boolean hasDream) {
75-
this.hasDream = hasDream;
83+
public void setRole(String role) {
84+
this.role = role;
7685
}
7786

78-
public void setIsPirate(boolean isPirate) {
79-
this.isPirate = isPirate;
87+
public void setHasDream(boolean hasDream) {
88+
this.hasDream = hasDream;
8089
}
8190
}
Lines changed: 77 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package com.codedifferently.lesson16.onepiece;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import com.codedifferently.lesson16.onepiece.Pirate.HakiType;
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.PrintStream;
410
import org.junit.jupiter.api.BeforeEach;
511
import org.junit.jupiter.api.Test;
612

@@ -10,73 +16,112 @@ public class PirateTest {
1016

1117
@BeforeEach
1218
void setUp() {
13-
pirate = new Pirate(2, "Zoro", "Swordsman", 900000);
19+
pirate = new Pirate("Luffy", "StrawHatPirates", 3000000000L, "Captain", true);
1420
}
1521

1622
@Test
17-
void testGetId() {
18-
int actual = pirate.getId();
23+
void testGetName() {
24+
String actual = pirate.getName();
1925

20-
assertThat(actual).isEqualTo(2);
26+
assertThat(actual).isEqualTo("Luffy");
2127
}
2228

2329
@Test
24-
void testSetId() {
25-
pirate.setId(3);
26-
int actual = pirate.getId();
30+
void testSetName() {
31+
pirate.setName("BlackBeard");
32+
String actual = pirate.getName();
2733

28-
assertThat(actual).isEqualTo(3);
34+
assertThat(actual).isEqualTo("BlackBeard");
2935
}
3036

3137
@Test
32-
void testGetName() {
33-
String actual = pirate.getName();
38+
void testGetCrew() {
39+
String actual = pirate.getCrew();
3440

35-
assertThat(actual).isEqualTo("Zoro");
41+
assertThat(actual).isEqualTo("StrawHatPirates");
3642
}
3743

3844
@Test
39-
void testSetName() {
40-
pirate.setName("Sanji");
41-
String actual = pirate.getName();
45+
void testSetCrew() {
46+
pirate.setCrew("BlackBeardPirates");
47+
String actual = pirate.getCrew();
4248

43-
assertThat(actual).isEqualTo("Sanji");
49+
assertThat(actual).isEqualTo("BlackBeardPirates");
4450
}
4551

4652
@Test
47-
void testGetDepartment() {
48-
String actual = pirate.getDepartment();
53+
void testGetBounty() {
54+
Long actual = pirate.getBounty();
4955

50-
assertThat(actual).isEqualTo("Swordsman");
56+
assertThat(actual).isEqualTo(3000000000L);
5157
}
5258

5359
@Test
54-
void testSetDepartment() {
55-
pirate.setDepartment("Cook");
56-
String actual = pirate.getDepartment();
60+
void testSetBounty() {
61+
pirate.setBounty(3996000000L);
62+
Long actual = pirate.getBounty();
5763

58-
assertThat(actual).isEqualTo("Cook");
64+
assertThat(actual).isEqualTo(3996000000L);
5965
}
6066

6167
@Test
62-
void testGetSalary() {
63-
double actual = pirate.getSalary();
68+
void testGetRole() {
69+
String actual = pirate.getRole();
6470

65-
assertThat(actual).isEqualTo(900000);
71+
assertThat(actual).isEqualTo("Captain");
6672
}
6773

6874
@Test
69-
void testSetSalary() {
70-
pirate.setSalary(850000);
71-
double actual = pirate.getSalary();
75+
void testSetRole() {
76+
pirate.setRole("Captain");
77+
String actual = pirate.getRole();
7278

73-
assertThat(actual).isEqualTo(850000);
79+
assertThat(actual).isEqualTo("Captain");
7480
}
7581

7682
@Test
77-
void testGetDetails() {
78-
String actual = pirate.getDetails();
83+
void testgetHasDream() {
84+
try {
85+
pirate.getHasDream();
86+
} catch (HasNoDreamException e) {
87+
System.out.println(e.getMessage());
88+
}
89+
}
90+
91+
@Test
92+
void testRollPowers() {
93+
pirate.rollPowers();
94+
assertNotNull(pirate.getPowers(), "Powers should not be null after rolling");
95+
boolean isValid = false;
96+
for (HakiType h : HakiType.values()) {
97+
if (pirate.getPowers() == h) {
98+
isValid = true;
99+
break;
100+
}
101+
}
102+
assertTrue(isValid, "Powers should be a valid HakiType");
103+
}
104+
105+
@Test
106+
void testRollPowersOutput() {
107+
108+
// Capture console output
109+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
110+
System.setOut(new PrintStream(outputStream));
111+
112+
pirate.rollPowers();
113+
114+
String output = outputStream.toString().trim();
115+
boolean matches = false;
116+
for (HakiType h : HakiType.values()) {
117+
if (output.equals("Random Haki: " + h)) {
118+
matches = true;
119+
break;
120+
}
121+
}
122+
123+
assertTrue(matches, "Haki Options");
79124

80-
assertThat(actual).isEqualTo("2 Zoro Swordsman 900000.0");
125+
System.setOut(System.out);
81126
}
82127
}

0 commit comments

Comments
 (0)