Skip to content

feat: adds assassin class, assassin test class and no targets excepti… #513

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

Closed
wants to merge 1 commit into from
Closed
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,116 @@
package com.codedifferently.lesson16.assassin;

import java.util.ArrayList;

public class Assassin {
private String type;
private String weaponType;
private int numberofTargets;
private ArrayList<String> weapons;
private double paymentforHire;
private Rank rank;

public Assassin(
String type, String weaponType, int numberofTargets, Rank rank, int paymentforHire) {
this.type = type;
this.weaponType = weaponType;
this.numberofTargets = numberofTargets;
this.weapons = new ArrayList<>();
this.paymentforHire = paymentforHire;
this.rank = rank;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getWeaponType() {
return weaponType;
}

public void setWeaponType(String weaponType) {
this.weaponType = weaponType;
}

public int getNumberofTargets() {
return numberofTargets;
}

public void setNumberofTargets(int numberofTargets) {
this.numberofTargets = numberofTargets;
}

public ArrayList<String> getWeapons() {
return weapons;
}

public void setWeapons(ArrayList<String> weapons) {
this.weapons = weapons;
}

public double getPaymentforHire() {
return paymentforHire;
}

public void setPaymentforHire(double paymentforHire) {
this.paymentforHire = paymentforHire;
}

public Rank getRank() {
return rank;
}

public void setRank(Rank rank) {
this.rank = rank;
}

public enum Rank {
NOVICE,
ADEPT,
MASTER,
GRANDMASTER,
LEGEND
}

public void executeTarget(int numberofTargets) throws NoTargetsException {
if (numberofTargets < 0) {
throw new NoTargetsException("Number of targets cannot be negative.");
}
if (numberofTargets == 0) {
System.out.println("No targets to eliminate!");
}
for (int i = 0; i < numberofTargets; i++) {
System.out.println("Target " + i + " eliminated!");
}
}

public String goStealth() {
return type + " activates stealth mode!";
}

public void rankUp() {
switch (rank) {
case NOVICE -> rank = Rank.ADEPT;
case ADEPT -> rank = Rank.MASTER;
case MASTER -> rank = Rank.GRANDMASTER;
case GRANDMASTER -> rank = Rank.LEGEND;
case LEGEND -> {
System.out.println(type + " is already at the highest rank: Legend.");
return;
}
}
System.out.println(type + " has been promoted to " + rank + " rank!");
}

public String showWeapons() {
String result = "Weapons in arsenal:";
for (String weapon : weapons) {
result += "- " + weapon;
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.codedifferently.lesson16.assassin;

public class NoTargetsException extends Exception {
public NoTargetsException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.codedifferently.lesson16.assassintest;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is in the wrong package and folder. Should be in the same package as the class under test. please fix.


import static org.assertj.core.api.Assertions.assertThat;

import com.codedifferently.lesson16.assassin.Assassin;
import com.codedifferently.lesson16.assassin.Assassin.Rank;
import java.util.ArrayList;
import org.junit.jupiter.api.Test;

public class AssassinTest {
@Test
public void testGetRank() {
Assassin assassin = new Assassin("Ninja", "Sword", 7, Assassin.Rank.MASTER, 700000);
Rank expected = Rank.MASTER;
assertThat(assassin.getRank()).isEqualTo(expected);
}

@Test
void testSetWeaponType() {
Assassin assassin = new Assassin("Marksman", "Sword", 7, Assassin.Rank.MASTER, 700000);
assassin.setWeaponType("Crossbow");
String expected = "Crossbow";
assertThat(assassin.getWeaponType()).isEqualTo(expected);
}

@Test
void testSetPaymentforHire() {
Assassin assassin = new Assassin("Ninja", "Sword", 7, Assassin.Rank.MASTER, 700000);
assassin.setPaymentforHire(800000);
double expected = 800000;
assertThat(assassin.getPaymentforHire()).isEqualTo(expected);
}

@Test
public void testSetType() {
Assassin assassin = new Assassin("Ninja", "Sword", 7, Assassin.Rank.MASTER, 700000);
assassin.setType("Sniper");
String expected = "Sniper";
assertThat(assassin.getType()).isEqualTo(expected);
}

@Test
public void testGetPaymentForHire() {
Assassin assassin = new Assassin("Ninja", "Sword", 7, Assassin.Rank.MASTER, 700000);
double expected = 700000;
assertThat(assassin.getPaymentforHire()).isEqualTo(expected);
}

@Test
public void testGetWeaponType() {
Assassin assassin = new Assassin("Ninja", "Ninja star", 7, Assassin.Rank.MASTER, 700000);
String expected = "Ninja star";
assertThat(assassin.getWeaponType()).isEqualTo(expected);
}

@Test
public void testGetType() {
Assassin assassin = new Assassin("Sniper", "Sniper", 7, Assassin.Rank.MASTER, 700000);
String expected = "Sniper";
assertThat(assassin.getType()).isEqualTo(expected);
}

@Test
public void testGetNumberOfTargets() {
Assassin assassin = new Assassin("Sniper", "Sniper", 7, Assassin.Rank.MASTER, 700000);
int expected = 7;
assertThat(assassin.getNumberofTargets()).isEqualTo(expected);
}

@Test
public void testExecuteTarget() throws Exception {
Assassin assassin = new Assassin("Hitman", "Pistol", 2, Assassin.Rank.ADEPT, 600000);
assassin.setNumberofTargets(2);
assassin.executeTarget(2);
assertThat(assassin.getNumberofTargets()).isEqualTo(2);
}

@Test
public void testGoStealth() {
Assassin assassin = new Assassin("Ninja", "Sword", 7, Assassin.Rank.LEGEND, 1000000);
String expected = "Ninja activates stealth mode!";
assertThat(assassin.goStealth()).isEqualTo(expected);
}

@Test
public void testRankUp() {
Assassin assassin = new Assassin("Hitman", "Pistol", 3, Assassin.Rank.NOVICE, 500000);
assassin.rankUp();
Rank expected = Rank.ADEPT;
assertThat(assassin.getRank()).isEqualTo(expected);
}

@Test
public void testShowWeapons() {
Assassin assassin = new Assassin("Hitman", "Pistol", 3, Assassin.Rank.NOVICE, 500000);
assassin.setWeapons(new ArrayList<>());
assassin.getWeapons().add("Pistol");
assassin.getWeapons().add("Dagger");
assassin.getWeapons().add("Sniper");
String expected = "Weapons in arsenal:- Pistol- Dagger- Sniper";
assertThat(assassin.showWeapons()).isEqualTo(expected);
}
}