-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMmain.java
More file actions
244 lines (209 loc) · 6.85 KB
/
ATMmain.java
File metadata and controls
244 lines (209 loc) · 6.85 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
import java.util.ArrayList;
import java.util.Scanner;
class AccountHolder {
private String userId;
private String userPin;
public AccountHolder(String userId, String userPin) {
this.userId = userId;
this.userPin = userPin;
}
public String getUserId() {
return userId;
}
public String getUserPin() {
return userPin;
}
}
class Account {
private String accountId;
private double balance;
private ArrayList<String> transactionHistory;
public Account(String accountId, double balance) {
this.accountId = accountId;
this.balance = balance;
this.transactionHistory = new ArrayList<>();
}
public String getAccountId() {
return accountId;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
transactionHistory.add("Deposit: +" + amount);
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
transactionHistory.add("Withdraw: -" + amount);
} else {
System.out.println("Insufficient balance.");
}
}
public void transfer(Account recipient, double amount) {
if (balance >= amount) {
balance -= amount;
recipient.deposit(amount);
transactionHistory.add("Transfer: -" + amount + " to " + recipient.getAccountId());
} else {
System.out.println("Insufficient balance.");
}
}
public void showTransactionHistory() {
System.out.println("Transaction History for Account: " + accountId);
for (String transaction : transactionHistory) {
System.out.println(transaction);
}
System.out.println();
}
}
class Bank {
private ArrayList<Account> accounts;
public Bank() {
this.accounts = new ArrayList<>();
}
public void addAccount(Account account) {
accounts.add(account);
}
public Account getAccount(String accountId) {
for (Account account : accounts) {
if (account.getAccountId().equals(accountId)) {
return account;
}
}
return null;
}
}
class ATM {
private AccountHolder accountHolder;
private Bank bank;
private Scanner scanner;
public ATM(Bank bank) {
this.bank = bank;
this.scanner = new Scanner(System.in);
}
public void run() {
System.out.println("Welcome to the ATM!");
System.out.print("User ID: ");
String userId = scanner.nextLine();
System.out.print("PIN: ");
String userPin = scanner.nextLine();
accountHolder = authenticateUser(userId, userPin);
if (accountHolder != null) {
System.out.println("Authentication successful.");
showMenu();
} else {
System.out.println("Authentication failed. Exiting...");
}
}
private AccountHolder authenticateUser(String userId, String userPin) {
//authentication
if (userId.equals("user123") && userPin.equals("1234")) {
return new AccountHolder(userId, userPin);
} else {
return null;
}
}
private void showBalance() {
Account account = getAccountFromInput();
if (account != null) {
double balance = account.getBalance();
System.out.println("Current Balance: " + balance);
}
}
private void showTransactionHistory() {
Account account = getAccountFromInput();
if (account != null) {
account.showTransactionHistory();
}
}
private void performWithdrawal() {
Account account = getAccountFromInput();
if (account != null) {
System.out.print("Enter amount to withdraw: ");
double amount = scanner.nextDouble();
scanner.nextLine();
account.withdraw(amount);
}
}
private void performDeposit() {
Account account = getAccountFromInput();
if (account != null) {
System.out.print("Enter amount to deposit: ");
double amount = scanner.nextDouble();
scanner.nextLine();
account.deposit(amount);
}
}
private void performTransfer() {
Account sourceAccount = getAccountFromInput("Enter source account ID: ");
if (sourceAccount != null) {
Account recipientAccount = getAccountFromInput("Enter recipient account ID: ");
if (recipientAccount != null) {
System.out.print("Enter amount to transfer: ");
double amount = scanner.nextDouble();
scanner.nextLine();
sourceAccount.transfer(recipientAccount, amount);
}
}
}
private void showMenu() {
boolean exit = false;
while (!exit) {
System.out.println("\n=== ATM Menu ===");
System.out.println("1. Show Transaction History");
System.out.println("2. Withdraw");
System.out.println("3. Deposit");
System.out.println("4. Transfer");
System.out.println("5. Show Balance");
System.out.println("6. Quit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:showTransactionHistory();
break;
case 2:performWithdrawal();
break;
case 3:performDeposit();
break;
case 4:performTransfer();
break;
case 5:showBalance();
break;
case 6:exit = true;
break;
default:System.out.println("Invalid choice. Try again.");
break;
}
}
}
private Account getAccountFromInput() {
System.out.print("Enter account ID: ");
String accountId = scanner.nextLine();
return bank.getAccount(accountId);
}
private Account getAccountFromInput(String message) {
System.out.print(message);
String accountId = scanner.nextLine();
return bank.getAccount(accountId);
}
}
class ATMmain {
public static void main(String[] args) {
// Creating the Bank
Bank bank = new Bank();
// Creating some accounts
Account account1 = new Account("AC001", 15000);
Account account2 = new Account("AC002", 10000);
Account account3 = new Account("AC003", 5000);
// Adding the accounts to the bank
bank.addAccount(account1);
bank.addAccount(account2);
bank.addAccount(account3);
// Starting the ATM
ATM atm = new ATM(bank);
atm.run();
}
}