- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMSimulation.java
More file actions
Latest commit
154 lines (133 loc) · 5.14 KB
/
Copy pathATMSimulation.java
File metadata and controls
154 lines (133 loc) · 5.14 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
importjava.util.*;
// Base class representing a Bank Account
classAccount {
protecteddoublebalance;
protectedintpin;
publicAccount(doubleinitialBalance, intpin) {
this.balance = initialBalance;
this.pin = pin;
}
publicbooleanverifyPIN(intenteredPIN) {
returnthis.pin == enteredPIN;
}
publicdoublegetBalance() {
returnbalance;
}
publicvoiddeposit(doubleamount) {
if (amount <= 0) {
thrownewIllegalArgumentException("Deposit amount must be positive.");
}
balance += amount;
}
publicvoidwithdraw(doubleamount) {
if (amount <= 0) {
thrownewIllegalArgumentException("Withdrawal amount must be positive.");
}
if (amount > balance) {
thrownewIllegalArgumentException("Insufficient funds.");
}
balance -= amount;
}
}
// Derived class representing a specific type of Account (Savings)
classSavingsAccountextendsAccount {
publicSavingsAccount(doubleinitialBalance, intpin) {
super(initialBalance, pin);
}
// Example of inheritance: adding specific behavior
publicvoidaddInterest(doublerate) {
if (rate < 0) thrownewIllegalArgumentException("Interest rate cannot be negative.");
balance += balance * rate / 100;
}
}
// ATM class handles multiple accounts using a Collection
classATMSimulation {
privatestaticfinalScannerscanner = newScanner(System.in);
privatestaticfinalMap<Integer, SavingsAccount> accounts = newHashMap<>(); // Stores accounts by account number
privatestaticSavingsAccountcurrentAccount;
publicstaticvoidmain(String[] args) {
// Sample accounts
accounts.put(1001, newSavingsAccount(1000.00, 1234));
accounts.put(1002, newSavingsAccount(500.00, 5678));
accounts.put(1003, newSavingsAccount(1500.00, 4321));
System.out.println("=== Welcome to the ATM Simulation ===");
try {
login();
showMenu();
} catch (Exceptione) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("Thank you for using the ATM!");
}
}
privatestaticvoidlogin() {
intattempts = 0;
while (attempts < 3) {
System.out.print("\nEnter Account Number: ");
intaccNum = scanner.nextInt();
System.out.print("Enter PIN: ");
intenteredPIN = scanner.nextInt();
SavingsAccountaccount = accounts.get(accNum);
if (account != null && account.verifyPIN(enteredPIN)) {
currentAccount = account;
System.out.println("Login successful!\n");
return;
} else {
attempts++;
System.out.println("Invalid account number or PIN. Attempts left: " + (3 - attempts));
}
}
thrownewSecurityException("Too many failed attempts. Access denied.");
}
privatestaticvoidshowMenu() {
while (true) {
System.out.println("\n--- ATM Menu ---");
System.out.println("1. Check Balance");
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
System.out.println("4. Add Interest");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
try {
intchoice = scanner.nextInt();
switch (choice) {
case1 -> checkBalance();
case2 -> deposit();
case3 -> withdraw();
case4 -> addInterest();
case5 -> {
System.out.println("Session ended. Goodbye!");
return;
}
default -> System.out.println("Invalid choice. Please try again.");
}
} catch (InputMismatchExceptione) {
System.out.println("Invalid input. Please enter a number.");
scanner.nextLine(); // clear input buffer
} catch (IllegalArgumentExceptione) {
System.out.println("Error: " + e.getMessage());
}
}
}
privatestaticvoidcheckBalance() {
System.out.printf("Your current balance is: Rs.%.2f%n", currentAccount.getBalance());
}
privatestaticvoiddeposit() {
System.out.print("Enter deposit amount: ");
doubleamount = scanner.nextDouble();
currentAccount.deposit(amount);
System.out.printf("Rs.%.2f deposited successfully.%n", amount);
}
privatestaticvoidwithdraw() {
System.out.print("Enter withdrawal amount: ");
doubleamount = scanner.nextDouble();
currentAccount.withdraw(amount);
System.out.printf("Rs.%.2f withdrawn successfully.%n", amount);
}
privatestaticvoidaddInterest() {
System.out.print("Enter interest rate (%): ");
doublerate = scanner.nextDouble();
currentAccount.addInterest(rate);
System.out.println("Interest added successfully!");
}
}