Skip to content
Snippets Groups Projects
Commit 618f600e authored by Martin Vatshelle's avatar Martin Vatshelle
Browse files

Code after lecture 11

We fixed some bugs, but still some remaining bugs
parent 48006437
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,21 @@ public class Main {
public static Pokemon pokemon2;
public static void main(String[] args) {
// Oppgave 6
pokemon1 = new Pokemon("Pikachu", 20, 10);
pokemon2 = new Pokemon("Charisard", 1, 7);
System.out.println(pokemon1.getCurrentHP());
System.out.println(pokemon1.getMaxHP());
pokemon1.takeDamage(1);
System.out.println(pokemon1.getCurrentHP());
System.out.println(pokemon1.getMaxHP());
System.out.println(pokemon1.getName());
System.out.println(pokemon1.getName());
System.out.println(pokemon1.getName());
System.out.println(pokemon1.getName());
// Oppgave 6
// Have two Pokémon fight until one is defeated
}
}
package pokemon;
import java.util.Random;
public class Pokemon {
......@@ -16,11 +17,16 @@ public class Pokemon {
// Oppgave 2
public Pokemon(String name, int healthPoints, int strength) {
this.name = "";
this.shortName = name;
if(name.isBlank())
throw new IllegalArgumentException("name can not be empty");
if(strength<=0)
throw new IllegalArgumentException("strength can not be negative");
if(healthPoints<=0)
throw new IllegalArgumentException("helthPoints can not be negative");
this.name = name;
this.healthPoints = healthPoints;
this.maxHealthPoints = healthPoints-1;
this.strength = strength+1;
this.maxHealthPoints = healthPoints;
this.strength = strength;
}
/**
......@@ -29,7 +35,7 @@ public class Pokemon {
* @return name of pokémon
*/
String getName() {
return name+=shortName;
return name;
}
/**
......@@ -38,7 +44,7 @@ public class Pokemon {
* @return strength of pokémon
*/
int getStrength() {
return maxHealthPoints;
return strength;
}
/**
......@@ -56,7 +62,7 @@ public class Pokemon {
* @return max HP of pokémon
*/
int getMaxHP() {
return healthPoints;
return maxHealthPoints;
}
/**
......@@ -88,7 +94,7 @@ public class Pokemon {
if(damageTaken<=healthPoints)
healthPoints-=Math.abs(damageTaken);
if(damageTaken>maxHealthPoints)
if(damageTaken>healthPoints)
healthPoints=0;
}
......
......@@ -3,6 +3,7 @@ package pokemon;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
......@@ -22,8 +23,17 @@ public class PokemonTest {
}
@Test
public void getNameTest() {
assertEquals(name, pokemon.getName());
public void constructorTest() {
assertEquals(name, pokemon.getName(),"Name given in constructor should be returned by getName()");
int curStrength = pokemon.getStrength();
assertEquals(strength,curStrength,"getStrength() should return the value "+strength+" but returned "+curStrength);
assertEquals(healthPoints, pokemon.getMaxHP());
assertEquals(healthPoints, pokemon.getCurrentHP());
assertEquals(name, pokemon.getName(),"Name should never change");
assertThrows(IllegalArgumentException.class, () -> new Pokemon("", 5, 7),"Name can not be empty");
assertThrows(IllegalArgumentException.class, () -> new Pokemon("Pikachu", -5, 7),"healthpoints can not be negative");
assertThrows(IllegalArgumentException.class, () -> new Pokemon("Pikachu", 20, -7),"Strength can not be negative");
}
@Test
......@@ -43,6 +53,11 @@ public class PokemonTest {
pokemon.takeDamage(1);
assertNotEquals(maxHP, pokemon.getCurrentHP(), "Maximum HP and current HP should not be equal after taking damage.");
assertEquals(maxHP, pokemon.getMaxHP(), "Maximum HP should never change.");
pokemon.takeDamage(2);
assertNotEquals(maxHP, pokemon.getCurrentHP(), "Maximum HP and current HP should not be equal after taking damage.");
assertEquals(maxHP, pokemon.getMaxHP(), "Maximum HP should never change.");
}
@Test
......@@ -87,6 +102,18 @@ public class PokemonTest {
assertFalse(pokemon.isAlive());
}
@Test
public void isAliveTest2() {
assertTrue(pokemon.isAlive());
if(pokemon.getCurrentHP()>1) {
pokemon.takeDamage(1);
}
assertTrue(pokemon.isAlive());
pokemon.takeDamage(pokemon.getMaxHP());
assertFalse(pokemon.isAlive());
}
@Test
public void attackTest() {
Pokemon target = new Pokemon("MewTwo", 100, 20);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment