-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATM.java
More file actions
100 lines (88 loc) · 3.5 KB
/
ATM.java
File metadata and controls
100 lines (88 loc) · 3.5 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
// ATM.java
import java.util.*;
public class ATM {
private HashMap<String, UserAccount> users;
private Scanner scanner;
public ATM() {
users = new HashMap<>();
scanner = new Scanner(System.in);
seedUsers(); // Demo users
}
private void seedUsers() {
users.put("himanshu", new UserAccount("himanshu", "1234", 5000));
users.put("neha", new UserAccount("neha", "4321", 10000));
}
public void start() {
System.out.println("===== Welcome to the Java ATM =====");
int attempts = 3;
while (attempts > 0) {
System.out.print("Enter username: ");
String username = scanner.nextLine();
System.out.print("Enter PIN: ");
String pin = scanner.nextLine();
UserAccount user = users.get(username);
if (user != null && user.authenticate(pin)) {
showMenu(user);
return;
} else {
attempts--;
System.out.println("Invalid login. Attempts left: " + attempts);
}
}
System.out.println("Too many failed attempts. Account blocked.");
}
private void showMenu(UserAccount user) {
int choice;
do {
System.out.println("\nHello " + user.getUsername() + " 👋");
System.out.println("1. Check Balance");
System.out.println("2. Deposit Money");
System.out.println("3. Withdraw Money");
System.out.println("4. Transaction History");
System.out.println("5. Change PIN");
System.out.println("6. Exit");
System.out.print("Choose option: ");
choice = scanner.nextInt();
switch (choice) {
case 1 -> System.out.printf("Your balance is ₹%.2f\n", user.getBalance());
case 2 -> {
System.out.print("Enter amount to deposit: ₹");
double amount = scanner.nextDouble();
user.deposit(amount);
System.out.println("Deposited successfully.");
}
case 3 -> {
System.out.print("Enter amount to withdraw: ₹");
double amount = scanner.nextDouble();
if (user.withdraw(amount)) {
showDenominations(amount);
System.out.println("Withdrawal successful.");
} else {
System.out.println("Insufficient balance.");
}
}
case 4 -> user.printTransactions();
case 5 -> {
System.out.print("Enter new PIN: ");
scanner.nextLine(); // consume newline
String newPIN = scanner.nextLine();
user.changePIN(newPIN);
System.out.println("PIN changed successfully.");
}
case 6 -> System.out.println("Thank you for using Java ATM!");
default -> System.out.println("Invalid option. Try again.");
}
} while (choice != 6);
}
private void showDenominations(double amount) {
System.out.println("Dispensed as:");
int[] notes = {2000, 500, 200, 100};
for (int note : notes) {
int count = (int) amount / note;
if (count > 0) {
System.out.println("₹" + note + " x " + count);
amount %= note;
}
}
}
}