-
Notifications
You must be signed in to change notification settings - Fork 68
/
chapter13-contract.jsh
38 lines (31 loc) · 968 Bytes
/
chapter13-contract.jsh
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
// To starts, run jshell --enable-preview which is a program able to interpret Java syntax
// then cut and paste the following lines to see how it works
// To exit jshell type /exit
// # Contract
public enum UnitKind {
MARINE(10), FLAME_THROWER(8)
;
private final int maxPower;
private UnitKind(int maxPower) {
this.maxPower = maxPower;
}
}
public class MilitaryUnit {
private final UnitKind kind;
private final int power;
private IntUnaryOperator bonus;
public MilitaryUnit(UnitKind kind, int power) {
this.kind = Objects.requireNonNull(kind);
if (power < 0 || power >= kind.maxPower) {
throw new IllegalArgumentException("invalid power " + power);
}
this.power = power;
this.bonus = x -> x;
}
public void bonus(IntUnaryOperator bonus) {
this.bonus = Objects.requireNonNull(bonus);
}
public int fightingPower() {
return Math.max(0, Math.min(unit.maxPower, bonus.applyAsInt(power)));
}
}