-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtm.java
More file actions
165 lines (146 loc) · 5.25 KB
/
Atm.java
File metadata and controls
165 lines (146 loc) · 5.25 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
import java.util.*;
public class Atm {
public static void main(String[] args) {
Bank bank = new Bank();
Scanner sc = new Scanner(System.in);
System.out.println("Enter your User ID:");
String userId = sc.nextLine();
System.out.println("Enter your PIN:");
String pin = sc.nextLine();
User user = bank.authenticateUser(userId, pin);
if (user != null) {
System.out.println("Welcome to the ATM!");
boolean quit = false;
while (!quit) {
System.out.println("\nATM Menu:");
System.out.println("1. Transaction History");
System.out.println("2. Withdraw");
System.out.println("3. Deposit");
System.out.println("4. Transfer");
System.out.println("5. Quit");
System.out.print("Choose an option: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
bank.printTransactionHistory(user);
break;
case 2:
System.out.print("Enter amount to withdraw: ");
double withdrawAmount = sc.nextDouble();
bank.withdraw(user, withdrawAmount);
break;
case 3:
System.out.print("Enter amount to deposit: ");
double depositAmount = sc.nextDouble();
bank.deposit(user, depositAmount);
break;
case 4:
System.out.print("Enter recipient User ID: ");
sc.nextLine();
String recipientId = sc.nextLine();
System.out.print("Enter amount to transfer: ");
double transferAmount = sc.nextDouble();
bank.transfer(user, recipientId, transferAmount);
break;
case 5:
quit = true;
System.out.println("Thank you for using the ATM. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
} else {
System.out.println("Authentication failed. Please try again.");
}
sc.close();
}
}
class Bank {
private Map<String, User> users;
public Bank() {
users = new HashMap<>();
// Adding some dummy users for testing
users.put("prashanthi", new User("prashanthi", "1234"));
users.put("akash", new User("akash", "5678"));
}
public User authenticateUser(String userId, String pin) {
User user = users.get(userId);
if (user != null && user.getPin().equals(pin)) {
return user;
}
return null;
}
public void printTransactionHistory(User user) {
user.printTransactionHistory();
}
public void withdraw(User user, double amount) {
if (user.withdraw(amount)) {
System.out.println("Withdrawal successful.");
} else {
System.out.println("Insufficient balance.");
}
}
public void deposit(User user, double amount) {
user.deposit(amount);
System.out.println("Deposit successful.");
}
public void transfer(User user, String recipientId, double amount) {
User recipient = users.get(recipientId);
if (recipient == null) {
System.out.println("Recipient not found.");
return;
}
if (user.withdraw(amount)) {
recipient.deposit(amount);
System.out.println("Transfer successful.");
} else {
System.out.println("Insufficient balance.");
}
}
}
class User {
private String userId;
private String pin;
private double balance;
private List<String> transactionHistory;
public User(String userId, String pin) {
this.userId = userId;
this.pin = pin;
this.balance = 0.0;
this.transactionHistory = new ArrayList<>();
}
public String getUserId() {
return userId;
}
public String getPin() {
return pin;
}
public double getBalance() {
return balance;
}
public List<String> getTransactionHistory() {
return transactionHistory;
}
public boolean withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
transactionHistory.add("Withdraw: " + amount);
return true;
}
return false;
}
public void deposit(double amount) {
balance += amount;
transactionHistory.add("Deposit: " + amount);
}
public void printTransactionHistory() {
if (transactionHistory.isEmpty()) {
System.out.println("No transactions found.");
} else {
for (String transaction : transactionHistory) {
System.out.println(transaction);
}
}
}
}