-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNormalState.java
More file actions
46 lines (40 loc) · 1.13 KB
/
NormalState.java
File metadata and controls
46 lines (40 loc) · 1.13 KB
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
package designpattern.behavioral_pattern.state.v2;
/**
* <p>Description: 正常状态:具体状态类</p>
*
* @author Cui Kaihui
* @date 2019/6/24 22:20
*/
public class NormalState extends AccountState {
public NormalState(Account acc) {
this.acc = acc;
}
public NormalState(AccountState state) {
this.acc = state.acc;
}
@Override
public void deposit(double amount) {
acc.setBalance(acc.getBalance() + amount);
stateCheck();
}
@Override
public void withdraw(double amount) {
acc.setBalance(acc.getBalance() - amount);
stateCheck();
}
@Override
public void computeInterest() {
System.out.println("正常状态,无须支付利息!");
}
//状态转换
@Override
public void stateCheck() {
if (acc.getBalance() > -2000 && acc.getBalance() <= 0) {
acc.setState(new OverdraftState(this));
} else if (acc.getBalance() == -2000) {
acc.setState(new RestrictedState(this));
} else if (acc.getBalance() < -2000) {
System.out.println("操作受限!");
}
}
}