Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions src/main/java/object/mesheikbrown/RPGCharacter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package object.mesheikbrown;

public class RPGCharacter {
private String name;
private int health;
private int maxHealth;
private int level;
private int mana;
private boolean alive;

//The constructor runs when a new RPGCharacter object is created.
//It sets all the starting values for the character.
//It also checks if the values are valid using validation rules.
public RPGCharacter(String name, int health, int maxHealth, int level, int mana, boolean alive){
this.name = name;
this.health = health;
this.maxHealth = maxHealth;
this.level = level;
this.mana = mana;
this.alive = alive;

// Validation: name cannot be empty
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank.");
}

// Validation: max health must be positive
if (maxHealth <= 0) {
throw new IllegalArgumentException("Max health must be greater than 0.");
}

// Validation: health must be between 0 and maxHealth
if (health < 0 || health > maxHealth) {
throw new IllegalArgumentException("Health must be between 0 and maxHealth.");
}

// Validation: level must be at least 1
if (level <= 0) {
throw new IllegalArgumentException("Level must be greater than 0.");
}

// Validation: mana cannot be negative
if (mana < 0) {
throw new IllegalArgumentException("Mana cannot be negative.");
}

}

//Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getMaxHealth() {
return maxHealth;
}
public void setMaxHealth(int maxHealth) {
this.maxHealth = maxHealth;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getMana() {
return mana;
}
public void setMana(int mana) {
this.mana = mana;
}
public boolean isAlive() {
return alive;
}
public void setAlive(boolean alive) {
this.alive = alive;
}

// Custom Method 1
public void takeDamage(int amount) {

if (amount <= 0) {
throw new IllegalArgumentException("Damage must be greater than 0.");
}

health -= amount;

if (health <= 0) {
health = 0;
alive = false;
}
}

// Custom Method 2
public void heal(int amount) {

if (amount <= 0) {
throw new IllegalArgumentException("Heal amount must be greater than 0.");
}

if (!alive) {
return;
}

health += amount;

if (health > maxHealth) {
health = maxHealth;
}
}

// Custom Method 3
public void levelUp() {

level++;
maxHealth += 10;
mana += 5;

health = maxHealth;
}

}//ends class
202 changes: 202 additions & 0 deletions src/test/java/object/mesheikbrown/RPGCharacterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package object.mesheikbrown;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

/*
This class tests the RPGCharacter class.

The purpose of this file is to make sure every part of the RPGCharacter
class behaves correctly.

Each test follows the AAA pattern:

Arrange = set up the objects
Act = run the method we want to test
Assert = check if the result is correct
*/

public class RPGCharacterTest {

@Test
public void constructorHappyPath_setsFieldsCorrectly() {

// Arrange
// Create a new RPGCharacter object with valid values
RPGCharacter hero = new RPGCharacter("Knight", 80, 100, 1, 40, true);

// Act
// Retrieve values from the object using getters
String name = hero.getName();
int health = hero.getHealth();
int level = hero.getLevel();

// Assert
// Check if the values we retrieved match what we expect
assertEquals("Knight", name);
assertEquals(80, health);
assertEquals(1, level);

// Check that the character is alive
assertTrue(hero.isAlive());
}

@Test
public void constructor_invalidHealth_throwsException() {

// Arrange / Act / Assert
// We expect an exception because health cannot be greater than maxHealth
assertThrows(IllegalArgumentException.class, () ->
new RPGCharacter("Knight", 200, 100, 1, 40, true)
);
}

@Test
public void setters_updateValuesCorrectly() {

// Arrange
// Create a starting character
RPGCharacter hero = new RPGCharacter("Knight", 80, 100, 1, 40, true);

// Act
// Change the values using setter methods
hero.setName("Mage");
hero.setHealth(70);
hero.setLevel(2);

// Assert
// Verify the setters correctly updated the values
assertEquals("Mage", hero.getName());
assertEquals(70, hero.getHealth());
assertEquals(2, hero.getLevel());
}

@Test
public void takeDamage_reducesHealth() {

// Arrange
// Character starts with 80 health
RPGCharacter hero = new RPGCharacter("Knight", 80, 100, 1, 40, true);

// Act
// Apply 30 damage
hero.takeDamage(30);

// Assert
// Health should decrease from 80 → 50
assertEquals(50, hero.getHealth());
}

@Test
public void takeDamage_killsCharacter() {

// Arrange
// Character starts with low health
RPGCharacter hero = new RPGCharacter("Knight", 20, 100, 1, 40, true);

// Act
// Damage is larger than remaining health
hero.takeDamage(25);

// Assert
// Health should drop to 0
assertEquals(0, hero.getHealth());

// Character should now be dead
assertFalse(hero.isAlive());
}

@Test
public void heal_increasesHealth() {

// Arrange
// Character starts damaged
RPGCharacter hero = new RPGCharacter("Knight", 50, 100, 1, 40, true);

// Act
// Heal the character
hero.heal(20);

// Assert
// Health should increase
assertEquals(70, hero.getHealth());
}

@Test
public void heal_doesNotExceedMaxHealth() {

// Arrange
// Character is almost at max health
RPGCharacter hero = new RPGCharacter("Knight", 95, 100, 1, 40, true);

// Act
// Healing would normally go over the limit
hero.heal(20);

// Assert
// Health should stop at maxHealth (100)
assertEquals(100, hero.getHealth());
}

@Test
public void levelUp_increasesStats() {

// Arrange
// Create a character at level 1
RPGCharacter hero = new RPGCharacter("Knight", 80, 100, 1, 40, true);

// Act
// Level up the character
hero.levelUp();

// Assert

// Level should increase
assertEquals(2, hero.getLevel());

// Max health increases by 10
assertEquals(110, hero.getMaxHealth());

// Mana increases by 5
assertEquals(45, hero.getMana());

// Health resets to the new max health
assertEquals(110, hero.getHealth());
}
@Test
public void takeDamage_whenNotFatal_characterStaysAlive() {

// Arrange
RPGCharacter hero = new RPGCharacter("Knight", 80, 100, 1, 40, true);

// Act
hero.takeDamage(10);

// Assert
assertEquals(70, hero.getHealth());
assertTrue(hero.isAlive());
}

@Test
public void setters_updateEverySingleField() {

// Arrange
RPGCharacter hero = new RPGCharacter("Knight", 80, 100, 1, 40, true);

// Act
hero.setName("Rogue");
hero.setHealth(65);
hero.setMaxHealth(120);
hero.setLevel(3);
hero.setMana(75);
hero.setAlive(false);

// Assert
assertEquals("Rogue", hero.getName());
assertEquals(65, hero.getHealth());
assertEquals(120, hero.getMaxHealth());
assertEquals(3, hero.getLevel());
assertEquals(75, hero.getMana());
assertFalse(hero.isAlive());
}
}