Skip to content

Commit 7f2920a

Browse files
authored
feat: adds Davis' custom data types implementation (#518)
* feat:adds custom data types HW (incomplete) * feat:add oop principles HW (incomplete) * chore: fixed incomplete code as well made sure foramt was correct
1 parent e871365 commit 7f2920a

File tree

3 files changed

+224
-0
lines changed

3 files changed

+224
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.onepiece;
2+
3+
public class HasNoDreamException extends Exception {
4+
public HasNoDreamException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.codedifferently.lesson16.onepiece;
2+
3+
import java.util.Random;
4+
5+
public class Pirate {
6+
public enum HakiType {
7+
OBSERVATION,
8+
ARMANENT,
9+
CONQUERORS,
10+
ADVANCED_OBSERVATION,
11+
ADVANCED_ARMANENT,
12+
ADVANCED_CONQUERORS,
13+
WILL_OF_D;
14+
}
15+
16+
private String name;
17+
private String crew;
18+
private Long bounty;
19+
private String role;
20+
private Boolean hasDream;
21+
private HakiType powers;
22+
23+
private final HakiType[] haki = HakiType.values();
24+
private static final Random cflip = new Random();
25+
26+
public Pirate(String name, String crew, Long bounty, String role, Boolean hasDream) {
27+
this.name = name;
28+
this.crew = crew;
29+
this.bounty = bounty;
30+
this.role = role;
31+
this.hasDream = true;
32+
this.powers = HakiType.WILL_OF_D;
33+
}
34+
35+
// Getters and setters
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public String getCrew() {
41+
return crew;
42+
}
43+
44+
public long getBounty() {
45+
return bounty;
46+
}
47+
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;
58+
}
59+
60+
public HakiType getPowers() {
61+
return powers;
62+
}
63+
64+
public void rollPowers() {
65+
int randomIndex = cflip.nextInt(haki.length);
66+
HakiType newHaki = haki[randomIndex];
67+
powers = newHaki;
68+
System.out.println("Random Haki: " + powers);
69+
}
70+
71+
public void setName(String name) {
72+
this.name = name;
73+
}
74+
75+
public void setCrew(String crew) {
76+
this.crew = crew;
77+
}
78+
79+
public void setBounty(Long bounty) {
80+
this.bounty = bounty;
81+
}
82+
83+
public void setRole(String role) {
84+
this.role = role;
85+
}
86+
87+
public void setHasDream(boolean hasDream) {
88+
this.hasDream = hasDream;
89+
}
90+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.codedifferently.lesson16.onepiece;
2+
3+
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;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
public class PirateTest {
14+
15+
Pirate pirate;
16+
17+
@BeforeEach
18+
void setUp() {
19+
pirate = new Pirate("Luffy", "StrawHatPirates", 3000000000L, "Captain", true);
20+
}
21+
22+
@Test
23+
void testGetName() {
24+
String actual = pirate.getName();
25+
26+
assertThat(actual).isEqualTo("Luffy");
27+
}
28+
29+
@Test
30+
void testSetName() {
31+
pirate.setName("BlackBeard");
32+
String actual = pirate.getName();
33+
34+
assertThat(actual).isEqualTo("BlackBeard");
35+
}
36+
37+
@Test
38+
void testGetCrew() {
39+
String actual = pirate.getCrew();
40+
41+
assertThat(actual).isEqualTo("StrawHatPirates");
42+
}
43+
44+
@Test
45+
void testSetCrew() {
46+
pirate.setCrew("BlackBeardPirates");
47+
String actual = pirate.getCrew();
48+
49+
assertThat(actual).isEqualTo("BlackBeardPirates");
50+
}
51+
52+
@Test
53+
void testGetBounty() {
54+
Long actual = pirate.getBounty();
55+
56+
assertThat(actual).isEqualTo(3000000000L);
57+
}
58+
59+
@Test
60+
void testSetBounty() {
61+
pirate.setBounty(3996000000L);
62+
Long actual = pirate.getBounty();
63+
64+
assertThat(actual).isEqualTo(3996000000L);
65+
}
66+
67+
@Test
68+
void testGetRole() {
69+
String actual = pirate.getRole();
70+
71+
assertThat(actual).isEqualTo("Captain");
72+
}
73+
74+
@Test
75+
void testSetRole() {
76+
pirate.setRole("Captain");
77+
String actual = pirate.getRole();
78+
79+
assertThat(actual).isEqualTo("Captain");
80+
}
81+
82+
@Test
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");
124+
125+
System.setOut(System.out);
126+
}
127+
}

0 commit comments

Comments
 (0)