-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATM.java
More file actions
281 lines (241 loc) · 9.57 KB
/
Copy pathATM.java
File metadata and controls
281 lines (241 loc) · 9.57 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import java.util.Scanner;
public class ATM1 {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
int[] acctNum = new int[] {12345, 67890};
String[] acctName = new String[]{"Joseph", "Yusuf"};
String[] acctSurName = new String[]{"LEDET", "OGRENCI"};
String[] acctPIN = new String[]{"0101", "6789"};
double[] acctBal = new double[]{10000.0, 2.50};
bankLogin(acctNum, acctName, acctSurName, acctPIN, acctBal);
}
public static int menuDisplay(String[] item, Scanner inp) {
for(int i = 0; i < item.length; i++)
System.out.println((i+1) + " - " + item[i]);
System.out.print("0 to Quit\nPlease enter your selection >> ");
return Integer.parseInt(inp.nextLine());
}
// findAcct - get the index in an array of an account
public static int findAcct(int[] acctNums, int acctNum) {
for (int i = 0; i < acctNums.length; i++)
if(acctNums[i] == acctNum)
return i;
return -1;
}
// bankLogin - user interface to login to the atm
public static void bankLogin(int[] acctNum, String[] acctName, String[] acctSurName, String[] acctPIN, double[] acctBalance){
Scanner input = new Scanner(System.in);
System.out.print("Please enter your account number >> ");
int userNum = Integer.parseInt(input.nextLine());
System.out.print("Please enter your PIN >> ");
String userPIN = input.nextLine();
int acctIndex = findAcct(acctNum, userNum);
if(acctIndex == -1 || !userPIN.equals(acctPIN[acctIndex])){
System.out.println("ERROR: Account/PIN combination not found.");
return;
}
atm(acctName, acctSurName, acctBalance, acctIndex, input);
}
// atm - user interface for the atm
public static void atm(String[] acctName, String[] acctSurName, double[] acctBalance, int index, Scanner inp){
int choice = 0;
String[] menu = new String[]{"Account Balance", "Deposit", "Withdrawal", "Change Name"};
do{
System.out.println("\nHello " + acctName[index] + " " + acctSurName[index]);
System.out.println("What would you like to do today?");
choice = menuDisplay(menu, inp);
double amount = 0;
switch(choice) {
case 1: // Balance
System.out.println("Your account balance is " + acctBalance[index]);
break;
case 2: // Deposit
System.out.print("How much are you depositing? ");
amount = Double.parseDouble(inp.nextLine());
if(isDepositValid(amount))
acctBalance[index] += amount;
else
System.out.println("ERROR: Invalid deposit amount");
break;
case 3: // Withdrawal
System.out.print("How much are you withdrawing? ");
amount = Double.parseDouble(inp.nextLine());
if(isWithdrawalValid(acctBalance[index], amount)) {
System.out.println("You will receive the following:");
System.out.println(moneyGiven(amount));
acctBalance[index] -= amount;
} else
System.out.println("ERROR: Invalid withdrawal amount");
break;
case 4: // Change Name
inp.nextLine();
System.out.print("Please enter your name >> ");
String name = inp.nextLine();
String name2 = "" + Character.toUpperCase(name.charAt(0));
String name3 = name.substring(1).toLowerCase();
acctName[index] = name2 + name3;
// acctName[index] = Character.toUpperCase(acctName[index].charAt(0)) +
// acctName[index].substring(1).toLowerCase();
System.out.print("Please enter your surname >> ");
acctSurName[index] = inp.nextLine().toUpperCase();
break;
case 0:
System.out.println("Thank you for using our ATM. Have a nice day!");
break;
default:
System.out.println("Invalid choice. Exiting System.");
choice = 0;
}
} while (choice != 0);
}
// main - the main ATM program
public static void main_old(String args[]){
Scanner input = new Scanner(System.in);
System.out.print("Please enter your first name >> ");
String name = input.nextLine();
System.out.print("Please enter your surname >> ");
String surname = input.nextLine().toUpperCase();
double balance = -1;
do {
System.out.print("Please enter your starting balance >> ");
double balanceIn = Double.parseDouble(input.nextLine());
if (balanceIn < 0)
System.out.println("ERROR: Invalid balance");
else
balance = balanceIn;
} while(balance < 0);
int choice = 0;
do{
System.out.println("\nHello " + name + " " + surname);
System.out.println("What would you like to do today?");
System.out.println("1 for Account Balance");
System.out.println("2 for Cash Deposit");
System.out.println("3 for Cash Withdrawal");
System.out.println("0 to quit");
System.out.print("Please enter your selection >> ");
choice = Integer.parseInt(input.nextLine());
double amount = 0;
switch(choice) {
case 1: // Balance
System.out.println("Your account balance is " + balance);
break;
case 2: // Deposit
System.out.print("How much are you depositing? ");
amount = Double.parseDouble(input.nextLine());
if(isDepositValid(amount))
balance += amount;
else
System.out.println("ERROR: Invalid deposit amount");
break;
case 3: // Withdrawal
System.out.print("How much are you withdrawing? ");
amount = Double.parseDouble(input.nextLine());
if(isWithdrawalValid(balance, amount)) {
System.out.println("You will receive the following:");
System.out.println(moneyGiven(amount));
balance -= amount;
} else
System.out.println("ERROR: Invalid withdrawal amount");
break;
case 0:
System.out.println("Thank you for using our ATM. Have a nice day!");
break;
default:
System.out.println("Invalid choice. Exiting System.");
choice = 0;
}
} while (choice != 0);
}
// isDepositValid - is the amount valid to be deposited
public static boolean isDepositValid(double amount){
return amount > 0;
}
// isWithdrawalValid - is the amount valid to be withdrawn
public static boolean isWithdrawalValid(double balance, double amount){
return amount > 0 && amount <= balance;
}
//* moneyGiven - the different bills and coins to be given for a withdrawal
public static String moneyGiven(double amount) { // Without 0's shown
int intAmount = (int)(amount * 100);
String retVal = "";
if(intAmount >= 20000){
retVal += intAmount / 20000 + " - 200\n";
intAmount %= 20000;
}
if(intAmount >= 10000){
retVal += intAmount / 10000 + " - 100\n";
intAmount %= 10000;
}
if(intAmount >= 5000){
retVal += intAmount / 5000 + " - 50\n";
intAmount %= 5000;
}
if(intAmount >= 2000){
retVal += intAmount / 2000 + " - 20\n";
intAmount %= 2000;
}
if(intAmount >= 1000){
retVal += intAmount / 1000 + " - 10\n";
intAmount %= 1000;
}
if(intAmount >= 500){
retVal += intAmount / 500 + " - 5\n";
intAmount %= 500;
}
if(intAmount >= 100){
retVal += intAmount / 100 + " - 1\n";
intAmount %= 100;
}
if(intAmount >= 50){
retVal += intAmount / 50 + " - 0.50\n";
intAmount %= 50;
}
if(intAmount >= 25){
retVal += intAmount / 25 + " - 0.25\n";
intAmount %= 25;
}
if(intAmount >= 10){
retVal += intAmount / 10 + " - 0.10\n";
intAmount %= 10;
}
if(intAmount >= 5){
retVal += intAmount / 5 + " - 0.05\n";
intAmount %= 5;
}
if(intAmount >= 1){
retVal += intAmount + " - 0.01";
intAmount %= 1;
}
return retVal;
}
// moneyGiven - the different bills and coins to be given for a withdrawal
public static String moneyGiven1(double amount) { // With 0's shown
int intAmount = (int)(amount * 100);
String retVal = "";
retVal += intAmount / 20000 + " - 200\n";
intAmount %= 20000;
retVal += intAmount / 10000 + " - 100\n";
intAmount %= 10000;
retVal += intAmount / 5000 + " - 50\n";
intAmount %= 5000;
retVal += intAmount / 2000 + " - 20\n";
intAmount %= 2000;
retVal += intAmount / 1000 + " - 10\n";
intAmount %= 1000;
retVal += intAmount / 500 + " - 5\n";
intAmount %= 500;
retVal += intAmount / 100 + " - 1\n";
intAmount %= 100;
retVal += intAmount / 50 + " - 0.50\n";
intAmount %= 50;
retVal += intAmount / 25 + " - 0.25\n";
intAmount %= 25;
retVal += intAmount / 10 + " - 0.10\n";
intAmount %= 10;
retVal += intAmount / 5 + " - 0.05\n";
intAmount %= 5;
retVal += intAmount + " - 0.01";
intAmount %= 1;
return retVal;
}
}