done
This commit is contained in:
61
src/Enemy.java
Normal file
61
src/Enemy.java
Normal file
@@ -0,0 +1,61 @@
|
||||
public abstract class Enemy implements Character {
|
||||
|
||||
private final String name;
|
||||
private int hitPoints;
|
||||
|
||||
protected Enemy(String name, int hitPoints) {
|
||||
if (name == null || name.isBlank()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Name must not be blank"
|
||||
);
|
||||
}
|
||||
|
||||
if (hitPoints <= 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Hit points must be positive"
|
||||
);
|
||||
}
|
||||
|
||||
this.name = name;
|
||||
this.hitPoints = hitPoints;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int getHitPoints() {
|
||||
return hitPoints;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean isDefeated() {
|
||||
return hitPoints == 0;
|
||||
}
|
||||
|
||||
protected final void takeDamage(int damage) {
|
||||
if (damage < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Damage must not be negative"
|
||||
);
|
||||
}
|
||||
|
||||
hitPoints = Math.max(
|
||||
0,
|
||||
hitPoints - damage
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return "%s{name='%s', hitPoints=%d, defeated=%s}"
|
||||
.formatted(
|
||||
getClass().getSimpleName(),
|
||||
name,
|
||||
hitPoints,
|
||||
isDefeated()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user