Merge pull request #1 from SowinskiBraeden/nico

Lab 2: Some fixes, Test class and comments added.
This commit is contained in:
SowinskiBraeden authored and GitHub committed 2025-09-21 22:03:12 -07:00
commit cd718fc2c7
6 files changed
+256 -42

No files matched your search

+48 -13
View File
@@ -2,9 +2,9 @@ package ca.bcit.comp2522.lab02;
/** /**
* Creature contains simple information regarding * Creature contains simple information regarding
* a creature such as its health and name. * a creature such as its health, name and date of birth.
* *
* @author Braeden Sowinski * @author Braeden Sowinski, Nicolas Agostini, Trishaan Shetty
* @version 1.0.0 * @version 1.0.0
*/ */
public class Creature { public class Creature {
@@ -17,8 +17,8 @@ public class Creature {
private int health; private int health;
/* /**
* validateName ensures input name is not empty. * validateName ensures input name is not null or empty.
* *
* @param name input * @param name input
* @throws IllegalArgumentException * @throws IllegalArgumentException
@@ -26,12 +26,12 @@ public class Creature {
private static void validateName(final String name) private static void validateName(final String name)
throws IllegalArgumentException throws IllegalArgumentException
{ {
if (name.isEmpty()) { if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("Name cannot be null or empty"); throw new IllegalArgumentException("Name cannot be null or empty");
} }
} }
/* /**
* validateHealth ensures input health is within valid range * validateHealth ensures input health is within valid range
* of MAX_HEALTH and MIN_HEALTH * of MAX_HEALTH and MIN_HEALTH
* *
@@ -46,7 +46,7 @@ public class Creature {
} }
} }
/* /**
* validateDateOfBirth ensures the date of birth * validateDateOfBirth ensures the date of birth
* is not in the future. * is not in the future.
* *
@@ -68,7 +68,7 @@ public class Creature {
* @param dateOfBirth of creature * @param dateOfBirth of creature
* @param health of creature to start * @param health of creature to start
*/ */
Creature(final String name, public Creature(final String name,
final Date dateOfBirth, final Date dateOfBirth,
final int health) final int health)
{ {
@@ -100,10 +100,15 @@ public class Creature {
* *
* @param damage to be dealt * @param damage to be dealt
* @throws DamageException if damage is negative * @throws DamageException if damage is negative
* @throws RuntimeException if the creature is not alive
*/ */
protected void takeDamage(final int damage) public void takeDamage(final int damage)
throws DamageException throws DamageException, RuntimeException
{ {
if (!this.isAlive()){
throw new RuntimeException("The creature is not alive.");
}
if (damage < NO_HEALTH) { if (damage < NO_HEALTH) {
throw new DamageException("Damage cannot be negative"); throw new DamageException("Damage cannot be negative");
} }
@@ -153,8 +158,19 @@ public class Creature {
* *
* @return details of the creature. * @return details of the creature.
*/ */
protected String getDetails() { public String getDetails() {
return "Name: " + this.name; 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,26 @@ public class Creature {
* *
* @return name of the creature * @return name of the creature
*/ */
protected String getName() { public String getName() {
return this.name; return this.name;
} }
/**
* Prints the details of the creature.
*/
public void printDetails() {
System.out.println(getDetails());
}
/**
* Prints the details of the creature.
* @return health value of the creature
*/
public int getHealth() { return this.health;}
/**
* Returns the date of birth of the creature.
* @return Date of Birth of the creature.
*/
public Date getDateOfBirth() { return this.dateOfBirth;}
} }
+16 -6
View File
@@ -13,7 +13,7 @@ public final class Date {
public static final int MAX_YEAR = 2025; public static final int MAX_YEAR = 2025;
// Domain of dates // 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 MIN_DAY = 1;
private static final int CURRENT_DAY = 15; private static final int CURRENT_DAY = 15;
private static final int CURRENT_MONTH = 9; private static final int CURRENT_MONTH = 9;
@@ -144,8 +144,9 @@ public final class Date {
} }
} }
/* /**
validateYear checks input year is in valid range validateYear checks input year is in valid range
@param year to be validated
*/ */
private static void validateYear(final int year) private static void validateYear(final int year)
throws IllegalArgumentException throws IllegalArgumentException
@@ -156,8 +157,9 @@ public final class Date {
} }
} }
/* /**
validateMonth checks input month is in valid range validateMonth checks input month is in valid range
@param month to be validated
*/ */
private static void validateMonth(final int month) private static void validateMonth(final int month)
throws IllegalArgumentException throws IllegalArgumentException
@@ -168,9 +170,12 @@ public final class Date {
} }
} }
/* /**
validateDay ensures the day given is less than the max day * validateDay ensures the day given is less than the max day
calculated by daysInMonth and greater than the MIN_DAY * calculated by daysInMonth and greater than the MIN_DAY
* @param day
* @param month
* @param year
*/ */
private static void validateDay( private static void validateDay(
final int day, final int day,
@@ -385,6 +390,11 @@ public final class Date {
return day; return day;
} }
/**
* isInFuture validates if the Date instance is from a date greater than
* MAX_YEAR, which corresponds to the current year
* @return if the date is in the future
*/
public boolean isInFuture() { public boolean isInFuture() {
if (this.year > MAX_YEAR) { if (this.year > MAX_YEAR) {
+29 -8
View File
@@ -38,14 +38,17 @@ public class Dragon extends Creature {
* @param health int * @param health int
* @param firePower int * @param firePower int
*/ */
Dragon( public Dragon(
final String name, final String name,
final Date dateOfBirth, final Date dateOfBirth,
final int health, final int health,
final int firePower final int firePower
) { )
{
super(name, dateOfBirth, health); super(name, dateOfBirth, health);
validateFirePower(firePower);
this.firePower = firePower; this.firePower = firePower;
} }
@@ -55,8 +58,20 @@ public class Dragon extends Creature {
* @return details of dragon * @return details of dragon
*/ */
@Override @Override
protected String getDetails() { public String getDetails() {
return "Name: " + this.getName() + " FirePower: " + this.firePower; 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();
} }
/** /**
@@ -65,17 +80,23 @@ public class Dragon extends Creature {
* *
* @param target Creature to deal damage to * @param target Creature to deal damage to
* @throws LowFirePowerException if not enough firePower * @throws LowFirePowerException if not enough firePower
* @throws RuntimeException if the dragon is not alive to accomplish the action
*/ */
protected void breatheFire(final Creature target) public void breatheFire(final Creature target)
throws LowFirePowerException throws LowFirePowerException, RuntimeException {
{
if (firePower > FIRE_POWER_USAGE) { if (!this.isAlive()) {
throw new RuntimeException("The dragon is not alive.");
}
if(firePower < FIRE_POWER_USAGE) {
throw new LowFirePowerException("Fire power too low"); throw new LowFirePowerException("Fire power too low");
} }
this.firePower -= FIRE_POWER_USAGE; this.firePower -= FIRE_POWER_USAGE;
target.takeDamage(FIRE_POWER_DAMAGE); target.takeDamage(FIRE_POWER_DAMAGE);
} }
/** /**
+22 -7
View File
@@ -2,7 +2,7 @@ package ca.bcit.comp2522.lab02;
/** /**
* Elf extends Creature and * Elf extends Creature and
* holds a mana value? Never heard of that * holds a mana value
* *
* @author Braeden Sowinski * @author Braeden Sowinski
* @version 1.0.0 * @version 1.0.0
@@ -15,7 +15,7 @@ public class Elf extends Creature {
private int mana; private int mana;
/* /**
* validateManaValue ensures a given value is within * validateManaValue ensures a given value is within
* the correct mana range * the correct mana range
* *
@@ -38,7 +38,7 @@ public class Elf extends Creature {
* @param health int * @param health int
* @param mana int * @param mana int
*/ */
Elf( public Elf(
final String name, final String name,
final Date dateOfBirth, final Date dateOfBirth,
final int health, final int health,
@@ -57,8 +57,17 @@ public class Elf extends Creature {
* @return details of elf * @return details of elf
*/ */
@Override @Override
protected String getDetails() { public String getDetails() {
return "Name: " + this.getName() + " Mana: " + this.mana; 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();
} }
/** /**
@@ -67,10 +76,15 @@ public class Elf extends Creature {
* *
* @param target Creature to deal damage to * @param target Creature to deal damage to
* @throws LowManaException if mana value is too low * @throws LowManaException if mana value is too low
* @throws RuntimeException if the Elf is not alive to accomplish the action
*/ */
protected void castSpell(final Creature target) public void castSpell(final Creature target)
throws LowManaException throws LowManaException, RuntimeException
{ {
if(!this.isAlive()){
throw new RuntimeException("The elf is not alive.");
}
if (this.mana < MANA_USAGE) { if (this.mana < MANA_USAGE) {
throw new LowManaException("Not enough mana to cast spell"); throw new LowManaException("Not enough mana to cast spell");
} }
@@ -78,6 +92,7 @@ public class Elf extends Creature {
this.mana -= MANA_USAGE; this.mana -= MANA_USAGE;
target.takeDamage(MANA_DAMAGE); target.takeDamage(MANA_DAMAGE);
} }
/** /**
+24 -8
View File
@@ -17,7 +17,7 @@ public class Orc extends Creature {
private int rage; private int rage;
/* /**
* validateRageValue ensures a given value is within * validateRageValue ensures a given value is within
* the correct rage range * the correct rage range
* *
@@ -40,7 +40,7 @@ public class Orc extends Creature {
* @param health int * @param health int
* @param rage int * @param rage int
*/ */
Orc( public Orc(
final String name, final String name,
final Date dateOfBirth, final Date dateOfBirth,
final int health, final int health,
@@ -59,21 +59,35 @@ public class Orc extends Creature {
* @return details of orc * @return details of orc
*/ */
@Override @Override
protected String getDetails() { public String getDetails() {
return "Name: " + this.getName() + " Rage: " + this.rage; 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();
} }
/** /**
* bezerk goes the orc when he is really mad. * berserk goes the orc when he is really mad.
* dealing damage to target Creatures with the * dealing damage to target Creatures with the
* potential for double damage. * potential for double damage.
* *
* @param target Creature to deal damage to * @param target Creature to deal damage to
* @throws LowRageException if rage value is too low * @throws LowRageException if rage value is too low
* @throws RuntimeException if the Orc is not alive to accomplish the action
*/ */
protected void bezerk(final Creature target) public void berserk(final Creature target)
throws LowRageException throws LowRageException, RuntimeException
{ {
if(!this.isAlive()){
throw new RuntimeException("The orc is not alive.");
}
this.rage += RAGE_INCREMENT; this.rage += RAGE_INCREMENT;
if (this.rage > MAX_RAGE_VALUE) { if (this.rage > MAX_RAGE_VALUE) {
@@ -85,7 +99,9 @@ public class Orc extends Creature {
} else if (this.rage > DAMAGE_THRESHOLD) { } else if (this.rage > DAMAGE_THRESHOLD) {
target.takeDamage(DAMAGE * DOUBLE); target.takeDamage(DAMAGE * DOUBLE);
} else { } else {
target.takeDamage(DOUBLE); target.takeDamage(DAMAGE);
} }
} }
} }
@@ -0,0 +1,117 @@
package ca.bcit.comp2522.lab02;
/**
* 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)
*/
public static void main(String[] args) {
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);
if (creatureDragon instanceof Dragon) {
System.out.println("creatureDragon is a Dragon.");
}
System.out.println("creatureDragon type: " + creatureDragon.getClass().getSimpleName());
if (creatureElf instanceof Elf) {
System.out.println("creatureElf is an Elf.");
}
System.out.println("creatureElf type: " + creatureElf.getClass().getSimpleName());
if (creatureOrc instanceof Orc) {
System.out.println("creatureOrc is an Orc.");
}
System.out.println("creatureOrc type: " + creatureOrc.getClass().getSimpleName());
creatureDragon.printDetails();
creatureElf.printDetails();
creatureOrc.printDetails();
final Dragon dragon = (Dragon) creatureDragon;
final Elf elf = (Elf) creatureElf;
final Orc orc = (Orc) creatureOrc;
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());
}
System.out.println("Final Stats:");
dragon.printDetails();
elf.printDetails();
orc.printDetails();
}
}