Code Review

OOP. Principles.

?

public String mew() {
    return "mew";
}

public String mew(Person user) {
    int debuff = -2;
    user.changeHappiness(debuff);
    return "mew";
}

?

public class CatTest {
    Cat cat = new Cat("Aspen");
    Person testPersonOne = new Person(100);
    Person testPersonTwo = new Person(80);

    @Test
    void mewTest2() {
        cat.mew(testPersonOne);
        int actual = testPersonOne.getHappiness();

        Assertions.assertEquals(98, actual);
    }

    @Test
    void mewTest3() {
        cat.mew(testPersonOne);
        cat.mew(testPersonOne);
        int actual = testPersonOne.getHappiness();

        Assertions.assertEquals(96, actual);
    }

    @Test
    void mewTest4() {
        String actualMew = cat.mew(testPersonOne);
        cat.mew(testPersonOne);
        cat.mew(testPersonOne);
        int actual = testPersonOne.getHappiness();

        Assertions.assertEquals("mew", actualMew);
        Assertions.assertEquals(94, actual);
    }

    @Test
    void purrTest1() {
        String actualPurr = cat.purr(testPersonTwo);
        int actualHappiness = testPersonTwo.getHappiness();

        Assertions.assertEquals("purr", actualPurr);
        Assertions.assertEquals(85, actualHappiness);
    }

    @Test
    void purrTest2() {
        String actualPurr = cat.purr(testPersonTwo);
        cat.purr(testPersonTwo);
        cat.purr(testPersonTwo);
        int actualHappiness = testPersonTwo.getHappiness();

        Assertions.assertEquals("purr", actualPurr);
        Assertions.assertEquals(95, actualHappiness);
    }

    @Test
    void purrTest3() {
        String actualPurr = cat.purr(testPersonTwo);
        cat.purr(testPersonTwo);
        cat.purr(testPersonTwo);
        cat.purr(testPersonTwo);
        int actualHappiness = testPersonTwo.getHappiness();

        Assertions.assertEquals("purr", actualPurr);
        Assertions.assertEquals(100, actualHappiness);
    }
}

?

public abstract class Enemy implements Mortal {
    private int health;

    public Enemy(int health) {
        this.health = health;
    }

    public void takeDamage(int damage) {
        health -= damage;
    }

    public abstract void attackHero(Hero hero);

    @Override
    public boolean isAlive() {
        return health > 0;
    }

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        this.health = health;
    }
}
@Test
public void takeDamageTest3() {
    Hero hero = new Mag("Antonidas", 110);

    int damage = 50;
    hero.takeDamage(damage);

    Assertions.assertEquals(60, hero.getHealth());
}

?

public class Person {
    double happiness;

    // ...
}

?

public class CatTest {
    @Test
    void cat() {
        Assertions.assertEquals("Lily", new Cat("Lily").getName());
    }

    @Test
    void mew() {
        Assertions.assertEquals("mew", new Cat("Lily").mew());
    }

    @Test
    void pur() {
        Assertions.assertEquals("pur", new Cat("Lily").pur());
    }

    @Test
    void getName() {
        Cat Lily = new Cat("Lily");
        Assertions.assertEquals("Lily", Lily.getName());
    }

    @Test
    void setName() {
        Cat Lily = new Cat("Lily");
        Lily.setName("Lily2");
        Assertions.assertEquals("Lily2", Lily.getName());
    }


}

?

public abstract class Enemy implements Mortal {
    private int health;
    boolean isAlive;

    public Enemy(int health, boolean isAlive) {
        this.health = health;
        this.isAlive = isAlive;
    }

    public void takeDamage(int damage) {
        this.health -= damage;
    }

    public int setHealth(int health) {
        this.health = health;
        return health;
    }

    public int getHealth() {
        return health;
    }

    public abstract void attackHero(Hero hero);

    public abstract void skillEnemy(Hero hero);

    @Override
    public boolean isAlive() {
        return getHealth() > 0;
    }
}

?

public class Mag extends Hero {
    public Mag(String name, int health) {
        super(name, health);
    }

    @Override
    public void attackEnemy(Enemy enemy) {
        System.out.println("Mag Attack Enemy!!!");
        if (enemy.isAlive()) {
            enemy.takeDamage(10);
        }
    }

    @Override
    public void skillHero(Enemy enemy) {
        int battleHeal = 15;
        if (this.getHealth() < 50 && this.getHealth() > 40) {
            setHealth(getHealth() + battleHeal);
        }
    }

    @Override
    public boolean isAlive() {
        return super.getHealth() > 0;
    }
}

?

public class Skeleton extends Enemy {
    public Skeleton(int health, boolean isAlive) {
        super(health, isAlive);
    }

    @Override
    public void attackHero(Hero hero) {
        hero.takeDamage(4);
    }

    @Override
    public void skillEnemy(Hero hero) {
        final int fullRestoreHealth = 100;
        if (this.getHealth() == 0) {
            this.setHealth(fullRestoreHealth);
        }
    }
}

?

public class Warrior extends Hero {
    protected final int DAMAGE_WARRIOR = 30;

    public Warrior(String name, int health) {
        super(name,health);
    }

    public int attackEnemy(Enemy enemy) {
        if(enemy.isAlive()) {
            enemy.takeDamage(DAMAGE_WARRIOR);
        }
        return enemy.getHealth();
    }
}

public class Archer extends Hero {
    protected final int DAMAGE_ARCHER = 20;

    public Archer(String name, int health) {
        super(name, health);
    }

    @Override
    public int attackEnemy(Enemy enemy) {
        if(enemy.isAlive()) {
            enemy.takeDamage(DAMAGE_ARCHER);
        }
        return enemy.getHealth();
    }
}

?

public class Siamese extends Cat {
    public String mew() {
        return "Mewmew - Mewmew!";
    }

    @Override
    public String mew(Person person) {
        person.changeHappiness(-5);
        return "Mew - Mew!";
    }

    public String purr() {
        return "MrrMrrMmrrrmmrrrr!";
    }

    @Override
    public String purr(Person person) {
        person.changeHappiness(+5);
        return "MrrMrrM!";
    }
}

?

public int attackMag() {
    Mag mag = new Mag("Mag", 2);
    Zombie zombie = new Zombie(50);
    mag.attackEnemy(zombie);
    return zombie.getHealth() / 2;

}

?

abstract public class Hero {
    private String name;
    private int health;

    public Hero(String name, int health) {
        this.health = health;
        this.name = name;
    }

    public Hero() {
    }

    public void takeDamageHero(int damage) {
        health -= damage;
    }

    public abstract void attackEnemy(Enemy enemy);

    public String getName() {
        return name;
    }

    public int getHealth() {
        return health;
    }
}

?

public String screams() {
    String cry = "";
    if (isAlive()) {
        cry = "I will finish you";
    } else {
        cry = "I will back";
    }
    return cry;
}

?

public class TestEnemy {
    Zombie zombie = new Zombie(50);
    Alligator alligator = new Alligator(50);
    Godzilla godzilla = new Godzilla(50);
    Mag mag = new Mag("Mag", 50);
    Archer archer = new Archer("Archer", 50);
    Warrior warrior = new Warrior("Warrior", 50);


    @Test
    public void testEnemyZombie() {
        //WHEN
        int actual = zombie.attackMag();

        //THEN
        Assertions.assertEquals(24, actual);

    }

    @Test
    public void testEnemyZombieTakeDamage() {
        //WHEN
        archer.attackEnemy(alligator);
        int actual = alligator.getHealth();

        //THEN
        Assertions.assertEquals(49, actual);

    }

?

static Stream<Arguments> provideArgumentsCatMewChangeHappiness() {
    return Stream.of(
            Arguments.of("90", "Mew")
    );
}

@ParameterizedTest
@MethodSource("provideArgumentsCatMewChangeHappiness")
public void testCatMewChangeHappiness(double expectedHappiness, String expectedMew) {
    String actualMew = cat.mew(person1);
    double actualHappiness = person1.getHappiness();

    Assertions.assertEquals(expectedMew, actualMew);
    Assertions.assertEquals(expectedHappiness, actualHappiness);
}

?

public abstract class Hero implements Mortal {
    private String name;
    public int health;
    private boolean isAlive;

    public Hero(String name, int health, boolean isAlive) {
        this.name = name;
        this.health = health;
        this.isAlive = isAlive;
    }

    public abstract void attackEnemy(Enemy enemy);

    public void takeDamage(int damage) {
        setHealth(getHealth() - damage);
    }

    @Override
    public boolean isAlive() {
        isAlive = getHealth() > 0;
        return isAlive;
    }

    public String getName() {
        return name;
    }

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        this.health = health;
    }
}

?

public abstract class Enemy implements Mortal {
    private int health;
    private boolean isAlive;

    public Enemy(int health, boolean isAlive) {
        this.health = health;
        this.isAlive = isAlive;
    }

    public void takeDamage(int damage) {
        setHealth(getHealth() - damage);
    }

    @Override
    public boolean isAlive() {
        isAlive = getHealth() > 0;
        return isAlive;
    }

    public abstract void attackHero(Hero hero);

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        this.health = health;
    }
}

?

public class EnemySkeleton extends Enemy {
    private final int DAMAGE_SKELETON = 25;
    private final int DAMAGE_MULTIPLAYER = 2;

    public EnemySkeleton(int health, boolean isAlive) {
        super(health, isAlive);
    }

    @Override
    public void attackHero(Hero hero) {
        hero.takeDamage(DAMAGE_SKELETON);
    }

    public void attackHeroIncreased(Hero hero) {
        hero.takeDamage(DAMAGE_SKELETON * DAMAGE_MULTIPLAYER);
    }
}

?

public class Archer extends Hero {
    private final int DAMAGE_ARCHER = 10;

    public Archer(String name, int health, boolean isAlive) {
        super(name, health, isAlive);
    }

    @Override
    public void attackEnemy(Enemy enemy) {
        enemy.takeDamage(DAMAGE_ARCHER);
    }

    public void attackEnemyFatality(Enemy enemy) {
        do {
            enemy.takeDamage(DAMAGE_ARCHER);
        } while (enemy.getHealth() > 0);
    }
}

?

public class TestArcher {
    Archer archer = new Archer("Archer", 100, true);
    Enemy enemy = new EnemySkeleton(100, true);

    @Test
    public void testAttackEnemy() {
        final int EXPECTED_ENEMY_HEALTH = 90;

        archer.attackEnemy(enemy);

        Assertions.assertEquals(EXPECTED_ENEMY_HEALTH, enemy.getHealth());
    }

    @Test
    public void testAttackEnemyFatality() {
        final int EXPECTED_ENEMY_HEALTH_FATALITY = 0;

        archer.attackEnemyFatality(enemy);

        Assertions.assertEquals(EXPECTED_ENEMY_HEALTH_FATALITY, enemy.getHealth());
    }
}

Sequence

?

public class TestEnemy {
    Enemy enemy = new EnemyZombie(100, true);
    Hero hero = new Mag("Mag", 100, true);

    @Test
    public void testTakeDamage() {
        final int DAMAGE = 20;
        final int EXPECTED_HEALTH = 80;

        enemy.takeDamage(DAMAGE);

        Assertions.assertEquals(EXPECTED_HEALTH, enemy.getHealth());
    }

    @Test
    public void testIsAliveTrue() {
        Assertions.assertEquals(true, enemy.isAlive());
    }

    @Test
    public void testIsAliveFalse() {
        do {
            hero.attackEnemy(enemy);
        } while (enemy.getHealth() > 0);

        Assertions.assertEquals(false, enemy.isAlive());
    }
}

?

public abstract class Enemy implements Mortal {
    private int health;
    boolean isAlive;


    public Enemy(int health, boolean isAlive) {
        this.health = health;
        this.isAlive = isAlive;
    }

    public void takeDamage(int damage) {
        this.health -= damage;
    }

    public abstract void attackHero(Hero hero);

    @Override
    public boolean isAlive() {
        return getHealth() > 0;
    }

    public void setHealth(int health) {
        this.health = health;
    }

    public int getHealth() {
        return health;
    }
}

?

public class Vampire extends Enemy {
    public Vampire(int health, boolean isAlive) {
        super(health, isAlive);
    }

    @Override
    public void attackHero(Hero hero) {
        final int DAMAGE_VAMPIRE = 30;
        hero.takeDamage(DAMAGE_VAMPIRE);
    }

    public void takeHealth(Hero hero) {
        final int TAKE_HEALTH = 10;
        hero.takeDamage(TAKE_HEALTH);
        setHealth(getHealth() + TAKE_HEALTH);
    }
}

?

static Stream<Arguments> provideArgumentsForSetAndGetNameMewAndPurrMethods() {
    return Stream.of(
            Arguments.of("Mr. Paw", "Mr. Paw meow-meow purr"),
            Arguments.of("Big Cat", "Big Cat meow-meow purr"),
            Arguments.of("Buddy", "Buddy meow-meow purr")
    );
}

@ParameterizedTest
@MethodSource("provideArgumentsForSetAndGetNameMewAndPurrMethods")
void testSetAndGetNameMewAndPurrMethods(String name, String expected) {
    cat.setName(name);
    String actual = cat.getName() + " " + cat.mew() + " " + cat.purr();
    Assertions.assertEquals(expected, actual);
}

?

public class Enemy implements Mortal {
    private int health;

    public Enemy(int health) {
        this.health = health;
    }

    public int takeDamage(int damage) {
        if (isAlive()) {
            setHealth(getHealth() - damage);
        }
        return getHealth();
    }

    public boolean isAlive() {
        return getHealth() > 0;
    }

    public int attackHero(Hero hero) {
        return attackHero(hero);
    }

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        this.health = health;
    }
}

?

public class Werewolf extends Enemy {
    private final int werewolfDamage = 25;
    private Random random = new Random();

    public Werewolf(int health) {
        super(health);
    }

    public void attackHero(Hero hero) {
        hero.takeDamage(werewolfDamage);
        if (healingAttack() == true) {
            super.setHealth(getHealth() + 30);
        }
    }

    @Override
    public void takeDamage(int damage) {
        if (isAlive() == true) {
            super.takeDamage(damage);
        } else {
            super.setHealth(0);
        }
        if (isAlive() == false) {
            super.setHealth(0);
        }
    }

    public boolean healingAttack() {
        int chanceHealingAttack = random.nextInt(2);
        if (chanceHealingAttack == 1) {
            return true;
        } else {
            return false;
        }
    }
}

?

public class Mag extends Hero {
    private final int magDamage = 40;
    private Random random = new Random();

    public Mag(String name, int health) {
        super(name, health);
    }

    @Override
    public void attackEnemy(Enemy enemy) {
        enemy.takeDamage(magDamage);
    }

    @Override
    public void takeDamage(int damage) {
        if (isAlive() == true) {
            super.takeDamage(damage);
        } else {
            super.setHealth(0);
        }
        if (isAlive() == true) {
            if (healthRecovery() == true) {
                super.setHealth(getHealth() + 40);
            }
        } else {
            super.setHealth(0);
        }
    }

    public boolean healthRecovery() {
        int chanceHealthRecovery = random.nextInt(2);
        if (chanceHealthRecovery == 1) {
            return true;
        } else {
            return false;
        }
    }
}

?

@MethodSource("provideArgumentsAttackEnemy")
void testAttackEnemy(int healthEnemy, int expected) {
    Archer archer = new Archer("Legolas", 100);
    Enemy enemy = new Enemy(healthEnemy);

    archer.attackEnemy(enemy);
    int actual = enemy.getHealth();

    assertEquals(expected, actual);
}

?

/**
 * Parameterized tests I can't be written because the method is called by a random value.
 */
@Test
void testAttackEnemy() {
    Archer archer = new Archer("Legolas", 100);
    int enemyDamage = 30;
    int heroHealth;

    archer.takeDamage(enemyDamage);
    heroHealth = archer.getHealth();
    System.out.println("Hero health: " + heroHealth);
    archer.setHealth(10);
}

?

static Stream<Arguments> provideArgumentsCatMew() {
    return Stream.of(
            Arguments.of("Mew")
    );
}
@ParameterizedTest
@MethodSource("provideArgumentsCatMew")
void testCatMew(String expected) {
    Cat cat = new Cat("Murzik");

    String actual = cat.mew();

    assertEquals(expected, actual);
}

?

public class Cat {
    private String name;

    Cat(String name) {
        this.name = name;
    }

    public void mew() { System.out.println("mjau"); }

    public void mew(Person person) {
        person.changeHappiness(-1);
        this.mew();
    }

?

public abstract class Hero implements Mortal {
    private String name;
    private int health;

    {
        health = 200;
    }

    Hero(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public abstract void attackEnemy();

    public void attackEnemy(Enemy enemy) {
        enemy.takeDamage(5);
    }
    public void setHealth(int health) {
        this.health = health;
    }

    public int getHealth(){
        return health;
    }
    public void takeDamage(int damage) {
        health -= damage;
    }

    public boolean isAlive() {
        return (health > 0);
    }
}

?

@Test
public void testMew() {
    cat.mew();
    System.out.println("mjau");
}

?

public class HeroTests {}
/*
    Hero hero = new Hero("Peter");
    Enemy enemy = new Enemy(100);
    @Test
    public void testHero() {
        Assertions.assertEquals("Peter", hero.getName());
    }
    @Test
    public void testHeroAttackEnemy() {
        hero.attackEnemy();
        System.out.println("Hero attacks enemy");
    }
    @Test
    public void testHeroAttackEnemyAndDamage() {
        hero.attackEnemy(enemy);
        Assertions.assertEquals(95, enemy.getHealth());
    }
}*/