-
Notifications
You must be signed in to change notification settings - Fork 23
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
..._16/objects/objects_app/src/main/java/com/codedifferently/lesson16/assassin/Assassin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...s/objects_app/src/main/java/com/codedifferently/lesson16/assassin/NoTargetsException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
...cts/objects_app/src/test/java/com/codedifferently/lesson16/assassintest/AssassinTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.codedifferently.lesson16.assassintest; | ||
|
||
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.