forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtm-simulator.java
More file actions
158 lines (131 loc) · 5.53 KB
/
Atm-simulator.java
File metadata and controls
158 lines (131 loc) · 5.53 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
package hello;
import java.util.*;
public class Atm {
private static class ATM {
private int balance = 5000;
private static int password = 1991;
public int getBalance() {
return balance;
}
public void deposit(int amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposit successful. Current balance: " + balance);
} else {
System.out.println("Invalid deposit amount");
}
}
public void withdraw(int amount) {
if (amount <= 0) {
System.out.println("Invalid withdrawal amount");
} else if (balance >= amount) {
balance -= amount;
int gst = amount * 2 / 100;
balance -= gst;
System.out.println("Withdrawal successful. GST (2%): " + gst);
System.out.println("Current balance: " + balance);
} else {
System.out.println("Insufficient balance");
}
}
public boolean verifyPassword(int input) {
return input == password;
}
public void changePassword(int newPassword) {
password = newPassword;
System.out.println("Password updated successfully");
}
}
public static void main(String[] args) {
ATM atm = new ATM();
Scanner sc = new Scanner(System.in);
boolean sessionActive = true;
System.out.println("Enter the User Name:");
String username = sc.nextLine();
if (!username.equals("chaitanya")) {
System.out.println("Invalid username");
return;
}
while (sessionActive) {
System.out.println("ENTER THE PASSWORD:");
int pin = sc.nextInt();
if (!atm.verifyPassword(pin)) {
System.out.println("Wrong password. Transaction cancelled.");
continue;
}
System.out.println("__WELCOME__");
System.out.println("\nWelcome to --------BANK OF INDIA-------:");
System.out.println("1. Check the balance");
System.out.println("2. Deposit the amount");
System.out.println("3. Withdraw the amount");
System.out.println("4. Send Money");
System.out.println("5. Change Password");
System.out.println("6. Exit");
System.out.println("___________________________________");
System.out.println("ENTER YOUR CHOICE:");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Balance: " + atm.getBalance());
break;
case 2:
System.out.println("Enter the amount to deposit:");
int depositAmount = sc.nextInt();
atm.deposit(depositAmount);
break;
case 3:
System.out.println("Enter the amount to withdraw:");
int withdrawAmount = sc.nextInt();
atm.withdraw(withdrawAmount);
break;
case 4:
System.out.println("1. OM");
System.out.println("2. SHAM");
System.out.println("3. KARAN");
System.out.println("4. RAMAN");
System.out.println("ENTER THE RECIPIENT:");
int recipient = sc.nextInt();
System.out.println("Enter amount to transfer:");
int transferAmount = sc.nextInt();
if (transferAmount <= 0) {
System.out.println("Invalid amount");
break;
}
if (transferAmount > atm.getBalance()) {
System.out.println("Insufficient balance");
break;
}
int gst = transferAmount * 2 / 100;
atm.withdraw(transferAmount + gst);
System.out.println("Transfer successful to recipient " + recipient);
System.out.println("GST (2%): " + gst);
System.out.println("Current balance: " + atm.getBalance());
break;
case 5:
System.out.println("Enter current password:");
int currentPin = sc.nextInt();
if (!atm.verifyPassword(currentPin)) {
System.out.println("Wrong password");
break;
}
System.out.println("Enter new password:");
int newPin = sc.nextInt();
System.out.println("Confirm new password:");
int confirmPin = sc.nextInt();
if (newPin == confirmPin) {
atm.changePassword(newPin);
} else {
System.out.println("Passwords don't match");
}
break;
case 6:
sessionActive = false;
System.out.println("Thank you for banking with us!");
break;
default:
System.out.println("Invalid choice");
}
}
sc.close();
}
}