-
Notifications
You must be signed in to change notification settings - Fork 0
/
Soldier.java
71 lines (56 loc) · 1.8 KB
/
Soldier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.io.Serializable;
public abstract class Soldier implements Serializable {
private final int value;
private int experience;
private int strength;
private final General general;
private final String type;
public Soldier(int value, General general, String type) {
this.value = value;
this.experience = 1;
this.strength = value;
this.general = general;
this.type = type;
general.addSoldier(this);
}
public final int getValue() {
return value;
}
public final int getExperience() {
return experience;
}
public void setStrength(int strength) {
setStrengthException(strength);
this.strength = strength;
}
private void setStrengthException(int strength){ if(strength < getExperience() * getValue()) throw new IllegalArgumentException();}
public final int getStrength() {
return strength;
}
public General getGeneral() {
return general;
}
public final String getType() {return type; }
public void setExperience(int experience) {
setExperienceError(experience);
this.experience = experience;
}
private void setExperienceError(int experience){
if(experience < 0) throw new IllegalArgumentException();
}
public void addExperience(){
setExperience(getExperience()+1);
}
public Boolean canAdvance(){
return getExperience() >= getValue() * 5;
}
public final Boolean isDead(){
return this.getExperience() == 0;
}
public void executeDeath(Boolean isDead){
if(isDead){
getGeneral().removeSoldier(this);
}
}
protected abstract void executeAdvance(Boolean canAdvance);
}