|
@@ -1,9 +1,12 @@
|
1
|
1
|
package rocks.zipcode.atm.bank;
|
2
|
2
|
|
|
3
|
+import javafx.scene.control.TextInputDialog;
|
3
|
4
|
import rocks.zipcode.atm.ActionResult;
|
4
|
5
|
|
5
|
6
|
import java.util.HashMap;
|
6
|
7
|
import java.util.Map;
|
|
8
|
+import java.util.Optional;
|
|
9
|
+import java.util.Scanner;
|
7
|
10
|
|
8
|
11
|
/**
|
9
|
12
|
* @author ZipCodeWilmington
|
|
@@ -11,42 +14,154 @@ import java.util.Map;
|
11
|
14
|
public class Bank {
|
12
|
15
|
|
13
|
16
|
private Map<Integer, Account> accounts = new HashMap<>();
|
|
17
|
+ private String name;
|
|
18
|
+ private String email;
|
|
19
|
+
|
|
20
|
+ public String getName() {
|
|
21
|
+ return name;
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ public void setName(String name) {
|
|
25
|
+ this.name = name;
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ public String getEmail() {
|
|
29
|
+ return email;
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ public void setEmail(String email) {
|
|
33
|
+ this.email = email;
|
|
34
|
+ }
|
14
|
35
|
|
15
|
36
|
public Bank() {
|
16
|
|
- accounts.put(1000, new BasicAccount(new AccountData(
|
17
|
|
- 1000, "Example 1", "example1@gmail.com", 500
|
18
|
|
- )));
|
|
37
|
+//
|
|
38
|
+// this.name = "No name available";
|
|
39
|
+// this.email = "No e-mail available";
|
|
40
|
+
|
|
41
|
+ accounts.put(1000, new BasicAccount(new AccountData(1000, "Example 1", "example1@gmail.com", 500)));
|
19
|
42
|
|
20
|
43
|
accounts.put(2000, new PremiumAccount(new AccountData(
|
21
|
|
- 2000, "Example 2", "example2@gmail.com", 200
|
22
|
|
- )));
|
|
44
|
+ 2000, "Example 2", "example2@gmail.com", 200)));
|
23
|
45
|
}
|
24
|
46
|
|
25
|
47
|
public ActionResult<AccountData> getAccountById(int id) {
|
26
|
48
|
Account account = accounts.get(id);
|
27
|
49
|
|
|
50
|
+ ActionResult result;
|
|
51
|
+
|
28
|
52
|
if (account != null) {
|
29
|
|
- return ActionResult.success(account.getAccountData());
|
|
53
|
+ result = ActionResult.success(account.getAccountData());
|
30
|
54
|
} else {
|
31
|
|
- return ActionResult.fail("No account with id: " + id + "\nTry account 1000 or 2000");
|
|
55
|
+ result = null;
|
|
56
|
+ ActionResult.fail("Account ID " + id + " does not exist. Please create new account.");
|
32
|
57
|
}
|
|
58
|
+
|
|
59
|
+ return result;
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ public ActionResult<AccountData> createAccountById(int id) {
|
|
63
|
+ Account account = accounts.get(id);
|
|
64
|
+
|
|
65
|
+ ActionResult result;
|
|
66
|
+
|
|
67
|
+ if (account == null) {
|
|
68
|
+
|
|
69
|
+ Optional<String> newName = ActionResult.getInfo("Set Name", "Enter name: ");
|
|
70
|
+ Optional<String> newEmail = ActionResult.getInfo("Set E-mail", "Enter e-mail: ");
|
|
71
|
+ Optional<String> initialDeposit = ActionResult.getInfo("Initial Deposit", "Enter initial deposit: ");
|
|
72
|
+
|
|
73
|
+ // Fetch user input
|
|
74
|
+ AccountData accountData = new AccountData(id, newName.get(), newEmail.get(), Integer.parseInt(initialDeposit.get()));
|
|
75
|
+ Account newAccount = new Account(accountData);
|
|
76
|
+
|
|
77
|
+ accounts.put(id, newAccount);
|
|
78
|
+
|
|
79
|
+ result = ActionResult.success(newAccount.getAccountData());
|
|
80
|
+ } else {
|
|
81
|
+ result = null;
|
|
82
|
+ ActionResult.fail("Account ID " + id + " already exists. Please try again.");
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ return result;
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ public ActionResult<AccountData> editAccountName1(int id) {
|
|
89
|
+ Account account = accounts.get(id);
|
|
90
|
+ AccountData accountInfo = account.getAccountData();
|
|
91
|
+
|
|
92
|
+ Optional<String> newName = ActionResult.getInfo("Edit name", "Enter new name: ");
|
|
93
|
+
|
|
94
|
+ AccountData accountData = new AccountData(id, newName.get(), accountInfo.getEmail(), accountInfo.getBalance());
|
|
95
|
+ Account newAccount = new Account(accountData);
|
|
96
|
+
|
|
97
|
+ accounts.put(id, newAccount);
|
|
98
|
+
|
|
99
|
+ return ActionResult.success(newAccount.getAccountData());
|
33
|
100
|
}
|
34
|
101
|
|
35
|
|
- public ActionResult<AccountData> deposit(AccountData accountData, int amount) {
|
36
|
|
- Account account = accounts.get(accountData.getId());
|
37
|
|
- account.deposit(amount);
|
|
102
|
+// public ActionResult<AccountData> editAccountName(String name) {
|
|
103
|
+//
|
|
104
|
+// AccountData accountData = new AccountData(55, name, "akjsdkas@kasd.com", 500);
|
|
105
|
+//
|
|
106
|
+// Account account = new Account(accountData);
|
|
107
|
+//
|
|
108
|
+// accounts.put(55, account);
|
|
109
|
+//
|
|
110
|
+// return ActionResult.success(account.getAccountData());
|
|
111
|
+// }
|
|
112
|
+
|
|
113
|
+ public ActionResult<AccountData> editAccountEmail(int id) {
|
|
114
|
+ Account account = accounts.get(id);
|
|
115
|
+ AccountData accountInfo = account.getAccountData();
|
38
|
116
|
|
39
|
|
- return ActionResult.success(account.getAccountData());
|
|
117
|
+ Optional<String> newEmail = ActionResult.getInfo("Edit E-mail", "Enter new e-mail: ");
|
|
118
|
+
|
|
119
|
+ AccountData accountData = new AccountData(id, accountInfo.getName(), newEmail.get(), accountInfo.getBalance());
|
|
120
|
+ Account newAccount = new Account(accountData);
|
|
121
|
+
|
|
122
|
+ accounts.put(id, newAccount);
|
|
123
|
+
|
|
124
|
+ return ActionResult.success(newAccount.getAccountData());
|
40
|
125
|
}
|
41
|
126
|
|
42
|
|
- public ActionResult<AccountData> withdraw(AccountData accountData, int amount) {
|
43
|
|
- Account account = accounts.get(accountData.getId());
|
|
127
|
+ public ActionResult<AccountData> deposit(int id) {
|
|
128
|
+ Account account = accounts.get(id);
|
|
129
|
+ AccountData accountInfo = account.getAccountData();
|
|
130
|
+
|
|
131
|
+ Optional<String> depositAmount = ActionResult.getInfo("Deposit into Account", "Enter amount to deposit: ");
|
|
132
|
+
|
|
133
|
+ AccountData accountData = new AccountData(id, accountInfo.getName(), accountInfo.getEmail(), accountInfo.getBalance() + Integer.parseInt(depositAmount.get()));
|
|
134
|
+ Account newAccount = new Account(accountData);
|
|
135
|
+
|
|
136
|
+ accounts.put(id, newAccount);
|
|
137
|
+
|
|
138
|
+ return ActionResult.success(newAccount.getAccountData());
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ public ActionResult<AccountData> withdraw(int id) {
|
|
142
|
+ Account account = accounts.get(id);
|
|
143
|
+ AccountData accountInfo = account.getAccountData();
|
|
144
|
+
|
|
145
|
+ Optional<String> withdrawAmount = ActionResult.getInfo("Withdraw from Account", "Enter amount to withdraw: ");
|
|
146
|
+
|
|
147
|
+ int amount = Integer.parseInt(withdrawAmount.get());
|
|
148
|
+
|
44
|
149
|
boolean ok = account.withdraw(amount);
|
45
|
150
|
|
|
151
|
+ ActionResult result;
|
|
152
|
+
|
46
|
153
|
if (ok) {
|
47
|
|
- return ActionResult.success(account.getAccountData());
|
|
154
|
+ AccountData accountData = new AccountData(id, accountInfo.getName(), accountInfo.getEmail(), accountInfo.getBalance() - Integer.parseInt(withdrawAmount.get()));
|
|
155
|
+ Account newAccount = new Account(accountData);
|
|
156
|
+
|
|
157
|
+ accounts.put(id, newAccount);
|
|
158
|
+
|
|
159
|
+ result = ActionResult.success(newAccount.getAccountData());
|
48
|
160
|
} else {
|
49
|
|
- return ActionResult.fail("Withdraw failed: " + amount + ". Account has: " + account.getBalance());
|
|
161
|
+ result = null;
|
|
162
|
+ ActionResult.fail("Withdraw failed: " + amount + ". Account has: " + account.getBalance());
|
50
|
163
|
}
|
|
164
|
+
|
|
165
|
+ return result;
|
51
|
166
|
}
|
52
|
167
|
}
|