diff --git a/src/code/ca/bcit/comp2522/lab02/Creature.class b/src/code/ca/bcit/comp2522/lab02/Creature.class new file mode 100644 index 0000000..d9ff845 Binary files /dev/null and b/src/code/ca/bcit/comp2522/lab02/Creature.class differ diff --git a/src/code/ca/bcit/comp2522/lab02/Creature.java b/src/code/ca/bcit/comp2522/lab02/Creature.java index 4a83b60..d1bbc04 100644 --- a/src/code/ca/bcit/comp2522/lab02/Creature.java +++ b/src/code/ca/bcit/comp2522/lab02/Creature.java @@ -68,7 +68,7 @@ public class Creature { * @param dateOfBirth of creature * @param health of creature to start */ - Creature(final String name, + public Creature(final String name, final Date dateOfBirth, final int health) { @@ -101,17 +101,22 @@ public class Creature { * @param damage to be dealt * @throws DamageException if damage is negative */ - protected void takeDamage(final int damage) - throws DamageException + public void takeDamage(final int damage) + throws DamageException, RuntimeException { - if (damage < NO_HEALTH) { - throw new DamageException("Damage cannot be negative"); + if (!this.isAlive()){ + throw new RuntimeException("The creature is not alive."); } + else{ + if (damage < NO_HEALTH) { + throw new DamageException("Damage cannot be negative"); + } - this.health -= damage; + this.health -= damage; - if (this.health < NO_HEALTH) { - this.health = NO_HEALTH; + if (this.health < NO_HEALTH) { + this.health = NO_HEALTH; + } } } @@ -153,8 +158,19 @@ public class Creature { * * @return details of the creature. */ - protected String getDetails() { - return "Name: " + this.name; + public String getDetails() { + StringBuilder builder; + builder = new StringBuilder(); + builder.append("Is alive: " + this.isAlive()); + builder.append("Name: " + this.name); + builder.append("Date of birth: " + this.dateOfBirth); + builder.append("Age: " + this.getAgeYears()); + builder.append("Health: " + this.health); + + + + return builder.toString(); + } /** @@ -162,7 +178,18 @@ public class Creature { * * @return name of the creature */ - protected String getName() { + public String getName() { return this.name; } -} + + /** + * Prints the details of the creature. + */ + public void printDetails() { + System.out.println(getDetails()); + } + + public int getHealth() { return this.health;} + public Date getDateOfBirth() { return this.dateOfBirth;} + +} \ No newline at end of file diff --git a/src/code/ca/bcit/comp2522/lab02/DamageException.class b/src/code/ca/bcit/comp2522/lab02/DamageException.class new file mode 100644 index 0000000..7d21c00 Binary files /dev/null and b/src/code/ca/bcit/comp2522/lab02/DamageException.class differ diff --git a/src/code/ca/bcit/comp2522/lab02/Date.class b/src/code/ca/bcit/comp2522/lab02/Date.class new file mode 100644 index 0000000..702dfb0 Binary files /dev/null and b/src/code/ca/bcit/comp2522/lab02/Date.class differ diff --git a/src/code/ca/bcit/comp2522/lab02/Date.java b/src/code/ca/bcit/comp2522/lab02/Date.java index 00cf2b6..8a777b5 100644 --- a/src/code/ca/bcit/comp2522/lab02/Date.java +++ b/src/code/ca/bcit/comp2522/lab02/Date.java @@ -13,7 +13,7 @@ public final class Date { public static final int MAX_YEAR = 2025; // Domain of dates - private static final int MIN_YEAR = 1800; + private static final int MIN_YEAR = 0; private static final int MIN_DAY = 1; private static final int CURRENT_DAY = 15; private static final int CURRENT_MONTH = 9; diff --git a/src/code/ca/bcit/comp2522/lab02/Dragon.class b/src/code/ca/bcit/comp2522/lab02/Dragon.class new file mode 100644 index 0000000..d0932c8 Binary files /dev/null and b/src/code/ca/bcit/comp2522/lab02/Dragon.class differ diff --git a/src/code/ca/bcit/comp2522/lab02/Dragon.java b/src/code/ca/bcit/comp2522/lab02/Dragon.java index b0ad86e..e92bc00 100644 --- a/src/code/ca/bcit/comp2522/lab02/Dragon.java +++ b/src/code/ca/bcit/comp2522/lab02/Dragon.java @@ -38,7 +38,7 @@ public class Dragon extends Creature { * @param health int * @param firePower int */ - Dragon( + public Dragon( final String name, final Date dateOfBirth, final int health, @@ -55,10 +55,21 @@ public class Dragon extends Creature { * @return details of dragon */ @Override - protected String getDetails() { - return "Name: " + this.getName() + " FirePower: " + this.firePower; - } + public String getDetails() { + StringBuilder builder; + builder = new StringBuilder(); + builder.append("Is alive: " + this.isAlive()); + builder.append(" Name: " + this.getName()); + builder.append(" Date of birth: " + (this.getDateOfBirth()).getYyyyMmDd()); + builder.append(" Age: " + this.getAgeYears()); + builder.append(" Health: " + this.getHealth()); + builder.append(" FirePower: " + this.firePower); + + + return builder.toString(); + + } /** * breathFire onto Creatures to deal damage * and reduces current firePower value @@ -66,16 +77,22 @@ public class Dragon extends Creature { * @param target Creature to deal damage to * @throws LowFirePowerException if not enough firePower */ - protected void breatheFire(final Creature target) - throws LowFirePowerException - { - if (firePower > FIRE_POWER_USAGE) { - throw new LowFirePowerException("Fire power too low"); + public void breatheFire(final Creature target) + throws LowFirePowerException, RuntimeException { + + if (!this.isAlive()) { + throw new RuntimeException("The dragon is not alive."); + } + else { + if(firePower < FIRE_POWER_USAGE) { + throw new LowFirePowerException("Fire power too low"); + } } this.firePower -= FIRE_POWER_USAGE; target.takeDamage(FIRE_POWER_DAMAGE); + } /** diff --git a/src/code/ca/bcit/comp2522/lab02/Elf.class b/src/code/ca/bcit/comp2522/lab02/Elf.class new file mode 100644 index 0000000..c41c807 Binary files /dev/null and b/src/code/ca/bcit/comp2522/lab02/Elf.class differ diff --git a/src/code/ca/bcit/comp2522/lab02/Elf.java b/src/code/ca/bcit/comp2522/lab02/Elf.java index ccd90c1..4e4d5e7 100644 --- a/src/code/ca/bcit/comp2522/lab02/Elf.java +++ b/src/code/ca/bcit/comp2522/lab02/Elf.java @@ -38,7 +38,7 @@ public class Elf extends Creature { * @param health int * @param mana int */ - Elf( + public Elf( final String name, final Date dateOfBirth, final int health, @@ -57,8 +57,17 @@ public class Elf extends Creature { * @return details of elf */ @Override - protected String getDetails() { - return "Name: " + this.getName() + " Mana: " + this.mana; + public String getDetails() { + StringBuilder builder; + builder = new StringBuilder(); + builder.append("Is alive: " + this.isAlive()); + builder.append(" Name: " + this.getName()); + builder.append(" Date of birth: " + (this.getDateOfBirth()).getYyyyMmDd()); + builder.append(" Age: " + this.getAgeYears()); + builder.append(" Health: " + this.getHealth()); + builder.append(" Mana: " + this.mana); + + return builder.toString(); } /** @@ -68,16 +77,21 @@ public class Elf extends Creature { * @param target Creature to deal damage to * @throws LowManaException if mana value is too low */ - protected void castSpell(final Creature target) - throws LowManaException + public void castSpell(final Creature target) + throws LowManaException, RuntimeException { - if (this.mana < MANA_USAGE) { - throw new LowManaException("Not enough mana to cast spell"); + if(!this.isAlive()){ + throw new RuntimeException("The elf is not alive."); } + else{ + if (this.mana < MANA_USAGE) { + throw new LowManaException("Not enough mana to cast spell"); + } - this.mana -= MANA_USAGE; + this.mana -= MANA_USAGE; - target.takeDamage(MANA_DAMAGE); + target.takeDamage(MANA_DAMAGE); + } } /** diff --git a/src/code/ca/bcit/comp2522/lab02/HealingException.class b/src/code/ca/bcit/comp2522/lab02/HealingException.class new file mode 100644 index 0000000..daa9a29 Binary files /dev/null and b/src/code/ca/bcit/comp2522/lab02/HealingException.class differ diff --git a/src/code/ca/bcit/comp2522/lab02/LowFirePowerException.class b/src/code/ca/bcit/comp2522/lab02/LowFirePowerException.class new file mode 100644 index 0000000..8faa063 Binary files /dev/null and b/src/code/ca/bcit/comp2522/lab02/LowFirePowerException.class differ diff --git a/src/code/ca/bcit/comp2522/lab02/LowManaException.class b/src/code/ca/bcit/comp2522/lab02/LowManaException.class new file mode 100644 index 0000000..29398a3 Binary files /dev/null and b/src/code/ca/bcit/comp2522/lab02/LowManaException.class differ diff --git a/src/code/ca/bcit/comp2522/lab02/LowRageException.class b/src/code/ca/bcit/comp2522/lab02/LowRageException.class new file mode 100644 index 0000000..c182d74 Binary files /dev/null and b/src/code/ca/bcit/comp2522/lab02/LowRageException.class differ diff --git a/src/code/ca/bcit/comp2522/lab02/Orc.class b/src/code/ca/bcit/comp2522/lab02/Orc.class new file mode 100644 index 0000000..e2e99b4 Binary files /dev/null and b/src/code/ca/bcit/comp2522/lab02/Orc.class differ diff --git a/src/code/ca/bcit/comp2522/lab02/Orc.java b/src/code/ca/bcit/comp2522/lab02/Orc.java index 2331f35..20df0d0 100644 --- a/src/code/ca/bcit/comp2522/lab02/Orc.java +++ b/src/code/ca/bcit/comp2522/lab02/Orc.java @@ -40,7 +40,7 @@ public class Orc extends Creature { * @param health int * @param rage int */ - Orc( + public Orc( final String name, final Date dateOfBirth, final int health, @@ -59,8 +59,17 @@ public class Orc extends Creature { * @return details of orc */ @Override - protected String getDetails() { - return "Name: " + this.getName() + " Rage: " + this.rage; + public String getDetails() { + StringBuilder builder; + builder = new StringBuilder(); + builder.append("Is alive: " + this.isAlive()); + builder.append(" Name: " + this.getName()); + builder.append(" Date of birth: " + (this.getDateOfBirth()).getYyyyMmDd()); + builder.append(" Age: " + this.getAgeYears()); + builder.append(" Health: " + this.getHealth()); + builder.append(" Rage: " + this.rage); + + return builder.toString(); } /** @@ -71,21 +80,30 @@ public class Orc extends Creature { * @param target Creature to deal damage to * @throws LowRageException if rage value is too low */ - protected void bezerk(final Creature target) - throws LowRageException + public void berserk(final Creature target) + throws LowRageException, RuntimeException { - this.rage += RAGE_INCREMENT; + if(!this.isAlive()){ + throw new RuntimeException("The orc is not alive."); + } + else{ + + this.rage += RAGE_INCREMENT; + + if (this.rage > MAX_RAGE_VALUE) { + this.rage = MAX_RAGE_VALUE; + } + + if (rage < RAGE_INCREMENT) { + throw new LowRageException("Rage too low"); + } else if (this.rage > DAMAGE_THRESHOLD) { + target.takeDamage(DAMAGE * DOUBLE); + } else { + target.takeDamage(DOUBLE); + } - if (this.rage > MAX_RAGE_VALUE) { - this.rage = MAX_RAGE_VALUE; } - if (rage < RAGE_INCREMENT) { - throw new LowRageException("Rage too low"); - } else if (this.rage > DAMAGE_THRESHOLD) { - target.takeDamage(DAMAGE * DOUBLE); - } else { - target.takeDamage(DOUBLE); - } } + } diff --git a/src/tests/ca/bcit/comp2522/CreatureTest.java b/src/tests/ca/bcit/comp2522/CreatureTest.java new file mode 100644 index 0000000..4619a83 --- /dev/null +++ b/src/tests/ca/bcit/comp2522/CreatureTest.java @@ -0,0 +1,119 @@ +package ca.bcit.comp2522; + +import ca.bcit.comp2522.lab02.Creature; +import ca.bcit.comp2522.lab02.Date; +import ca.bcit.comp2522.lab02.Dragon; +import ca.bcit.comp2522.lab02.Elf; +import ca.bcit.comp2522.lab02.Orc; +import ca.bcit.comp2522.lab02.LowFirePowerException; +import ca.bcit.comp2522.lab02.LowManaException; +import ca.bcit.comp2522.lab02.LowRageException; + +/** + * A test driver for the Creature class and its subclasses. + */ +public class CreatureTest { + /** + * Main entry point for the test program. + * + * @param args command-line arguments (not used) + * @throws LowFirePowerException if a dragon attempts to breathe fire with insufficient fire power + * @throws LowManaException if an elf attempts to cast a spell with insufficient mana + */ + public static void main(String[] args) throws LowFirePowerException, LowManaException, LowRageException { + + final Creature creatureDragon; + final Creature creatureElf; + final Creature creatureOrc; + + final Date dragonBirthDate; + final Date elfBirthDate; + final Date orcBirthDate; + + dragonBirthDate = new Date(2012, 9, 17); + elfBirthDate = new Date(1900, 11, 5); + orcBirthDate = new Date(500, 5, 3); + + creatureDragon = new Dragon("Sparky", dragonBirthDate, 5, 60); + creatureElf = new Elf("Ben", elfBirthDate, 10, 15); + creatureOrc = new Orc("Shobob", orcBirthDate, 12, 3); + + final Dragon dragon = (Dragon)creatureDragon; + final Elf elf = (Elf)creatureElf; + final Orc orc = (Orc)creatureOrc; + + + + dragon.printDetails(); + if (dragon instanceof Creature) System.out.println("This creature is a Dragon."); + elf.printDetails(); + if (elf instanceof Creature) System.out.println("This creature is an Elf."); + orc.printDetails(); + if (orc instanceof Creature) System.out.println("This creature is an Orc."); + + // Fight sequence — all guarded by friendly exception handling + try { + + System.out.println("- Dragon breathed fire on the Elf"); + dragon.breatheFire(elf); + + dragon.printDetails(); + elf.printDetails(); + orc.printDetails(); + + } catch (LowFirePowerException e) { + System.out.println("Attack Not allowed: the dragon's fire power was too low to breathe fire."); + } catch (Exception e) { + System.out.println("Unexpected problem during attack: " + e.getMessage()); + } + + try { + + System.out.println("- Elf cast a spell on the Dragon"); + elf.castSpell(dragon); + + dragon.printDetails(); + elf.printDetails(); + orc.printDetails(); + + } catch (LowManaException e) { + System.out.println("Attack Not allowed: the elf didn't have enough mana to cast the spell."); + } catch (Exception e) { + System.out.println("Unexpected problem during attack: " + e.getMessage()); + } + + try { + + System.out.println("- Orc went berserk on the Elf"); + orc.berserk(elf); + + dragon.printDetails(); + elf.printDetails(); + orc.printDetails(); + + } catch (LowRageException e) { + System.out.println("Attack Not allowed: the orc didn't have enough rage to go berserk"); + } catch (Exception e) { + System.out.println("Unexpected problem during attack: " + e.getMessage()); + } + try { + + System.out.println("- Dragon breathed fire on the Elf"); + dragon.breatheFire(elf); + + dragon.printDetails(); + elf.printDetails(); + orc.printDetails(); + + } catch (LowFirePowerException e) { + System.out.println("Attack Not allowed: the dragon's fire power was too low to breathe fire."); + } catch (Exception e) { + System.out.println("Unexpected problem during attack: " + e.getMessage()); + } + + System.out.println("Final Stats:"); + dragon.printDetails(); + elf.printDetails(); + orc.printDetails(); + } +}