diff --git a/LABJavaIntroTotesting/.gitignore b/LABJavaIntroTotesting/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/LABJavaIntroTotesting/.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/LABJavaIntroTotesting/.idea/.gitignore b/LABJavaIntroTotesting/.idea/.gitignore new file mode 100644 index 0000000..a0ccf77 --- /dev/null +++ b/LABJavaIntroTotesting/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Environment-dependent path to Maven home directory +/mavenHomeManager.xml diff --git a/LABJavaIntroTotesting/.idea/encodings.xml b/LABJavaIntroTotesting/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/LABJavaIntroTotesting/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/LABJavaIntroTotesting/.idea/misc.xml b/LABJavaIntroTotesting/.idea/misc.xml new file mode 100644 index 0000000..df00c07 --- /dev/null +++ b/LABJavaIntroTotesting/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/LABJavaIntroTotesting/.idea/vcs.xml b/LABJavaIntroTotesting/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/LABJavaIntroTotesting/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/LABJavaIntroTotesting/pom.xml b/LABJavaIntroTotesting/pom.xml new file mode 100644 index 0000000..3acb57e --- /dev/null +++ b/LABJavaIntroTotesting/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + com.ironhack + LABJavaIntroTotesting + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + org.junit.jupiter + junit-jupiter-api + 5.11.4 + test + + + \ No newline at end of file diff --git a/LABJavaIntroTotesting/src/main/java/com/ironhack/javaKeywords/JavaKey.java b/LABJavaIntroTotesting/src/main/java/com/ironhack/javaKeywords/JavaKey.java new file mode 100644 index 0000000..2aa1ee4 --- /dev/null +++ b/LABJavaIntroTotesting/src/main/java/com/ironhack/javaKeywords/JavaKey.java @@ -0,0 +1,54 @@ +package com.ironhack.javaKeywords; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class JavaKey { + private String stringKey; + private final List JAVA_KEYWORDS = Arrays.asList( + "abstract", "assert", "boolean", "break", "byte", + "case", "catch", "char", "class", "const", + "continue", "default", "do", "double", "else", + "enum", "extends", "final", "finally", "float", + "for", "goto", "if", "implements", "import", + "instanceof", "int", "interface", "long", "native", + "new", "package", "private", "protected", "public", + "return", "short", "static", "strictfp", "super", + "switch", "synchronized", "this", "throw", "throws", + "transient", "try", "void", "volatile", "while", + "true", "false", "null"); + + + public JavaKey(String stringKey) { + this.stringKey = stringKey; + + } + + public String getStringKey() { + return stringKey; + } + + public List getJAVA_KEYWORDS() { + return JAVA_KEYWORDS; + } + + public String compareKeyString() { + + String[] words = this.stringKey.split("\\s+"); + List foundKeyWords = new ArrayList<>(); + for (String word : words) { + for (String keyword : JAVA_KEYWORDS) { + if (word.equalsIgnoreCase((keyword))) { + foundKeyWords.add(word); + + } + } + } + if (foundKeyWords.isEmpty()) { + return "Doesn't found any key word."; + } else { + return "Key word found: " + foundKeyWords; + } + } +} diff --git a/LABJavaIntroTotesting/src/main/java/com/ironhack/javaKeywords/Main.java b/LABJavaIntroTotesting/src/main/java/com/ironhack/javaKeywords/Main.java new file mode 100644 index 0000000..cd4ac77 --- /dev/null +++ b/LABJavaIntroTotesting/src/main/java/com/ironhack/javaKeywords/Main.java @@ -0,0 +1,31 @@ +package com.ironhack.javaKeywords; + +import java.util.List; +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + + //We ask user for a String call stringPhrase + Scanner scanner = new Scanner(System.in); + String stringPhrase = ""; + boolean validInput = false; + + do { + System.out.println("Please write your phrase"); + if (scanner.hasNextLine()) { + stringPhrase = scanner.nextLine(); + validInput = true; + } else { + System.out.println("That's not a valid input"); + } + } while (!validInput); + System.out.println("Your phrase is: " + stringPhrase); + scanner.close(); + + JavaKey jk = new JavaKey(stringPhrase); + String foundJavKeyWords = jk.compareKeyString(); + System.out.println(foundJavKeyWords); + + } +} diff --git a/LABJavaIntroTotesting/src/main/java/com/ironhack/oddIntegers/Main.java b/LABJavaIntroTotesting/src/main/java/com/ironhack/oddIntegers/Main.java new file mode 100644 index 0000000..5e3b29b --- /dev/null +++ b/LABJavaIntroTotesting/src/main/java/com/ironhack/oddIntegers/Main.java @@ -0,0 +1,34 @@ +package com.ironhack.oddIntegers; + +import java.util.List; +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + + //We ask user for a number call n + Scanner scanner = new Scanner(System.in); + int n = 0; + boolean validInput = false; + + do { + System.out.println("Please write an integer"); + if (scanner.hasNextInt()) { + n = scanner.nextInt(); + validInput = true; + } else { + System.out.println("That's not a valid integer. Try again"); + scanner.next(); + } + + } while (!validInput); + System.out.println("Your integer is: " + n); + +scanner.close(); + +NListGenerator generator = new NListGenerator(n); + List oddNumbers = generator.generateOddList(); + System.out.println("Odd numbers from 0 to " + n + ": " + oddNumbers); + + } +} diff --git a/LABJavaIntroTotesting/src/main/java/com/ironhack/oddIntegers/NListGenerator.java b/LABJavaIntroTotesting/src/main/java/com/ironhack/oddIntegers/NListGenerator.java new file mode 100644 index 0000000..1298b13 --- /dev/null +++ b/LABJavaIntroTotesting/src/main/java/com/ironhack/oddIntegers/NListGenerator.java @@ -0,0 +1,38 @@ +package com.ironhack.oddIntegers; + +import java.util.ArrayList; +import java.util.List; + +public class NListGenerator { + + private int n; + private List nList; + + public NListGenerator(int n) { + this.n = n; + } + + public int getN() { + return n; + } + + public List generateOddList() { + nList = new ArrayList<>(); + + if (this.n < 0) { + for (int i = 0; i >= this.n; i--) { + if (i % 2 != 0) { + nList.add(i); + } + } + } else if (this.n > 0) { + for (int i = 0; i <= this.n; i++) { + if (i % 2 != 0) { + nList.add(i); + } + } + } + return nList; + } + +} diff --git a/LABJavaIntroTotesting/src/main/java/com/ironhack/videoGame/Elf.java b/LABJavaIntroTotesting/src/main/java/com/ironhack/videoGame/Elf.java new file mode 100644 index 0000000..1a5a3a2 --- /dev/null +++ b/LABJavaIntroTotesting/src/main/java/com/ironhack/videoGame/Elf.java @@ -0,0 +1,25 @@ +package com.ironhack.videoGame; + +public class Elf extends Player{ + private int speed; + + public int getSpeed() { + return speed; + } + + public void setSpeed(int speed) { + this.speed = speed; + } + + public Elf(int health, int strength, int lives, int speed) { + super(health, strength, lives); + this.speed = speed; + } + + @Override + public String toString() { + return "Elf{" + + "speed=" + speed + + "} " + super.toString(); + } +} diff --git a/LABJavaIntroTotesting/src/main/java/com/ironhack/videoGame/Player.java b/LABJavaIntroTotesting/src/main/java/com/ironhack/videoGame/Player.java new file mode 100644 index 0000000..789d0f0 --- /dev/null +++ b/LABJavaIntroTotesting/src/main/java/com/ironhack/videoGame/Player.java @@ -0,0 +1,82 @@ +package com.ironhack.videoGame; + +public class Player { + private int health; + private int strength; + private int lives; + + public Player(int health, int strength, int lives) { + this.health = health; + this.strength = strength; + this.lives = lives; + } + + public int getHealth() { + return health; + } + + public int getStrength() { + return strength; + } + + public int getLives() { + return lives; + } + + public void setHealth(int health) { + this.health = health; + } + + public void setStrength(int strength) { + this.strength = strength; + } + + public void setLives(int lives) { + this.lives = lives; + } + + public void decrementLives() { + if (this.lives > 0) { + this.lives--; + this.health = 100; //Restore the health + } + } + + public void checkHealth() { + if (this.health <= 0) { + this.decrementLives(); + } + } + + public String attack(Player playerToAttack) { + + if (this.strength <= 0) { + return "This player cannot attack, doesn't have any strength"; + } + if (playerToAttack.getLives() <= 0) { + return "The player " + playerToAttack + " is dead"; + } + + int newHealth = playerToAttack.getHealth() - this.strength; + playerToAttack.setHealth(newHealth); + playerToAttack.checkHealth(); + playerToAttack.checkHealth(); + + if (playerToAttack.getLives() <= 0) { + return playerToAttack + " is dead!"; + } else { + return "damage has occurred " + playerToAttack + " health: " + playerToAttack.getHealth(); + } + } + + @Override + public String toString() { + return "Player{" + + "health=" + health + + ", strength=" + strength + + ", lives=" + lives + + '}'; + } +} + + diff --git a/LABJavaIntroTotesting/src/main/java/com/ironhack/videoGame/Warrior.java b/LABJavaIntroTotesting/src/main/java/com/ironhack/videoGame/Warrior.java new file mode 100644 index 0000000..e41d3bd --- /dev/null +++ b/LABJavaIntroTotesting/src/main/java/com/ironhack/videoGame/Warrior.java @@ -0,0 +1,21 @@ +package com.ironhack.videoGame; + +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 Elf convertToElf() { + return new Elf (this.getHealth(), this.getStrength(), this.getLives(), this.force); + + } + + @Override + public String toString() { + return "Warrior{" + + "force=" + force + + "} " + super.toString(); + } +} diff --git a/LABJavaIntroTotesting/src/main/java/com/ironhack/videoGame/Wizard.java b/LABJavaIntroTotesting/src/main/java/com/ironhack/videoGame/Wizard.java new file mode 100644 index 0000000..4b02dbc --- /dev/null +++ b/LABJavaIntroTotesting/src/main/java/com/ironhack/videoGame/Wizard.java @@ -0,0 +1,29 @@ +package com.ironhack.videoGame; + +public class Wizard extends Player { + private int spell; + + public int getSpell() { + return spell; + } + + public void setSpell(int spell) { + this.spell = spell; + } + + public Wizard(int health, int strength, int lives, int spell) { + super(health, strength, lives); + this.spell = spell; + } + + public Elf convertToElf () { + return new Elf(this.getHealth(), this.getStrength(), this.getLives(), this.spell); + } + + @Override + public String toString() { + return "Wizard{" + + "spell=" + spell + + "} " + super.toString(); + } +} diff --git a/LABJavaIntroTotesting/src/test/java/com/ironhack/javaKeywords/JavaKeyTest.java b/LABJavaIntroTotesting/src/test/java/com/ironhack/javaKeywords/JavaKeyTest.java new file mode 100644 index 0000000..41ab696 --- /dev/null +++ b/LABJavaIntroTotesting/src/test/java/com/ironhack/javaKeywords/JavaKeyTest.java @@ -0,0 +1,33 @@ +package com.ironhack.javaKeywords; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class JavaKeyTest { + + @Test + @DisplayName("Should return if exist any Java Keyword") + void compareKeyString() { + JavaKey jk = new JavaKey("The word static is a Key Word."); + String result = jk.compareKeyString(); + assertTrue(result.contains("static")); + + } + @Test + @DisplayName("Should return if doesn't exist any Java Keyword") + void compareKeyStringNoKeyWord() { + JavaKey jk = new JavaKey("Any key word exist."); + String result = jk.compareKeyString(); + assertEquals("Doesn't found any key word.", result); + } + + @Test + @DisplayName("Should return Multiple Key Word") + void compareKeyStringMultipleKeyWord() { + JavaKey jk = new JavaKey("Lets do a for while we have a if and else"); + String result = jk.compareKeyString(); + assertEquals("Key word found: [do, for, while, if, else]", result); + } +} \ No newline at end of file diff --git a/LABJavaIntroTotesting/src/test/java/com/ironhack/oddIntegers/NListGeneratorTest.java b/LABJavaIntroTotesting/src/test/java/com/ironhack/oddIntegers/NListGeneratorTest.java new file mode 100644 index 0000000..6611ee3 --- /dev/null +++ b/LABJavaIntroTotesting/src/test/java/com/ironhack/oddIntegers/NListGeneratorTest.java @@ -0,0 +1,61 @@ +package com.ironhack.oddIntegers; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class NListGeneratorTest { + + @Test + @DisplayName("Should generate a list of positive odd numbers from 0 to n") + void generatePositiveOddList() { + List expected = List.of(1, 3, 5, 7); + int n = 7; + NListGenerator generator = new NListGenerator(n); + List result = generator.generateOddList(); + assertIterableEquals(expected, result); + + } + + @Test + @DisplayName("Should generate a list of negative odd numbers 0 down to -n") + void generateNegativeOddList() { + List expected = List.of(-1, -3, -5, -7); + int n = -7; + NListGenerator generator = new NListGenerator(n); + List result = generator.generateOddList(); + assertIterableEquals(expected, result); + } + + @Test + @DisplayName("Should return an empty list when n is 0") + void generateZeroOddList() { + List expected = List.of(); + int n = 0; + NListGenerator generator = new NListGenerator(n); + List result = generator.generateOddList(); + assertIterableEquals(expected, result); + } + @Test + @DisplayName("Should generate [1] when n is 1") + void generateOddListOne() { + List expected = List.of(1); + int n = 1; + NListGenerator generator = new NListGenerator(n); + List result = generator.generateOddList(); + assertIterableEquals(expected, result); + } + + @Test + @DisplayName("Should generate [-1] when n is -1") + void generateOddListMinusOne() { + List expected = List.of(-1); + int n = -1; + NListGenerator generator = new NListGenerator(n); + List result = generator.generateOddList(); + assertIterableEquals(expected, result); + } +} diff --git a/LABJavaIntroTotesting/src/test/java/com/ironhack/videoGame/PlayerTest.java b/LABJavaIntroTotesting/src/test/java/com/ironhack/videoGame/PlayerTest.java new file mode 100644 index 0000000..5c7a77e --- /dev/null +++ b/LABJavaIntroTotesting/src/test/java/com/ironhack/videoGame/PlayerTest.java @@ -0,0 +1,54 @@ +package com.ironhack.videoGame; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class PlayerTest { + private Player player; + + + @BeforeEach + @DisplayName("Should provide a Player Instance") + void setUp() { + player = new Player(100,20,30); + } + + @Test + @DisplayName("Should decreases Lives and Reset Health when lives is greater than Zero") + void testDecrementLives() { + player.decrementLives(); + assertEquals(29, player.getLives()); + assertEquals(100, player.getHealth()); + + } + + @Test + @DisplayName("Should call decrement lives if the live is zero or less") + void testCheckHealth() { + player.setHealth(0); + player.checkHealth(); + assertEquals(29, player.getLives()); + } + + @Test + @DisplayName("Should attack whe the target is alive") + void testAttackTargetAlive() { + Player target = new Player(80, 10, 2); + String result = player.attack(target); + assertEquals(80 - player.getStrength(), target.getHealth()); + assertTrue(result.contains("damage has occurred")); + } + @Test + @DisplayName("Should not attack the target is dead") + void testAttackTargetDead () { + player.setStrength(0); + Player target = new Player(80, 10, 2); + String result = player.attack(target); + assertEquals("This player cannot attack, doesn't have any strength", result); + } + + +} \ No newline at end of file diff --git a/LABJavaIntroTotesting/src/test/java/com/ironhack/videoGame/WarriorTest.java b/LABJavaIntroTotesting/src/test/java/com/ironhack/videoGame/WarriorTest.java new file mode 100644 index 0000000..c7a0d42 --- /dev/null +++ b/LABJavaIntroTotesting/src/test/java/com/ironhack/videoGame/WarriorTest.java @@ -0,0 +1,18 @@ +package com.ironhack.videoGame; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class WarriorTest extends PlayerTest { + + @Test + void convertToElf() { + Warrior warrior = new Warrior(100, 20, 3, 15); + Elf elf = warrior.convertToElf(); + assertEquals(warrior.getHealth(), elf.getHealth()); + assertEquals(warrior.getStrength(), elf.getStrength()); + assertEquals(warrior.getLives(), elf.getLives()); + assertEquals(15, elf.getSpeed()); + } +} \ No newline at end of file diff --git a/LABJavaIntroTotesting/src/test/java/com/ironhack/videoGame/WizardTest.java b/LABJavaIntroTotesting/src/test/java/com/ironhack/videoGame/WizardTest.java new file mode 100644 index 0000000..444f298 --- /dev/null +++ b/LABJavaIntroTotesting/src/test/java/com/ironhack/videoGame/WizardTest.java @@ -0,0 +1,18 @@ +package com.ironhack.videoGame; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class WizardTest extends PlayerTest { + + @Test + void convertToElf() { + Wizard wizard = new Wizard(100, 20, 3, 15); + Elf elf = wizard.convertToElf(); + assertEquals(wizard.getHealth(), elf.getHealth()); + assertEquals(wizard.getStrength(), elf.getStrength()); + assertEquals(wizard.getLives(), elf.getLives()); + assertEquals(15, elf.getSpeed()); + } +} \ No newline at end of file