-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtmApp.java
More file actions
213 lines (175 loc) · 5.32 KB
/
AtmApp.java
File metadata and controls
213 lines (175 loc) · 5.32 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
public class BankAtm {
private String firstName;
private String lastName;
private int pin;
private double balance;
private boolean accountCreated = false;
Scanner input = new Scanner(System.in);
static List<BankAtm> accounts = new ArrayList<>();
public void getFullName() {
System.out.println(firstName + " " + lastName);
}
public void createAccount() {
System.out.print("Enter First Name: ");
firstName = input.nextLine();
System.out.print("Enter Last Name: ");
lastName = input.nextLine();
System.out.print("Set a 4-digit PIN: ");
pin = input.nextInt();
if (String.valueOf(pin).length() == 4) {
balance = 0.0;
accountCreated = true;
accounts.add(this);
System.out.println("Account Created Successfully! Welcome, " + firstName + " " + lastName);
} else {
System.out.println("Invalid PIN! Must be exactly 4 digits.");
}
input.nextLine();
}
public static BankAtm accountByName(String accountName) {
String[] storeName = accountName.trim().split(" ");
if (storeName.length < 2) {
System.out.println("Full name must include first and last name.");
return null;
}
String firstName = storeName[0];
String lastName = storeName[1];
for (BankAtm account : accounts)
if (account.firstName.equalsIgnoreCase(firstName) && account.lastName.equalsIgnoreCase(lastName)) {
return account;
}
return null;
}
public void deposit() {
if (!accountCreated) {
System.out.println("No account found! Please create an account first.");
return;
}
System.out.print("Enter deposit amount: ");
double amount = input.nextDouble();
balance += amount;
System.out.println("Deposit successful! New balance: " + balance);
}
public void withdraw() {
if (!accountCreated) {
System.out.println("No account found! Please create an account first.");
return;
}
System.out.print("Enter PIN: ");
int accountPin = input.nextInt();
if (accountPin != pin) {
System.out.println("Incorrect PIN. Withdrawal canceled.");
return;
}
System.out.print("Enter withdrawal amount: ");
double amount = input.nextDouble();
if (amount <= balance) {
balance -= amount;
System.out.println("Withdrawal successful! New balance: " + balance);
} else {
System.out.println("Insufficient balance.");
}
}
public void checkBalance() {
if (!accountCreated) {
System.out.println("No account found! Please create an account first.");
return;
}
System.out.print("Enter PIN: ");
int accountPin = input.nextInt();
if (accountPin == pin) {
System.out.println("Your balance: " + balance);
} else {
System.out.println("Incorrect PIN. Access denied.");
}
}
public void changePin() {
if (!accountCreated) {
System.out.println("No account found! Please create an account first.");
return;
}
System.out.print("Enter old PIN: ");
int oldPin = input.nextInt();
if (oldPin == pin) {
System.out.print("Enter new 4-digit PIN: ");
int newPin = input.nextInt();
if (String.valueOf(newPin).length() == 4) {
pin = newPin;
System.out.println("PIN changed successfully!");
} else {
System.out.println("Invalid PIN! PIN must be exactly 4 digits.");
}
} else {
System.out.println("Incorrect old PIN. PIN change failed.");
}
}
public void transfer(BankAtm receiver) {
if (!accountCreated) {
System.out.println("No account found! Please create an account first.");
return;
}
System.out.print("Enter your PIN: ");
int enteredPin = input.nextInt();
if (enteredPin != pin) {
System.out.println("Incorrect PIN.");
return;
}
System.out.print("Enter transfer amount: ");
double amount = input.nextDouble();
if (amount <= balance) {
balance -= amount;
receiver.balance += amount;
System.out.println("Transfer successful! new balance is: " + balance);
} else {
System.out.println("Insufficient balance.");
}
}
public void showMenu() {
int choice;
do {
System.out.println();
System.out.println("==========================");
System.out.println("\nATM MENU");
System.out.println("==========================");
System.out.println();
System.out.println("1. Create Account");
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
System.out.println("4. Check Balance");
System.out.println("5. Change PIN");
System.out.println("6. Transfer");
System.out.println("7. Exit");
System.out.print("Select an option: ");
choice = input.nextInt();
input.nextLine();
switch (choice) {
case 1 -> createAccount();
case 2 -> deposit();
case 3 -> withdraw();
case 4 -> checkBalance();
case 5 -> changePin();
case 6 -> {
System.out.print("Enter receiver's full name: ");
String receiverName = input.nextLine();
BankAtm receiver = accountByName(receiverName);
if (receiver != null && receiver != this) {
transfer(receiver);
} else if (receiver == this) {
System.out.println("You can't transfer to yourself.");
} else {
System.out.println("Receiver not found.");
}
}
case 7 -> System.out.println("Goodbye!");
default -> System.out.println("Invalid option! Try again.");
}
} while (choice != 7);
}
public static void main(String[] args) {
BankAtm atm = new BankAtm();
atm.showMenu();
}
}