-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCashMachine.java
More file actions
97 lines (76 loc) · 2.24 KB
/
CashMachine.java
File metadata and controls
97 lines (76 loc) · 2.24 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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package rocks.zipcode.atm;
import rocks.zipcode.atm.bank.AccountData;
import rocks.zipcode.atm.bank.Bank;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* @author ZipCodeWilmington
*/
public class CashMachine {
private final Bank bank;
private AccountData accountData = null;
private String msg;
public CashMachine(Bank bank) {
this.bank = bank;
}
private Consumer<AccountData> update = data -> {
accountData = data;
};
public String getMsg(){ //method to return the error msg on line 92
return msg;
}
public void login(int id) {
tryCall(
() -> bank.getAccountById(id),
update
);
}
public void deposit(Float amount) {
public boolean userIsLoggedIn() { //method to validate if the log-in id is valid
//if account data is = null; means user is not logged in = false
//if account data is valid; true
if (accountData == null) {
return false;
} else {
return true;
}
if (accountData != null) {
tryCall(
() -> bank.deposit(accountData, amount),
update
);
}
}
public void withdraw(Float amount) {
if (accountData != null) {
tryCall(
() -> bank.withdraw(accountData, amount),
update
);
}
}
public void exit() {
if (accountData != null) {
accountData = null;
}
}
@Override
public String toString() {
return accountData != null ? accountData.toString() : "Log-in with your account ID.";
}
private <T> void tryCall(Supplier<ActionResult<T> > action, Consumer<T> postAction) {
msg = "";
try {
ActionResult<T> result = action.get();
if (result.isSuccess()) {
T data = result.getData();
postAction.accept(data);
} else {
String errorMessage = result.getErrorMessage();
throw new RuntimeException(errorMessage);
}
} catch (Exception e) {
msg = "Error: " + e.getMessage();
}
}
}