forked from madonawambua/Bank-Account-Draft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATM.java
More file actions
executable file
·251 lines (185 loc) · 6.75 KB
/
ATM.java
File metadata and controls
executable file
·251 lines (185 loc) · 6.75 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package Root;
import java.util.Scanner;
/**
* @author syombua
*
*/
public class ATM {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Bank theBank = new Bank("Bank of Syombua");
User aUser = theBank.addUser("Madona", "Wambua", "1234");
//add checking accounts for users
Account newAccount = new Account("Checking", aUser, theBank);
aUser.addAccount(newAccount);
theBank.addAccount(newAccount);
User curUser;
while(true){
//stay in the login prompt until successful login
curUser = ATM.mainMenuPrompt(theBank, sc);
//stay in the main menu until user quits
ATM.printUserMenu(curUser, sc);
}
}
public static User mainMenuPrompt(Bank theBank, Scanner sc) {
// TODO Auto-generated method stub
String userID;
String pin;
User outhUser;
//prompt the user or the user id or pin until the correct one is reached
do{
System.out.printf("\n\nWelcome to %s\n\n ",theBank.getName());
System.out.print("Enter user ID: ");
userID = sc.nextLine();
System.out.print("Enter Pin: ");
pin = sc.nextLine();
//try to get the user object corresponding to the ID and the pin
outhUser = theBank.userLogin(userID, pin);
if(outhUser == null){
System.out.println("Incorrect user ID/Pin combination" + "Please try again");
}
}while(outhUser == null); // continue looping until successful log in
return outhUser;
}
public static void printUserMenu(User theUser, Scanner sc) {
//print a summary of the users accounts
theUser.printAccountsSummary();
int choice;
//user menu
do{
System.out.printf("Welcome %s What would you love to do?\n" , theUser.getFirstName());
System.out.println(" 1. Show account Tansaction history");
System.out.println(" 2. Withdraw");
System.out.println(" 3. Deposit");
System.out.println(" 4. Transfer");
System.out.println(" 5. Quit");
System.out.println();
System.out.println("Enter Choice: ");
choice = sc.nextInt();
if(choice <1 || choice> 5){
System.out.println("Invalicd choice. Please choose 1-5");
}
}while(choice <1 || choice>5);
//process choice
switch (choice){
case 1:
ATM.showTransHistory(theUser, sc);
break;
case 2:
ATM.withdrawlFunds(theUser,sc);
break;
case 3:
ATM.depositFunds(theUser,sc);
break;
case 4:
ATM.transferFunds(theUser,sc);
break;
}
//redisplay menu unless user wants to quit
if(choice != 5){
//ATM.printUserMenu(theUser, sc);
System.exit(1);
}
}
public static void showTransHistory(User theUser, Scanner sc) {
int theAcct ;
//get account whose transaction history to look at
do{
System.out.printf("Enter the number (1-%d) of the account\n"+"whose transactions you want to see:",theUser.numAccounts());
theAcct = sc.nextInt() -1;
if(theAcct <0 || theAcct >= theUser.numAccounts()){
System.out.println("Invalid Account: please try again");
}
}while(theAcct < 0|| theAcct >= theUser.numAccounts());
//print the transaction history
theUser.printAcctTransHistory(theAcct);
}
public static void transferFunds(User theUser, Scanner sc) {
int fromAcct;
int toAcct;
double amount;
double acctBal;
do{
System.out.printf("Enter the number (1 - %d of the account\n"+ "to Transfer from:",theUser.numAccounts());
fromAcct = sc.nextInt()-1;
if(fromAcct <0 || fromAcct >= theUser.numAccounts()){
System.out.println("Invalid Account: please try again");
}
}while(fromAcct < 0|| fromAcct >= theUser.numAccounts());
acctBal = theUser.getAcctBalance(fromAcct);
//get the account to Trasnfer to
do{
System.out.printf("Enter the number (1- %d) of the account\n" + "to transafer to:",theUser.numAccounts());
toAcct =sc.nextInt() - 1;
if(toAcct <0 || toAcct >= theUser.numAccounts()){
System.out.println("Invalid Account: please try again");
}
}while(toAcct < 0|| toAcct >= theUser.numAccounts());
do{
System.out.printf("Enter the amount to Transfer(max $%.02f :$",acctBal);
amount = sc.nextDouble();
if(amount<0){
System.out.println("Amount must be greater than Zero.");
} else if (amount > acctBal){
System.out.printf("Amount must not be greater than \n" + "balance of $%.02f\n",acctBal);
}
}while(amount < 0 || amount > acctBal);
//finally do the transfer
theUser.addAcctTransaction(fromAcct, -1*amount, String.format("Transfer to account %s",theUser.getAcctUUID(toAcct)));
theUser.addAcctTransaction(toAcct, amount, String.format("Transfer tp account %s",theUser.getAcctUUID(fromAcct)));
}
public static void withdrawlFunds(User theUser, Scanner sc) {
// TODO Auto-generated method stub
int fromAcct;
double amount;
double acctBal;
String memo;
do{
System.out.printf("Enter the number (1-%d )of the account\n"+"to withdraw from:",theUser.numAccounts());
fromAcct = sc.nextInt()-1;
if(fromAcct <0 || fromAcct >= theUser.numAccounts()){
System.out.println("Invalid Account: please try again");
}
}while(fromAcct < 0|| fromAcct >= theUser.numAccounts());
acctBal = theUser.getAcctBalance(fromAcct);
do{
System.out.printf("Enter the amount to Transfer(max $%.02f, args): $",acctBal);
amount = sc.nextDouble();
if(amount<0){
System.out.println("Amount must be greater than Zero");
} else if (amount > acctBal){
System.out.printf("Amount must not be greater than \n" + "balance of $%.02f\n",acctBal);
}
}while(amount < 0 || amount > acctBal);
sc.nextLine();
System.out.println("Enter a Memo: ");
memo = sc.nextLine();
theUser.addAcctTransaction(fromAcct, -1*amount, memo);
}
public static void depositFunds(User theUser, Scanner sc) {
// TODO Auto-generated method stub
int toAcct;
double amount;
double acctBal;
String memo;
do{
System.out.printf("Enter the number (1-%d )of the account\n"+ "to deposit in:",theUser.numAccounts());
toAcct = sc.nextInt()-1;
if(toAcct <0 || toAcct >= theUser.numAccounts()){
System.out.println("Invalid Account: please try again");
}
}while(toAcct < 0|| toAcct >= theUser.numAccounts());
acctBal = theUser.getAcctBalance(toAcct);
do{
System.out.printf("Enter the amount to Transfer(max $%.02f, args): $",acctBal);
amount = sc.nextDouble();
if(amount<0){
System.out.println("Amount must be greater than Zero");
}
}while(amount < 0);
sc.nextLine();
System.out.println("Enter a Memo: ");
memo = sc.nextLine();
theUser.addAcctTransaction(toAcct, amount, memo);
}
}