lab02 src code, no tests or main
This commit is contained in:
11 files changed
+360
-42
No files matched your search
@@ -138,11 +138,31 @@ public class Creature {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* getAgeYears calculates the age years
|
||||
* from Date.MAX_YEAR which is current year
|
||||
*
|
||||
* @return age in years
|
||||
*/
|
||||
protected int getAgeYears() {
|
||||
return 0;
|
||||
return Date.MAX_YEAR - this.dateOfBirth.getYear();
|
||||
}
|
||||
|
||||
/**
|
||||
* getDetails of the creature
|
||||
*
|
||||
* @return details of the creature.
|
||||
*/
|
||||
protected String getDetails() {
|
||||
return "";
|
||||
return "Name: " + this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* getName of creature
|
||||
*
|
||||
* @return name of the creature
|
||||
*/
|
||||
protected String getName() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,12 @@
|
||||
package ca.bcit.comp2522.lab02;
|
||||
|
||||
/**
|
||||
* DamageException is an unchecked exception
|
||||
* for when a given damage value is invalid.
|
||||
*
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class DamageException extends RuntimeException {
|
||||
public DamageException(String message) {
|
||||
super(message);
|
||||
|
||||
@@ -10,9 +10,10 @@ package ca.bcit.comp2522.lab02;
|
||||
* @version 1.0.0
|
||||
*/
|
||||
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 MAX_YEAR = 2025;
|
||||
private static final int MIN_DAY = 1;
|
||||
private static final int CURRENT_DAY = 15;
|
||||
private static final int CURRENT_MONTH = 9;
|
||||
|
||||
@@ -10,12 +10,17 @@ package ca.bcit.comp2522.lab02;
|
||||
public class Dragon extends Creature {
|
||||
private static final int MAX_FIRE_POWER = 100;
|
||||
private static final int MIN_FIRE_POWER = 0;
|
||||
private static final int FIRE_POWER_USAGE = 10;
|
||||
private static final int FIRE_POWER_DAMAGE = 20;
|
||||
|
||||
private final int firePower;
|
||||
private int firePower;
|
||||
|
||||
/*
|
||||
validateFirePower ensures input firepower is in valid
|
||||
range of MAX_FIRE_POWER and MIN_FIRE_POWER.
|
||||
/**
|
||||
* validateFirePower ensures a given value is within
|
||||
* the correct firePower range
|
||||
*
|
||||
* @param firePower to verify
|
||||
* @throws IllegalArgumentException if firePower is outside valid range
|
||||
*/
|
||||
private static void validateFirePower(int firePower)
|
||||
throws IllegalArgumentException {
|
||||
@@ -34,18 +39,56 @@ public class Dragon extends Creature {
|
||||
* @param firePower int
|
||||
*/
|
||||
Dragon(
|
||||
final String name,
|
||||
final Date dateOfBirth,
|
||||
final int health,
|
||||
final int firePower
|
||||
final String name,
|
||||
final Date dateOfBirth,
|
||||
final int health,
|
||||
final int firePower
|
||||
) {
|
||||
super(name, dateOfBirth, health);
|
||||
|
||||
this.firePower = firePower;
|
||||
}
|
||||
|
||||
/**
|
||||
* getDetails of Dragon with firepower
|
||||
*
|
||||
* @return details of dragon
|
||||
*/
|
||||
@Override
|
||||
protected String getDetails() {
|
||||
return "a";
|
||||
return "Name: " + this.getName() + " FirePower: " + this.firePower;
|
||||
}
|
||||
|
||||
/**
|
||||
* breathFire onto Creatures to deal damage
|
||||
* and reduces current firePower value
|
||||
*
|
||||
* @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");
|
||||
}
|
||||
|
||||
this.firePower -= FIRE_POWER_USAGE;
|
||||
|
||||
target.takeDamage(FIRE_POWER_DAMAGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* restoreFirePower by a given value and is
|
||||
* maxed by MAX_FIRE_POWER
|
||||
*
|
||||
* @param amount to increase firePower
|
||||
*/
|
||||
protected void restoreFirePower(final int amount) {
|
||||
this.firePower += amount;
|
||||
|
||||
if (this.firePower > MAX_FIRE_POWER) {
|
||||
this.firePower = MAX_FIRE_POWER;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package ca.bcit.comp2522.lab02;
|
||||
|
||||
/**
|
||||
* Elf extends Creature and
|
||||
* holds a mana value? Never heard of that
|
||||
*
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Elf extends Creature {
|
||||
private static final int MIN_MANA_VALUE = 0;
|
||||
private static final int MAX_MANA_VALUE = 50;
|
||||
private static final int MANA_DAMAGE = 10;
|
||||
private static final int MANA_USAGE = 5;
|
||||
|
||||
private int mana;
|
||||
|
||||
/*
|
||||
* validateManaValue ensures a given value is within
|
||||
* the correct mana range
|
||||
*
|
||||
* @param value to verify
|
||||
* @throws IllegalArgumentException if mana is outside valid range
|
||||
*/
|
||||
private static void validateManaValue(final int value)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
if (value < MIN_MANA_VALUE || value > MAX_MANA_VALUE) {
|
||||
throw new IllegalArgumentException("Invalid mana value: " + value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Elf constructor
|
||||
*
|
||||
* @param name String
|
||||
* @param dateOfBirth Date
|
||||
* @param health int
|
||||
* @param mana int
|
||||
*/
|
||||
Elf(
|
||||
final String name,
|
||||
final Date dateOfBirth,
|
||||
final int health,
|
||||
final int mana
|
||||
) {
|
||||
super(name, dateOfBirth, health);
|
||||
|
||||
validateManaValue(mana);
|
||||
|
||||
this.mana = mana;
|
||||
}
|
||||
|
||||
/**
|
||||
* getDetails of elf with Mana
|
||||
*
|
||||
* @return details of elf
|
||||
*/
|
||||
@Override
|
||||
protected String getDetails() {
|
||||
return "Name: " + this.getName() + " Mana: " + this.mana;
|
||||
}
|
||||
|
||||
/**
|
||||
* castSpell onto target Creatures to deal damage
|
||||
* and reduces current mana value
|
||||
*
|
||||
* @param target Creature to deal damage to
|
||||
* @throws LowManaException if mana value is too low
|
||||
*/
|
||||
protected void castSpell(final Creature target)
|
||||
throws LowManaException
|
||||
{
|
||||
if (this.mana < MANA_USAGE) {
|
||||
throw new LowManaException("Not enough mana to cast spell");
|
||||
}
|
||||
|
||||
this.mana -= MANA_USAGE;
|
||||
|
||||
target.takeDamage(MANA_DAMAGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* restoreMana by a given value and is maxed
|
||||
* to MAX_MANA_VALUE
|
||||
*
|
||||
* @param amount to increase mana
|
||||
*/
|
||||
protected void restoreMana(final int amount) {
|
||||
this.mana += amount;
|
||||
|
||||
if (this.mana > MAX_MANA_VALUE) {
|
||||
this.mana = MAX_MANA_VALUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,12 @@
|
||||
package ca.bcit.comp2522.lab02;
|
||||
|
||||
/**
|
||||
* HealingException is an unchecked exception
|
||||
* for when a given healing value is invalid.
|
||||
*
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class HealingException extends RuntimeException {
|
||||
public HealingException(String message) {
|
||||
super(message);
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package ca.bcit.comp2522.lab02;
|
||||
|
||||
/**
|
||||
* LowFirePowerException is a checked exception
|
||||
* when firePower is too low to deal damage.
|
||||
*
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class LowFirePowerException extends Exception {
|
||||
public LowFirePowerException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package ca.bcit.comp2522.lab02;
|
||||
|
||||
/**
|
||||
* LowManaException is a checked exception
|
||||
* when mana is too low to deal damage.
|
||||
*
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class LowManaException extends Exception {
|
||||
public LowManaException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package ca.bcit.comp2522.lab02;
|
||||
|
||||
/**
|
||||
* LowRageException is a checked exception
|
||||
* when rage is too low to deal damage.
|
||||
*
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class LowRageException extends Exception {
|
||||
public LowRageException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package ca.bcit.comp2522.lab02;
|
||||
|
||||
/**
|
||||
* Orc extends Creature and
|
||||
* holds a rage value. Orcs need anger management.
|
||||
*
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Orc extends Creature {
|
||||
private static final int MIN_RAGE_VALUE = 0;
|
||||
private static final int MAX_RAGE_VALUE = 30;
|
||||
private static final int RAGE_INCREMENT = 5;
|
||||
private static final int DAMAGE_THRESHOLD = 20;
|
||||
private static final int DAMAGE = 15;
|
||||
private static final int DOUBLE = 2;
|
||||
|
||||
private int rage;
|
||||
|
||||
/*
|
||||
* validateRageValue ensures a given value is within
|
||||
* the correct rage range
|
||||
*
|
||||
* @param value to verify
|
||||
* @throws IllegalArgumentException if rage is outside valid range
|
||||
*/
|
||||
private static void validateRageValue(final int value)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
if (value < MIN_RAGE_VALUE || value > MAX_RAGE_VALUE) {
|
||||
throw new IllegalArgumentException("Invalid rage value: " + value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Orc constructor
|
||||
*
|
||||
* @param name String
|
||||
* @param dateOfBirth Date
|
||||
* @param health int
|
||||
* @param rage int
|
||||
*/
|
||||
Orc(
|
||||
final String name,
|
||||
final Date dateOfBirth,
|
||||
final int health,
|
||||
final int rage
|
||||
) {
|
||||
super(name, dateOfBirth, health);
|
||||
|
||||
validateRageValue(rage);
|
||||
|
||||
this.rage = rage;
|
||||
}
|
||||
|
||||
/**
|
||||
* getDetails of orc with rage
|
||||
*
|
||||
* @return details of orc
|
||||
*/
|
||||
@Override
|
||||
protected String getDetails() {
|
||||
return "Name: " + this.getName() + " Rage: " + this.rage;
|
||||
}
|
||||
|
||||
/**
|
||||
* bezerk goes the orc when he is really mad.
|
||||
* dealing damage to target Creatures with the
|
||||
* potential for double damage.
|
||||
*
|
||||
* @param target Creature to deal damage to
|
||||
* @throws LowRageException if rage value is too low
|
||||
*/
|
||||
protected void bezerk(final Creature target)
|
||||
throws LowRageException
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user