diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..5d6b03c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..a2cccad --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,186 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1707577885030 + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..eebe54b --- /dev/null +++ b/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + org.example + lab-intro-to-testing-maven + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + junit + junit + RELEASE + compile + + + org.junit.jupiter + junit-jupiter + 5.8.1 + compile + + + + \ No newline at end of file diff --git a/src/main/java/org/example/Elf.java b/src/main/java/org/example/Elf.java new file mode 100644 index 0000000..db83657 --- /dev/null +++ b/src/main/java/org/example/Elf.java @@ -0,0 +1,18 @@ +package org.example; + +public class Elf extends Player { + private int speed; + + public Elf(int health, int strength, int lives, int speed) { + super(health, strength, lives); + this.speed = speed; + } + + public void setSpeed(int speed) { + this.speed = speed; + } + + public int getSpeed() { + return speed; + } +} diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java new file mode 100644 index 0000000..163145c --- /dev/null +++ b/src/main/java/org/example/Main.java @@ -0,0 +1,55 @@ +package org.example; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Main { + public static void main(String[] args) { + Elf elf = new Elf(30,8,3,15); + elf.decrementLive(); + System.out.println(elf.getLives()); + elf.decrementLive(); + System.out.println(elf.getLives()); + elf.decrementLive(); + System.out.println(elf.getLives()); + elf.decrementLive(); + System.out.println(elf.getLives()); + + } + + // method that takes in an integer n and returns an array of all odd integers from 1 to n. + public List buildArrayOddInts(int n) { + List arrayOddInts = new ArrayList(); + for (int i=0; i keywords = new ArrayList<>(); + keywords = Arrays.asList("abstract", "assert", "boolean", + "break", "byte", "case", "catch", "char", "class", "const", + "continue", "default", "do", "double", "else", "extends", "false", + "final", "finally", "float", "for", "goto", "if", "implements", + "import", "instanceof", "int", "interface", "long", "native", + "new", "null", "package", "private", "protected", "public", + "return", "short", "static", "strictfp", "super", "switch", + "synchronized", "this", "throw", "throws", "transient", "true", + "try", "void", "volatile", "while"); + + List wordsList = Arrays.stream(str.split(" ")).toList(); + for (String word : wordsList) { + for (String keyword : keywords) { + if (word.equals(keyword)) { + return true; + } + } + } + return false; + } +} \ No newline at end of file diff --git a/src/main/java/org/example/Player.java b/src/main/java/org/example/Player.java new file mode 100644 index 0000000..68fb483 --- /dev/null +++ b/src/main/java/org/example/Player.java @@ -0,0 +1,68 @@ +package org.example; + +public abstract class Player { + private int health; + private int strength; + private int lives; + private final int originalHealth; + + + // Constructor + public Player(int health, int strength, int lives) { + setHealth(health); + setStrength(strength); + setLives(lives); + originalHealth = health; + } + + // Setters + public void setHealth(int health) { + this.health = health; + } + + public void setStrength(int strength) { + this.strength = strength; + } + + public void setLives(int lives) { + this.lives = lives; + } + + // Getters + public int getHealth() { + return health; + } + + public int getStrength() { + return strength; + } + + public int getLives() { + return lives; + } + + // method that decrements the lives by 1 and restores the health to its original value. + // If lives are less than or equal to 0, print "This character is dead". + public void decrementLive() { + if (lives <= 0) { + System.out.println("This character is dead"); + } else { + setLives(lives-1); + setHealth(originalHealth); + } + } + + // method that decrements the health of the player passed as an argument by the strength of the current player. + public void attack(Player playerToAttack) { + playerToAttack.setHealth(playerToAttack.getHealth()-strength); + playerToAttack.checkHealth(); + } + + // method that checks if the current player's health is less than or equal to 0 + // if yes, calls decrementLive() method. + public void checkHealth() { + if (health <= 0) { + decrementLive(); + } + } +} \ No newline at end of file diff --git a/src/main/java/org/example/Warrior.java b/src/main/java/org/example/Warrior.java new file mode 100644 index 0000000..edc66c8 --- /dev/null +++ b/src/main/java/org/example/Warrior.java @@ -0,0 +1,24 @@ +package org.example; + +import java.util.Objects; + +public class Warrior extends Player { + private int force; + + public Warrior(int health, int strength, int lives, int force) { + super(health, strength, lives); + this.force = force; + } + + public void setForce(int force) { + this.force = force; + } + + public int getForce() { + return force; + } + + public Elf convertToElf() { + return new Elf(getHealth(), getStrength(), getLives(), getForce()); + } +} diff --git a/src/main/java/org/example/Wizard.java b/src/main/java/org/example/Wizard.java new file mode 100644 index 0000000..fb29f18 --- /dev/null +++ b/src/main/java/org/example/Wizard.java @@ -0,0 +1,22 @@ +package org.example; + +public class Wizard extends Player { + private int spell; + + public Wizard(int health, int strength, int lives, int spell) { + super(health, strength, lives); + this.spell = spell; + } + + public void setSpell(int spell) { + this.spell = spell; + } + + public int getSpell() { + return spell; + } + + public Elf convertToElf() { + return new Elf(getHealth(), getStrength(), getLives(), getSpell()); + } +} diff --git a/src/test/java/org/example/MainTest.java b/src/test/java/org/example/MainTest.java new file mode 100644 index 0000000..ceb87ff --- /dev/null +++ b/src/test/java/org/example/MainTest.java @@ -0,0 +1,54 @@ +package org.example; + +import org.junit.Before; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class MainTest { + + private final Main main = new Main(); + + @DisplayName("Test for n = 3") + @Test + void arrayOddInts_Testn3() { + assertEquals(Arrays.asList(1), main.buildArrayOddInts(3)); + } + @DisplayName("Test for n = 5") + @Test + void arrayOddInts_Testn5() { + assertEquals(Arrays.asList(1, 3), main.buildArrayOddInts(5)); + } + + @DisplayName("Test for n = 1") + @Test + void arrayOddInts_Testn1() { + assertEquals(Arrays.asList(), main.buildArrayOddInts(1)); + } + + @DisplayName("Test for n = -1") + @Test + void arrayOddInts_Testnegativen() { + assertEquals(Arrays.asList(), main.buildArrayOddInts(-1)); + } + + @DisplayName("Test for n = 6 (even num)") + @Test + void arrayOddInts_TestnEven() { + assertEquals(Arrays.asList(1,3,5), main.buildArrayOddInts(6)); + } + + @DisplayName("Detect keywords") + @Test + void detectingKeywords() { + assertEquals(false,main.detectKeywords("Hello my name is Laura")); + assertEquals(true,main.detectKeywords("Hello my name is Laura and I am short")); + assertEquals(true,main.detectKeywords("Don't break my heart")); + assertEquals(false,main.detectKeywords("I love to breakdance")); + } +} \ No newline at end of file diff --git a/src/test/java/org/example/PlayersTest.java b/src/test/java/org/example/PlayersTest.java new file mode 100644 index 0000000..3fda069 --- /dev/null +++ b/src/test/java/org/example/PlayersTest.java @@ -0,0 +1,107 @@ +package org.example; + +import org.junit.Before; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + + +public class PlayersTest { + + private Warrior warrior; + private Elf elf; + private Wizard wizard; + final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final PrintStream outContentClean = System.out; + + @BeforeEach + public void setUp() { + warrior = new Warrior(20,10,3,15); + elf = new Elf(30,8,3,15); + wizard = new Wizard(20,5,3,20); + System.setOut(new PrintStream(outContent)); + } + + @AfterEach + void outContentClean(){ + System.setOut(outContentClean); + } + + @DisplayName("Getters and setters") + @Test + void GettersSettersTests() { + assertEquals(20, warrior.getHealth()); + assertEquals(3, elf.getLives()); + assertEquals(20, wizard.getSpell()); + + warrior.setForce(17); + assertEquals(17, warrior.getForce()); + } + + @DisplayName("Converting to elf") + @Test + void Convert_to_Elf() { + assertEquals(Elf.class, warrior.convertToElf().getClass()); + assertEquals(15, warrior.convertToElf().getSpeed()); + assertEquals(Elf.class, wizard.convertToElf().getClass()); + assertEquals(20, wizard.convertToElf().getSpeed()); + } + + @DisplayName("Decrementing lives") + @Test + void Decrement_Lives_and_Dead_Players() { + assertEquals(3,elf.getLives()); + elf.decrementLive(); + assertEquals(2,elf.getLives()); + elf.decrementLive(); + assertEquals(1,elf.getLives()); + elf.decrementLive(); + assertEquals(0,elf.getLives()); + elf.decrementLive(); + assertEquals("This character is dead", outContent.toString().trim()); + assertEquals(0,elf.getLives()); + } + + @DisplayName("Attacking to decrement lives") + @Test + void Attacks_to_Decrement_Lives() { + // each time the warrior attacks the wizard, wizard's health is decremented by 10 (warrior's strength) + warrior.attack(wizard); + assertEquals(10, wizard.getHealth()); + // when the 2nd attack is done, the wizard's health is 20-10-10 = 0 + warrior.attack(wizard); + // as health is set to 0, it is restored to the initial value (20) + assertEquals(20, wizard.getHealth()); + // and lives are decremented by 1 (from 3 to 2) + assertEquals(2, wizard.getLives()); + } + + @DisplayName("Attacking till death") + @Test + void Attacks_till_Death() { + // for each 2 attacks from the warrior to the wizard, the wizard loses 1 live + // this means that in 6 attacks the wizard must have 0 lives and the original health points + for (int i=0; i < 6; i++) { + warrior.attack(wizard); + } + assertEquals(20, wizard.getHealth()); + assertEquals(0, wizard.getLives()); + + // with 2 more attacks, the wizard must die + for (int i=0; i < 2; i++) { + warrior.attack(wizard); + } + assertEquals(0, wizard.getHealth()); + assertEquals(0, wizard.getLives()); + assertEquals("This character is dead", outContent.toString().trim()); + } +} \ No newline at end of file