Skip to content

Feat: adds Pablo Limon-Paredes Saiyan Object and Test Lesson16 #533

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

Merged
merged 4 commits into from
Nov 19, 2024
Merged
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.saiyanoop;

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

import java.util.ArrayList;

public class Saiyan {
enum SaiyanForms {
BASE, // 0 - 1999
SSJ1, // 2000 - 2999
SSJ2, // 3000 - 3999
SSJ3 // 4000
}

private int powerLevel;
private String name;
private ArrayList<String> accessories;
private SaiyanForms saiyanForms;
private boolean hasATail;

public Saiyan(
int powerLevel,
String name,
ArrayList<String> accessories,
SaiyanForms saiyanForms,
boolean hasATail) {
this.powerLevel = powerLevel;
this.name = name;
this.accessories = accessories;
this.saiyanForms = saiyanForms;
this.hasATail = hasATail;
}

public int getPowerLevel() {
return powerLevel;
}

public void setPowerLevel(int powerLevel) throws InvalidPowerLevelCustomExcepetion {
this.powerLevel = powerLevel;
if (powerLevel <= 0) {
throw new InvalidPowerLevelCustomExcepetion("Power Level can not be zero or less!");
}

SaiyanForms form = SaiyanForms.BASE;
if (powerLevel > 2000 && powerLevel < 2999) {
form = SaiyanForms.SSJ1;
} else if (powerLevel > 3000 && powerLevel < 3999) {
form = SaiyanForms.SSJ2;
} else if (powerLevel > 4000) {
form = SaiyanForms.SSJ3;
}
setSaiyanForms(form);
}

public String getName() {
return name;
}

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

public ArrayList<String> getAccessories() {
return accessories;
}

public void addAccessories(String addingAcc) {
this.accessories.add(addingAcc);
}

public void removeAccessories(String removeAcc) {
accessories.remove(removeAcc);
}

public void removeAllAccessories() {
accessories.removeAll(accessories);
for (int i = accessories.size() - 1; i >= 0; i--) {
accessories.remove(i);
}
}

public SaiyanForms getSaiyanForms() {
return saiyanForms;
}

public void setSaiyanForms(SaiyanForms saiyanForms) {
this.saiyanForms = saiyanForms;
}

public boolean isHasATail() {
return hasATail;
}

public void setHasATail(boolean hasATail) {
this.hasATail = hasATail;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package com.codedifferently.lesson16.saiyanoop;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import com.codedifferently.lesson16.saiyanoop.Saiyan.SaiyanForms;
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.jupiter.api.Test;

public class SaiyanTest {
@Test
void testgetPowerLevel() {
Saiyan saiyan =
new Saiyan(
1200,
"Goku",
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
SaiyanForms.BASE,
false);
assertEquals(1200, saiyan.getPowerLevel());
}

@Test
void testpowerLevelDroppingAndFormDown() throws InvalidPowerLevelCustomExcepetion {
Saiyan saiyan =
new Saiyan(
4200,
"Goku",
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
SaiyanForms.SSJ3,
false);
saiyan.setPowerLevel(3300);
assertEquals(SaiyanForms.SSJ2, saiyan.getSaiyanForms());
saiyan.setPowerLevel(2300);
assertEquals(SaiyanForms.SSJ1, saiyan.getSaiyanForms());
saiyan.setPowerLevel(1300);
assertEquals(SaiyanForms.BASE, saiyan.getSaiyanForms());
}

@Test
void testpowerLevelRisingAndFormUp() throws InvalidPowerLevelCustomExcepetion {
Saiyan saiyan =
new Saiyan(
1200,
"Goku",
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
SaiyanForms.BASE,
false);
saiyan.setPowerLevel(2600);
assertEquals(SaiyanForms.SSJ1, saiyan.getSaiyanForms());
saiyan.setPowerLevel(3600);
assertEquals(SaiyanForms.SSJ2, saiyan.getSaiyanForms());
saiyan.setPowerLevel(4600);
assertEquals(SaiyanForms.SSJ3, saiyan.getSaiyanForms());
}

@Test
void testdoesHaveATail() {
Saiyan saiyan =
new Saiyan(
1200,
"Goku",
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
SaiyanForms.BASE,
false);
saiyan.setHasATail(true);
assertEquals(true, saiyan.isHasATail());
}

@Test
void testdoesNotHaveATail() {
Saiyan saiyan =
new Saiyan(
1200,
"Goku",
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
SaiyanForms.BASE,
false);
assertEquals(false, saiyan.isHasATail());
}

@Test
void testgetName() {
Saiyan saiyan =
new Saiyan(
1200,
"Goku",
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
SaiyanForms.BASE,
false);
assertEquals("Goku", saiyan.getName());
}

@Test
void testsetName() {
Saiyan saiyan =
new Saiyan(
1200,
"Goku",
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
SaiyanForms.BASE,
false);
saiyan.setName("Kakarot");
assertEquals("Kakarot", saiyan.getName());
}

@Test
void testgetAnAccessory() {
Saiyan saiyan =
new Saiyan(
1200,
"Goku",
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
SaiyanForms.BASE,
false);
assertEquals("Power Pole", saiyan.getAccessories().get(0));
}

@Test
void testaddingAnAccessory() {
Saiyan saiyan =
new Saiyan(
1200,
"Goku",
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
SaiyanForms.BASE,
false);
saiyan.addAccessories("Senzu Bean");
assertEquals(4, saiyan.getAccessories().size());
}

@Test
void testremovingAllAccessories() {
Saiyan saiyan =
new Saiyan(
1200,
"Goku",
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
SaiyanForms.BASE,
false);
saiyan.removeAllAccessories();
assertEquals(0, saiyan.getAccessories().size());
}

@Test
void testremoveAccessories() {
Saiyan saiyan =
new Saiyan(
1200,
"Goku",
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
SaiyanForms.BASE,
false);
saiyan.removeAccessories("Power Pole");
assertEquals(2, saiyan.getAccessories().size());
}

@Test
void testcanNotGetZeroOrNegativePowerLevel() throws InvalidPowerLevelCustomExcepetion {
Saiyan saiyan =
new Saiyan(
1200,
"Goku",
new ArrayList<String>(Arrays.asList("Power Pole", "Scouter", "Flying Nimbus")),
SaiyanForms.BASE,
false);

try {
saiyan.setPowerLevel(-10);
fail("Expected InvalidPowerLevelCustomException to be thrown");
} catch (InvalidPowerLevelCustomExcepetion e) {
assertEquals("Power Level can not be zero or less!", e.getMessage());
}
}
}