-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommand.java
More file actions
42 lines (38 loc) · 799 Bytes
/
Command.java
File metadata and controls
42 lines (38 loc) · 799 Bytes
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
class Command
{
enum Action
{
DEPOSIT, WITHDRAW
}
public Action action;
public int amount;
public boolean success;
public Command(Action action, int amount)
{
this.action = action;
this.amount = amount;
}
}
class Account2
{
public int balance;
public void process(Command c)
{
switch(c.action) {
case DEPOSIT:
balance += c.amount;
c.success = true;
break;
case WITHDRAW:
if (c.amount > balance) {
c.success = false;
} else {
balance -= c.amount;
c.success = true;
}
break;
default:
throw new IllegalStateException();
}
}
}