Skip to content
Open
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
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
*.idea
.idea
*.iml

target/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
35 changes: 35 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>groupId</groupId>
<artifactId>lab-java-intro-to-testing</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>


</dependencies>

</project>
19 changes: 19 additions & 0 deletions src/main/java/Elf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import lombok.*;

@Getter
public class Elf extends Player {

public static final int DEFAULT_SPEED = 10;

private int speed;

public Elf(int health, int strength, int lives, int speed) {
super(health, strength, lives);
this.speed = speed;
}

public Elf(int health, int strength, int lives) {
super(health, strength, lives);
this.speed = DEFAULT_SPEED;
}
}
34 changes: 34 additions & 0 deletions src/main/java/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public abstract class Player {
public Player(int health, int strength, int lives) {
this.health = health;
this.strength = strength;
this.lives = lives;
initialHealth = health;
}

private final int initialHealth;
private int health;
private int strength;
private int lives;

public void attack(Player playerToAttack) {
playerToAttack.setHealth(playerToAttack.getHealth() - strength);
playerToAttack.checkHealth();
}

public void decrementLive(){
lives--;
health = 3;
if (lives <= 0) System.out.println("This character is dead");
}

public void checkHealth() {
if (health <= 0) decrementLive();
}

}
29 changes: 29 additions & 0 deletions src/main/java/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Utils {


public static int[] getOddsFromOneToValue(int value) {
return IntStream.rangeClosed(1, value)
.filter(n -> n % 2 != 0)
.toArray();
}

public boolean containsKeyWord(String s) {
List<String> keywordsList = Arrays.asList(
"abstract", "continue", "for", "new", "switch", "assert", "default", "goto",
"package", "synchronized", "boolean", "do", "if", "private", "this", "break",
"double", "implements", "protected", "throw", "byte", "else", "import", "public",
"throws", "case", "enum", "instanceof", "return", "transient", "catch", "extends",
"int", "short", "try", "char", "final", "interface", "static", "void", "class",
"finally", "long", "strictfp", "volatile", "const", "float", "native", "super", "while"
);

return Arrays.stream(s.split(" ")).anyMatch(word -> keywordsList.contains(word));
}

}
17 changes: 17 additions & 0 deletions src/main/java/Warrior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import lombok.Getter;

@Getter
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(getHealth(), getStrength(), getLives());
}

}
27 changes: 27 additions & 0 deletions src/main/java/Wizard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import lombok.Getter;



@Getter
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 Elf convertToElf(){
return new Elf(getHealth(), getStrength(), getLives());
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Wizard wizard = (Wizard) o;
return spell == wizard.spell;
}

}
88 changes: 88 additions & 0 deletions src/test/java/PlayerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class PlayerTest {

Warrior warrior;
Wizard wizard;
Elf elf;

@Test
@DisplayName("Should create a warrior and convert it into an elf with the exact feature values and the default speed")
void convertWarriorToElf() {
warrior = new Warrior(100, 10, 3, 5);

elf = warrior.convertToElf();

assertTrue(warrior instanceof Warrior);
assertEquals(5, warrior.getForce());

assertTrue(elf instanceof Elf);

assertEquals(warrior.getHealth(), warrior.getInitialHealth());
assertEquals(warrior.getHealth(), elf.getHealth());
assertEquals(warrior.getStrength(), elf.getStrength());
assertEquals(warrior.getLives(), elf.getLives());
assertEquals(Elf.DEFAULT_SPEED, elf.getSpeed());
}

@Test
@DisplayName("Should create a wizard and convert it into an elf with the exact feature values and the default speed")
void convertWizardToElf() {
wizard = new Wizard(100, 10, 3, 5);

elf = wizard.convertToElf();

assertTrue(wizard instanceof Wizard);
assertEquals(5, wizard.getSpell());

assertTrue(elf instanceof Elf);
assertEquals(100, elf.getHealth());
assertEquals(10, elf.getStrength());
assertEquals(3, elf.getLives());

assertEquals(Elf.DEFAULT_SPEED, elf.getSpeed());
}

@Test
@DisplayName("Should create a copy of one wizard with the same feature values")
void createTwoDifferentWizards() {
Wizard wizard = new Wizard(100, 10, 3, 5);
Wizard wizardCopy = new Wizard(100, 10, 3, 5);

assertTrue(wizard.equals(wizardCopy));
}
@Test
@DisplayName("An Elf should attack a warrior and a wizard and decrease their health by -25")
void decrementHealth() {
wizard = new Wizard(80, 40, 3, 5);
warrior = new Warrior(100, 90, 3, 5);
elf = new Elf(300, 25, 3 ,5);
int initialWarriorHealth = warrior.getHealth();
int initialWizardHealth = wizard.getHealth();

elf.attack(warrior);
elf.attack(wizard);

assertEquals(initialWarriorHealth - elf.getStrength(), warrior.getHealth());
assertEquals(initialWizardHealth - elf.getStrength(), wizard.getHealth());
}

@Test
@DisplayName("An elf should kill a warrior")
void decrementLive() {
warrior = new Warrior(100, 90, 3, 5);
elf = new Elf(300, 25, 3 ,5);
int initialWarriorLives = warrior.getLives();

elf.attack(warrior);
elf.attack(warrior);
elf.attack(warrior);
elf.attack(warrior);

assertEquals(initialWarriorLives - 1, warrior.getLives());
}
}
31 changes: 31 additions & 0 deletions src/test/java/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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 UtilsTest {

private Utils utils;


@BeforeEach
public void setUp() {
utils = new Utils();
}

@Test
@DisplayName("Should take an integer n and return all odd integers from 1 to n")
void getOddsFromOneToValue() {
assertEquals(Utils.getOddsFromOneToValue(3).length, 2);
assertEquals(Utils.getOddsFromOneToValue(5).length, 3);
}

@Test
@DisplayName("Should return true when receive a String containing a keyword")
void containsKeyWord() {
assertTrue(utils.containsKeyWord("Don't break my heart"));
assertFalse(utils.containsKeyWord("I love to breakdance"));
}

}