Skip to content

feat:add oop principles HW (incomplete) #518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.codedifferently.lesson16.onepiece;

public class HasNoDreamException extends Exception {
public HasNoDreamException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.codedifferently.lesson16.onepiece;

import java.util.Random;

public class Pirate {
public enum HakiType {
OBSERVATION,
ARMANENT,
CONQUERORS,
ADVANCED_OBSERVATION,
ADVANCED_ARMANENT,
ADVANCED_CONQUERORS,
WILL_OF_D;
}

private String name;
private String crew;
private Long bounty;
private String role;
private Boolean hasDream;
private HakiType powers;

private final HakiType[] haki = HakiType.values();
private static final Random cflip = new Random();

public Pirate(String name, String crew, Long bounty, String role, Boolean hasDream) {
this.name = name;
this.crew = crew;
this.bounty = bounty;
this.role = role;
this.hasDream = true;
this.powers = HakiType.WILL_OF_D;
}

// Getters and setters
public String getName() {
return name;
}

public String getCrew() {
return crew;
}

public long getBounty() {
return bounty;
}

public String getRole() {
return role;
}

public boolean getHasDream() throws HasNoDreamException {
if (!hasDream) {
throw new HasNoDreamException(name + " has no dream!");
}
System.out.println("Has Dream");
return true;
}

public HakiType getPowers() {
return powers;
}

public void rollPowers() {
int randomIndex = cflip.nextInt(haki.length);
HakiType newHaki = haki[randomIndex];
powers = newHaki;
System.out.println("Random Haki: " + powers);
}

public void setName(String name) {
this.name = name;
}

public void setCrew(String crew) {
this.crew = crew;
}

public void setBounty(Long bounty) {
this.bounty = bounty;
}

public void setRole(String role) {
this.role = role;
}

public void setHasDream(boolean hasDream) {
this.hasDream = hasDream;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.codedifferently.lesson16.onepiece;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.codedifferently.lesson16.onepiece.Pirate.HakiType;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class PirateTest {

Pirate pirate;

@BeforeEach
void setUp() {
pirate = new Pirate("Luffy", "StrawHatPirates", 3000000000L, "Captain", true);
}

@Test
void testGetName() {
String actual = pirate.getName();

assertThat(actual).isEqualTo("Luffy");
}

@Test
void testSetName() {
pirate.setName("BlackBeard");
String actual = pirate.getName();

assertThat(actual).isEqualTo("BlackBeard");
}

@Test
void testGetCrew() {
String actual = pirate.getCrew();

assertThat(actual).isEqualTo("StrawHatPirates");
}

@Test
void testSetCrew() {
pirate.setCrew("BlackBeardPirates");
String actual = pirate.getCrew();

assertThat(actual).isEqualTo("BlackBeardPirates");
}

@Test
void testGetBounty() {
Long actual = pirate.getBounty();

assertThat(actual).isEqualTo(3000000000L);
}

@Test
void testSetBounty() {
pirate.setBounty(3996000000L);
Long actual = pirate.getBounty();

assertThat(actual).isEqualTo(3996000000L);
}

@Test
void testGetRole() {
String actual = pirate.getRole();

assertThat(actual).isEqualTo("Captain");
}

@Test
void testSetRole() {
pirate.setRole("Captain");
String actual = pirate.getRole();

assertThat(actual).isEqualTo("Captain");
}

@Test
void testgetHasDream() {
try {
pirate.getHasDream();
} catch (HasNoDreamException e) {
System.out.println(e.getMessage());
}
}

@Test
void testRollPowers() {
pirate.rollPowers();
assertNotNull(pirate.getPowers(), "Powers should not be null after rolling");
boolean isValid = false;
for (HakiType h : HakiType.values()) {
if (pirate.getPowers() == h) {
isValid = true;
break;
}
}
assertTrue(isValid, "Powers should be a valid HakiType");
}

@Test
void testRollPowersOutput() {

// Capture console output
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
System.setOut(new PrintStream(outputStream));

pirate.rollPowers();

String output = outputStream.toString().trim();
boolean matches = false;
for (HakiType h : HakiType.values()) {
if (output.equals("Random Haki: " + h)) {
matches = true;
break;
}
}

assertTrue(matches, "Haki Options");

System.setOut(System.out);
}
}