-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathATM.java
More file actions
206 lines (200 loc) · 8.29 KB
/
ATM.java
File metadata and controls
206 lines (200 loc) · 8.29 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
import java.util.*;//Scanner
import java.lang.*;
public class ATM{
public static void main(String[]args){
//init Scanner
Scanner sc = new Scanner(System.in);
//init bank
Bank theBank = new Bank("State Bank Of India");
//add user ,whichalso creates a savings account
User aUser = theBank.addUser("Aarsh","Dhokai","1111");
//add a checking account for our user
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 main menu until user quits
ATM.printUserMenu(curUser,sc);
}
}
public static User mainMenuPrompt(Bank theBank,Scanner sc){
//init
String userID;
String pin;
User authUser;
//prompt the user for user id and pincombo until a correct one is reached
do{
System.out.println("\n\nWelcome to "+theBank.getName()+"\n\n");
System.out.print("Enter User ID : ");
userID = sc.nextLine();
System.out.println();
System.out.print("Enter Pin : ");
pin = sc.nextLine();
//try to get the user object corresponding to th ID and pin Combo
authUser = theBank.userLogin(userID,pin);
if(authUser == null){
System.out.println("incorrect User ID/Pin, Please try again");
}
}while(authUser==null);//continue login until successful login
return authUser;
}
public static void printUserMenu(User theUser,Scanner sc){
//print a summary of user's account
theUser.printAccountSummary();
//init choice
int choice;
//user menu
do{
System.out.println("Welcome "+theUser.getFirstName()+" What would you like to do");
System.out.println("1>Show Transaction History");
System.out.println("2>withdraw");
System.out.println("3>Deposite");
System.out.println("4>Transfer");
System.out.println("5>Quit");
System.out.println();
System.out.println("Enter Your Choice : ");
choice = sc.nextInt();
if(choice<1 || choice>5){
System.out.println("Invalid Choice"+" Please choose between 1-5 ");
}
}while(choice<1 || choice>5);
//process the choice
switch(choice){
case 1:
ATM.showTransactionHistory(theUser,sc);
break;
case 2:
ATM.withdrawFunds(theUser,sc);
break;
case 3:
ATM.depositeFunds(theUser,sc);
break;
case 4:
ATM.transferFunds(theUser,sc);
break;
case 5:
//gobble up rest of previous input
sc.nextLine();
break;
}
//redisplay this menu unless user quit
if(choice != 5){
ATM.printUserMenu(theUser,sc);
}
}
public static void showTransactionHistory(User theUser,Scanner sc){
int theAct;
//get account whose history to look at
do{
System.out.printf("Enter the number (1-%d) of the account\n"+" whose transaction you waant to see : ",theUser.numAccounts());
theAct = sc.nextInt()-1;
if(theAct < 0 || theAct >= theUser.numAccounts()){
System.out.println("Invalid account. please try again.....");
}
}while(theAct < 0 || theAct >= theUser.numAccounts());
//Print transaction history
theUser.printActTransHistory(theAct);
}
public static void transferFunds(User theUser,Scanner sc){
//intits
int fromAct;
int toAct;
double amount;
double actBal;
//get the account to transfer from
do{
System.out.printf("Enter the number (1-%d) of the account\n"+"to transfer from:",theUser.numAccounts());
fromAct = sc.nextInt()-1;
if(fromAct < 0 || fromAct >= theUser.numAccounts()){
System.out.println("Invalid account. please try again.....");
}
}while(fromAct < 0 || fromAct >= theUser.numAccounts());
actBal = theUser.getAccountBalance(fromAct);
//get the account to transfer to
do{
System.out.printf("Enter the number (1-%d) of the account\n"+"to transfer to:",theUser.numAccounts());
toAct = sc.nextInt()-1;
if(toAct < 0 || toAct >= theUser.numAccounts()){
System.out.println("Invalid account. please try again.....");
}
}while(toAct < 0 || toAct >= theUser.numAccounts());
//get the amount to traansfer
do{
System.out.println("Enter the amount to transfer (max than $"+actBal+") : $");
amount = sc.nextDouble();
if(amount < 0){
System.out.println("Amount must be greater than zero");
}else if(amount > actBal){
System.out.println("Amount must not be greater than \n"+"balance of $"+actBal);
}
}while(amount < 0 || amount>actBal);
//do the transfer
theUser.addActTransaction(fromAct,-1*amount,String.format("Transfer to account "+theUser.getActUUID(toAct)));
theUser.addActTransaction(toAct,amount,String.format("Transfer to account "+theUser.getActUUID(fromAct)));
}
public static void withdrawFunds(User theUser,Scanner sc){
int fromAct;
String memo;
double amount;
double actBal;
//get the account to transfer from
do{
System.out.printf("Enter the number (1-%d) of the account\n"+"Where to withdraw :",theUser.numAccounts());
fromAct = sc.nextInt()-1;
if(fromAct < 0 || fromAct >= theUser.numAccounts()){
System.out.println("Invalid account. please try again.....");
}
}while(fromAct < 0 || fromAct >= theUser.numAccounts());
actBal = theUser.getAccountBalance(fromAct);
//get the amount to traansfer
do{
System.out.println("Enter the amount to withdraw (max $ "+actBal+"): $");
amount = sc.nextDouble();
if(amount < 0){
System.out.println("Amount must be greater than zero");
}else if(amount > actBal){
System.out.println("Amount must not be greater than \n"+"balance of $"+actBal);
}
}while(amount < 0 || amount>actBal);
//gobble up rest of previous input
sc.nextLine();
//get a memo
System.out.println("Enter a memo: ");
memo = sc.nextLine();
//do withdrawal
theUser.addActTransaction(fromAct,-1*amount,memo);
}
public static void depositeFunds(User theUser,Scanner sc){
int toAct;
String memo;
double amount;
double actBal;
//get the account to transfer from
do{
System.out.printf("Enter the number (1-%d) of the account\n"+"Where to Deposite:",theUser.numAccounts());
toAct = sc.nextInt()-1;
if(toAct < 0 || toAct >= theUser.numAccounts()){
System.out.println("Invalid account. please try again.....");
}
}while(toAct < 0 || toAct >= theUser.numAccounts());
actBal = theUser.getAccountBalance(toAct);
//get the amount to transfer
do{
System.out.print("Enter the amount to deposite (max than $"+actBal+") :$");
amount = sc.nextDouble();
if(amount < 0){
System.out.println("Amount must be greater than zero");
}
}while(amount < 0 );
//gobble up rest of previous input
sc.nextLine();
//get a memo
System.out.println("Enter a memo: ");
memo = sc.nextLine();
//do withdrawal
theUser.addActTransaction(toAct,amount,memo);
}
}