Skip to content

Commit 84604ed

Browse files
Create Atm-simulator.java
1 parent 2cccac4 commit 84604ed

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

Atm-simulator.java

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package hello;
2+
import java.util.*;
3+
4+
public class Atm {
5+
private static class ATM {
6+
private int balance = 5000;
7+
private static int password = 1991;
8+
9+
public int getBalance() {
10+
return balance;
11+
}
12+
13+
public void deposit(int amount) {
14+
if (amount > 0) {
15+
balance += amount;
16+
System.out.println("Deposit successful. Current balance: " + balance);
17+
} else {
18+
System.out.println("Invalid deposit amount");
19+
}
20+
}
21+
22+
public void withdraw(int amount) {
23+
if (amount <= 0) {
24+
System.out.println("Invalid withdrawal amount");
25+
} else if (balance >= amount) {
26+
balance -= amount;
27+
int gst = amount * 2 / 100;
28+
balance -= gst;
29+
System.out.println("Withdrawal successful. GST (2%): " + gst);
30+
System.out.println("Current balance: " + balance);
31+
} else {
32+
System.out.println("Insufficient balance");
33+
}
34+
}
35+
36+
public boolean verifyPassword(int input) {
37+
return input == password;
38+
}
39+
40+
public void changePassword(int newPassword) {
41+
password = newPassword;
42+
System.out.println("Password updated successfully");
43+
}
44+
}
45+
46+
public static void main(String[] args) {
47+
ATM atm = new ATM();
48+
Scanner sc = new Scanner(System.in);
49+
boolean sessionActive = true;
50+
51+
System.out.println("Enter the User Name:");
52+
String username = sc.nextLine();
53+
54+
if (!username.equals("chaitanya")) {
55+
System.out.println("Invalid username");
56+
return;
57+
}
58+
59+
while (sessionActive) {
60+
System.out.println("ENTER THE PASSWORD:");
61+
int pin = sc.nextInt();
62+
63+
if (!atm.verifyPassword(pin)) {
64+
System.out.println("Wrong password. Transaction cancelled.");
65+
continue;
66+
}
67+
68+
System.out.println("__WELCOME__");
69+
System.out.println("\nWelcome to --------BANK OF INDIA-------:");
70+
System.out.println("1. Check the balance");
71+
System.out.println("2. Deposit the amount");
72+
System.out.println("3. Withdraw the amount");
73+
System.out.println("4. Send Money");
74+
System.out.println("5. Change Password");
75+
System.out.println("6. Exit");
76+
System.out.println("___________________________________");
77+
System.out.println("ENTER YOUR CHOICE:");
78+
79+
int choice = sc.nextInt();
80+
81+
switch (choice) {
82+
case 1:
83+
System.out.println("Balance: " + atm.getBalance());
84+
break;
85+
86+
case 2:
87+
System.out.println("Enter the amount to deposit:");
88+
int depositAmount = sc.nextInt();
89+
atm.deposit(depositAmount);
90+
break;
91+
92+
case 3:
93+
System.out.println("Enter the amount to withdraw:");
94+
int withdrawAmount = sc.nextInt();
95+
atm.withdraw(withdrawAmount);
96+
break;
97+
98+
case 4:
99+
System.out.println("1. OM");
100+
System.out.println("2. SHAM");
101+
System.out.println("3. KARAN");
102+
System.out.println("4. RAMAN");
103+
System.out.println("ENTER THE RECIPIENT:");
104+
int recipient = sc.nextInt();
105+
106+
System.out.println("Enter amount to transfer:");
107+
int transferAmount = sc.nextInt();
108+
109+
if (transferAmount <= 0) {
110+
System.out.println("Invalid amount");
111+
break;
112+
}
113+
114+
if (transferAmount > atm.getBalance()) {
115+
System.out.println("Insufficient balance");
116+
break;
117+
}
118+
119+
int gst = transferAmount * 2 / 100;
120+
atm.withdraw(transferAmount + gst);
121+
System.out.println("Transfer successful to recipient " + recipient);
122+
System.out.println("GST (2%): " + gst);
123+
System.out.println("Current balance: " + atm.getBalance());
124+
break;
125+
126+
case 5:
127+
System.out.println("Enter current password:");
128+
int currentPin = sc.nextInt();
129+
130+
if (!atm.verifyPassword(currentPin)) {
131+
System.out.println("Wrong password");
132+
break;
133+
}
134+
135+
System.out.println("Enter new password:");
136+
int newPin = sc.nextInt();
137+
System.out.println("Confirm new password:");
138+
int confirmPin = sc.nextInt();
139+
140+
if (newPin == confirmPin) {
141+
atm.changePassword(newPin);
142+
} else {
143+
System.out.println("Passwords don't match");
144+
}
145+
break;
146+
147+
case 6:
148+
sessionActive = false;
149+
System.out.println("Thank you for banking with us!");
150+
break;
151+
152+
default:
153+
System.out.println("Invalid choice");
154+
}
155+
}
156+
sc.close();
157+
}
158+
}

0 commit comments

Comments
 (0)