-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMSystem.java
More file actions
192 lines (169 loc) · 5.64 KB
/
ATMSystem.java
File metadata and controls
192 lines (169 loc) · 5.64 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
import java.text.SimpleDateFormat;
import java.util.*;
public class ATMSystem {
public static ArrayList<Bank> bankSystem = new ArrayList<>();
public static Scanner sc = new Scanner(System.in);
public static String CURRENT_DATE = getCurrentDate();
public static void main(String[] arg) {
Bank bankA = new Bank("A");
Bank bankB = new Bank("B");
ATM atmA1 = new ATM(bankA, "1", 100);
ATM atmA2 = new ATM(bankA, "2", 100);
ATM atmB1 = new ATM(bankB, "1", 100);
ATM atmB2 = new ATM(bankB, "2", 100);
bankA.addATM(atmA1);
bankA.addATM(atmA2);
bankB.addATM(atmB1);
bankB.addATM(atmB2);
bankSystem.add(bankA);
bankSystem.add(bankB);
// adding customers into 2 banks
Account a1 = bankA.addAccount("11");
Account a2 = bankA.addAccount("12");
Account b1 = bankB.addAccount("111");
Account b2 = bankB.addAccount("112");
Account b3 = bankB.addAccount("113");
// customize account
a1.setup("mypassword", "03/14/20", 60);
a2.setup("123456", "12/09/19", 160);
b1.setup("pizza", "11/14/21", 200);
b2.setup("Disneyland", "02/02/18", 500);
b3.setup("lalala", "01/01/19", 40);
System.out.println("States of two banks");
System.out.println("+++++++++++++++++++++");
printBankState(bankA);
printBankState(bankB);
System.out.println("States of ATM");
System.out.println("+++++++++++++++++++++");
printAtmState(bankA);
printAtmState(bankB);
System.out.println("Enter your choice of ATM:");
String atmInput = sc.next();
Bank bank = getBankFromATM(atmInput);
ATM myAtm = bank.specifyAtm(atmInput);
Account acc = verifyCardAtATM(bank);
if(acc!= null){
Account authorizedAcc = authorizeCard(acc);
withdraw(authorizedAcc, myAtm);
}
}// end main
public static Account verifyCardAtATM(Bank bank) {
System.out.println("Please insert your card");
String cardInput = sc.next();
Account associateAcc = new Account(cardInput);
if (!String.valueOf(cardInput.charAt(0)).equals(bank.getBankId())) {
associateAcc = null;
System.out.println("Your card is not supported by this ATM. Returning card...");
} else {
associateAcc = bank.verifyCard(cardInput);
if (associateAcc.getCard().checkExp(CURRENT_DATE)){
System.out.println("This card is expired. Returning card...");
//associateAcc = null;
}
else
System.out.print("Card verified. ");
}
return associateAcc;
}
public static Account authorizeCard(Account acc) {
System.out.print("Please enter your password: ");
String pw = sc.next();
while (!acc.getCard().checkPasscode(pw)) {
System.out.println("Invalid password. Enter your password:");
pw = sc.next();
}
System.out.println("Card authorized. ");
return acc;
}
public static void withdraw(Account acc, ATM atm) {
System.out.println("Please enter amount you want to withdraw: ");
double amount = sc.nextDouble();
double limit = atm.getAccumulateLimit();
double bal = acc.getBalance();
checkAmount(limit,amount,bal);
acc.setBalance(bal - amount);
atm.setAccumulateLimit(amount);
System.out.println("Amount withdrawed: $" + amount);
System.out.println("Your account current balance: $" + acc.getBalance());
System.out.println("Do you have more transactions? (Y/N) 1");
char c;
String ans = sc.next();
c = ans.charAt(0);
while (c == 'y' || c == 'Y') {
checkAmount(limit,amount,bal);
System.out.println("Do you have more transactions? (Y/N) 2");
ans = sc.next();
c = ans.charAt(0);
}
if (c == 'n' || c == 'N') {
System.out.println("Quitting. Thanks for your business");
return;
}
}
public static void printBankState(Bank b) {
System.out.println("Bank" + b.getBankId());
ArrayList<Account> accList = b.getAccList();
Iterator<Account> iter = accList.iterator();
while (iter.hasNext()) {
Account temp = iter.next();
System.out.println("Bankid: " + temp.getBankId() + ", Account and cashcard: #" + temp.getAccId()
+ " expires on " + temp.getCard().getExpDate() + ", password: " + temp.getCard().getPassCode());
}
System.out.println();
}
public static void printAtmState(Bank b) {
ArrayList<ATM> al = b.getAtmList();
Iterator<ATM> iter = al.iterator();
while (iter.hasNext()) {
ATM temp = iter.next();
System.out.println("ATM " + temp.getAtmId());
System.out.println("The maximum limit for withdrawal per cash card is $" + temp.getLimit() + " per day.");
}
System.out.println();
}
public static String getCurrentDate() {
SimpleDateFormat sfd = new SimpleDateFormat("MM/dd/yy");
return sfd.format(new Date());
}
public static Bank getBankFromATM(String id) {
String bankId = String.valueOf(id.charAt(0));
Iterator<Bank> iter = bankSystem.iterator();
Bank found = new Bank(bankId);
while (iter.hasNext()) {
Bank temp = iter.next();
if (temp.getBankId().equals(bankId)) {
found = temp;
break;
} else {
found = null;
}
}
return found;
}
public static boolean isDigit(char c) {
return Character.isDigit(c);
}
public static void checkAmount(double limit, double amount, double bal){
if (amount > limit) {
System.out
.println("This amount exceeds the limit of daily withdrawal. Please enter another amount or quit");
String input = sc.next();
if (isDigit(input.charAt(0))) {
amount = Double.parseDouble(input);
} else {
System.out.println("Quitting. Thanks for your business");
return;
}
if (amount > bal) {
System.out.println("This amount exceeds your current balance. Please enter another amount or quit");
String input1 = sc.next();
if (isDigit(input1.charAt(0))) {
amount = Double.parseDouble(input1);
} else{
System.out.println("Quitting. Thanks for your business");
return;
}
}
}
}
}