-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMSimulation.java
More file actions
154 lines (133 loc) · 5.14 KB
/
ATMSimulation.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
import java.util.*;
// Base class representing a Bank Account
class Account {
protected double balance;
protected int pin;
public Account(double initialBalance, int pin) {
this.balance = initialBalance;
this.pin = pin;
}
public boolean verifyPIN(int enteredPIN) {
return this.pin == enteredPIN;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("Deposit amount must be positive.");
}
balance += amount;
}
public void withdraw(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("Withdrawal amount must be positive.");
}
if (amount > balance) {
throw new IllegalArgumentException("Insufficient funds.");
}
balance -= amount;
}
}
// Derived class representing a specific type of Account (Savings)
class SavingsAccount extends Account {
public SavingsAccount(double initialBalance, int pin) {
super(initialBalance, pin);
}
// Example of inheritance: adding specific behavior
public void addInterest(double rate) {
if (rate < 0) throw new IllegalArgumentException("Interest rate cannot be negative.");
balance += balance * rate / 100;
}
}
// ATM class handles multiple accounts using a Collection
class ATMSimulation {
private static final Scanner scanner = new Scanner(System.in);
private static final Map<Integer, SavingsAccount> accounts = new HashMap<>(); // Stores accounts by account number
private static SavingsAccount currentAccount;
public static void main(String[] args) {
// Sample accounts
accounts.put(1001, new SavingsAccount(1000.00, 1234));
accounts.put(1002, new SavingsAccount(500.00, 5678));
accounts.put(1003, new SavingsAccount(1500.00, 4321));
System.out.println("=== Welcome to the ATM Simulation ===");
try {
login();
showMenu();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("Thank you for using the ATM!");
}
}
private static void login() {
int attempts = 0;
while (attempts < 3) {
System.out.print("\nEnter Account Number: ");
int accNum = scanner.nextInt();
System.out.print("Enter PIN: ");
int enteredPIN = scanner.nextInt();
SavingsAccount account = 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));
}
}
throw new SecurityException("Too many failed attempts. Access denied.");
}
private static void showMenu() {
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 {
int choice = scanner.nextInt();
switch (choice) {
case 1 -> checkBalance();
case 2 -> deposit();
case 3 -> withdraw();
case 4 -> addInterest();
case 5 -> {
System.out.println("Session ended. Goodbye!");
return;
}
default -> System.out.println("Invalid choice. Please try again.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a number.");
scanner.nextLine(); // clear input buffer
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
private static void checkBalance() {
System.out.printf("Your current balance is: Rs.%.2f%n", currentAccount.getBalance());
}
private static void deposit() {
System.out.print("Enter deposit amount: ");
double amount = scanner.nextDouble();
currentAccount.deposit(amount);
System.out.printf("Rs.%.2f deposited successfully.%n", amount);
}
private static void withdraw() {
System.out.print("Enter withdrawal amount: ");
double amount = scanner.nextDouble();
currentAccount.withdraw(amount);
System.out.printf("Rs.%.2f withdrawn successfully.%n", amount);
}
private static void addInterest() {
System.out.print("Enter interest rate (%): ");
double rate = scanner.nextDouble();
currentAccount.addInterest(rate);
System.out.println("Interest added successfully!");
}
}