Skip to content

Commit e5fdacf

Browse files
author
Meiko-S22
committed
feat: adds assassin class, assassin test class and no targets exception class
1 parent dbd5296 commit e5fdacf

File tree

3 files changed

+226
-0
lines changed

3 files changed

+226
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.codedifferently.lesson16.assassin;
2+
3+
import java.util.ArrayList;
4+
5+
public class Assassin {
6+
private String type;
7+
private String weaponType;
8+
private int numberofTargets;
9+
private ArrayList<String> weapons;
10+
private double paymentforHire;
11+
private Rank rank;
12+
13+
public Assassin(
14+
String type, String weaponType, int numberofTargets, Rank rank, int paymentforHire) {
15+
this.type = type;
16+
this.weaponType = weaponType;
17+
this.numberofTargets = numberofTargets;
18+
this.weapons = new ArrayList<>();
19+
this.paymentforHire = paymentforHire;
20+
this.rank = rank;
21+
}
22+
23+
public String getType() {
24+
return type;
25+
}
26+
27+
public void setType(String type) {
28+
this.type = type;
29+
}
30+
31+
public String getWeaponType() {
32+
return weaponType;
33+
}
34+
35+
public void setWeaponType(String weaponType) {
36+
this.weaponType = weaponType;
37+
}
38+
39+
public int getNumberofTargets() {
40+
return numberofTargets;
41+
}
42+
43+
public void setNumberofTargets(int numberofTargets) {
44+
this.numberofTargets = numberofTargets;
45+
}
46+
47+
public ArrayList<String> getWeapons() {
48+
return weapons;
49+
}
50+
51+
public void setWeapons(ArrayList<String> weapons) {
52+
this.weapons = weapons;
53+
}
54+
55+
public double getPaymentforHire() {
56+
return paymentforHire;
57+
}
58+
59+
public void setPaymentforHire(double paymentforHire) {
60+
this.paymentforHire = paymentforHire;
61+
}
62+
63+
public Rank getRank() {
64+
return rank;
65+
}
66+
67+
public void setRank(Rank rank) {
68+
this.rank = rank;
69+
}
70+
71+
public enum Rank {
72+
NOVICE,
73+
ADEPT,
74+
MASTER,
75+
GRANDMASTER,
76+
LEGEND
77+
}
78+
79+
public void executeTarget(int numberofTargets) throws NoTargetsException {
80+
if (numberofTargets < 0) {
81+
throw new NoTargetsException("Number of targets cannot be negative.");
82+
}
83+
if (numberofTargets == 0) {
84+
System.out.println("No targets to eliminate!");
85+
}
86+
for (int i = 0; i < numberofTargets; i++) {
87+
System.out.println("Target " + i + " eliminated!");
88+
}
89+
}
90+
91+
public String goStealth() {
92+
return type + " activates stealth mode!";
93+
}
94+
95+
public void rankUp() {
96+
switch (rank) {
97+
case NOVICE -> rank = Rank.ADEPT;
98+
case ADEPT -> rank = Rank.MASTER;
99+
case MASTER -> rank = Rank.GRANDMASTER;
100+
case GRANDMASTER -> rank = Rank.LEGEND;
101+
case LEGEND -> {
102+
System.out.println(type + " is already at the highest rank: Legend.");
103+
return;
104+
}
105+
}
106+
System.out.println(type + " has been promoted to " + rank + " rank!");
107+
}
108+
109+
public String showWeapons() {
110+
String result = "Weapons in arsenal:";
111+
for (String weapon : weapons) {
112+
result += "- " + weapon;
113+
}
114+
return result;
115+
}
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.assassin;
2+
3+
public class NoTargetsException extends Exception {
4+
public NoTargetsException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.codedifferently.lesson16.assassintest;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import com.codedifferently.lesson16.assassin.Assassin;
6+
import com.codedifferently.lesson16.assassin.Assassin.Rank;
7+
import java.util.ArrayList;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class AssassinTest {
11+
@Test
12+
public void testGetRank() {
13+
Assassin assassin = new Assassin("Ninja", "Sword", 7, Assassin.Rank.MASTER, 700000);
14+
Rank expected = Rank.MASTER;
15+
assertThat(assassin.getRank()).isEqualTo(expected);
16+
}
17+
18+
@Test
19+
void testSetWeaponType() {
20+
Assassin assassin = new Assassin("Marksman", "Sword", 7, Assassin.Rank.MASTER, 700000);
21+
assassin.setWeaponType("Crossbow");
22+
String expected = "Crossbow";
23+
assertThat(assassin.getWeaponType()).isEqualTo(expected);
24+
}
25+
26+
@Test
27+
void testSetPaymentforHire() {
28+
Assassin assassin = new Assassin("Ninja", "Sword", 7, Assassin.Rank.MASTER, 700000);
29+
assassin.setPaymentforHire(800000);
30+
double expected = 800000;
31+
assertThat(assassin.getPaymentforHire()).isEqualTo(expected);
32+
}
33+
34+
@Test
35+
public void testSetType() {
36+
Assassin assassin = new Assassin("Ninja", "Sword", 7, Assassin.Rank.MASTER, 700000);
37+
assassin.setType("Sniper");
38+
String expected = "Sniper";
39+
assertThat(assassin.getType()).isEqualTo(expected);
40+
}
41+
42+
@Test
43+
public void testGetPaymentForHire() {
44+
Assassin assassin = new Assassin("Ninja", "Sword", 7, Assassin.Rank.MASTER, 700000);
45+
double expected = 700000;
46+
assertThat(assassin.getPaymentforHire()).isEqualTo(expected);
47+
}
48+
49+
@Test
50+
public void testGetWeaponType() {
51+
Assassin assassin = new Assassin("Ninja", "Ninja star", 7, Assassin.Rank.MASTER, 700000);
52+
String expected = "Ninja star";
53+
assertThat(assassin.getWeaponType()).isEqualTo(expected);
54+
}
55+
56+
@Test
57+
public void testGetType() {
58+
Assassin assassin = new Assassin("Sniper", "Sniper", 7, Assassin.Rank.MASTER, 700000);
59+
String expected = "Sniper";
60+
assertThat(assassin.getType()).isEqualTo(expected);
61+
}
62+
63+
@Test
64+
public void testGetNumberOfTargets() {
65+
Assassin assassin = new Assassin("Sniper", "Sniper", 7, Assassin.Rank.MASTER, 700000);
66+
int expected = 7;
67+
assertThat(assassin.getNumberofTargets()).isEqualTo(expected);
68+
}
69+
70+
@Test
71+
public void testExecuteTarget() throws Exception {
72+
Assassin assassin = new Assassin("Hitman", "Pistol", 2, Assassin.Rank.ADEPT, 600000);
73+
assassin.setNumberofTargets(2);
74+
assassin.executeTarget(2);
75+
assertThat(assassin.getNumberofTargets()).isEqualTo(2);
76+
}
77+
78+
@Test
79+
public void testGoStealth() {
80+
Assassin assassin = new Assassin("Ninja", "Sword", 7, Assassin.Rank.LEGEND, 1000000);
81+
String expected = "Ninja activates stealth mode!";
82+
assertThat(assassin.goStealth()).isEqualTo(expected);
83+
}
84+
85+
@Test
86+
public void testRankUp() {
87+
Assassin assassin = new Assassin("Hitman", "Pistol", 3, Assassin.Rank.NOVICE, 500000);
88+
assassin.rankUp();
89+
Rank expected = Rank.ADEPT;
90+
assertThat(assassin.getRank()).isEqualTo(expected);
91+
}
92+
93+
@Test
94+
public void testShowWeapons() {
95+
Assassin assassin = new Assassin("Hitman", "Pistol", 3, Assassin.Rank.NOVICE, 500000);
96+
assassin.setWeapons(new ArrayList<>());
97+
assassin.getWeapons().add("Pistol");
98+
assassin.getWeapons().add("Dagger");
99+
assassin.getWeapons().add("Sniper");
100+
String expected = "Weapons in arsenal:- Pistol- Dagger- Sniper";
101+
assertThat(assassin.showWeapons()).isEqualTo(expected);
102+
}
103+
}

0 commit comments

Comments
 (0)