-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
324 lines (282 loc) · 13.5 KB
/
Main.java
File metadata and controls
324 lines (282 loc) · 13.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
// Create an ArrayList to store all employees
private static ArrayList<Employee> employees = new ArrayList<>();
public static void main(String[] args) {
// Creating a new scanner
Scanner scanner = new Scanner(System.in);
// Clear the screen and print the welcome message
System.out.print("\033[H\033[2J");
System.out.println("Welcome to the bank application.");
// Initializing variables
int firstMenuOption = getMenuOption(scanner);
while (firstMenuOption != 6) {
switch (firstMenuOption) {
case 1:
createNewAccount(scanner);
break;
case 2:
depositMoney(scanner);
break;
case 3:
withdrawMoney(scanner);
break;
case 4:
checkEmployeeAccount(scanner);
break;
case 5:
listAllEmployees(scanner);
break;
default:
System.out.println("Invalid option. Please try again.");
break;
}
firstMenuOption = getMenuOption(scanner);
}
System.out.println("You have exited the bank application.");
// Close scanner
scanner.close();
}
public static int getMenuOption(Scanner scanner) {
System.out.println("-----------------------------");
System.out.println("| 1. Create a new account |");
System.out.println("| 2. Deposit money |");
System.out.println("| 3. Withdraw money |");
System.out.println("| 4. Check employee account |");
System.out.println("| 5. List all employees |");
System.out.println("| 6. Exit |");
System.out.println("-----------------------------");
// Read the user's input asking for a menu option
System.out.print("Enter an option: ");
int menuOption = scanner.nextInt();
scanner.nextLine();
// Clear the screen
System.out.print("\033[H\033[2J");
return menuOption;
}
public static void createNewAccount(Scanner scanner) {
try {
// Clear the screen and print the welcome message
System.out.print("\033[H\033[2J");
System.out.println("Creating a new account.");
System.out.println("-----------------------------");
// Read the user's input asking for the employee's name
String name;
while (true) {
System.out.print("Enter the employee's name: ");
name = scanner.nextLine();
if (name != null && !name.trim().isEmpty()) {
break;
}
System.out.print("\033[H\033[2J");
System.out.println("Name cannot be empty. Please try again.");
}
System.out.print("\033[H\033[2J");
// Read the user's input asking for the employee's last name
String lastName;
while (true) {
System.out.print("Enter the employee's last name: ");
lastName = scanner.nextLine();
if (lastName != null && !lastName.trim().isEmpty()) {
break;
}
System.out.print("\033[H\033[2J");
System.out.println("Last name cannot be empty. Please try again.");
}
System.out.print("\033[H\033[2J");
// Read the user's input asking for the employee's account number
int accountNumber;
while (true) {
boolean exists = false;
try {
System.out.print("Enter the employee's account number: ");
accountNumber = Integer.parseInt(scanner.nextLine());
// Looks for another existent account with the same account number
for (Employee employee : employees) {
if (employee.getAccountNumber() == accountNumber) {
exists = true;
System.out.print("\033[H\033[2J");
throw new IllegalArgumentException("An account with the same account number already exists. Please try again.");
}
}
if (accountNumber > 0) {
break; // Exit the loop if the input was valid
} else {
System.out.print("\033[H\033[2J");
System.out.println("Account number cannot be negative or zero. Please try again.");
}
} catch (NumberFormatException e) {
System.out.print("\033[H\033[2J");
if (exists) {
System.out.println(e.getMessage());
}
System.out.println("Invalid input. Please enter a number.");
}
}
System.out.print("\033[H\033[2J");
int balance = 0;
// Read the user's input asking for the employee's account type
String accountType;
while (true) {
System.out.print("Enter the employee's account type (A, B, or C): ");
accountType = scanner.nextLine().toUpperCase();
if (accountType.equals("A") || accountType.equals("B") || accountType.equals("C")) {
break;
}
System.out.print("\033[H\033[2J");
System.out.println("Account type must be 'A', 'B', or 'C'. Please try again.");
}
// Clear the screen
System.out.print("\033[H\033[2J");
// Create a new employee
employees.add(new Employee(name, lastName, accountNumber, balance, accountType));
System.out.println("Employee account created successfully.");
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
public static void depositMoney(Scanner scanner) {
NumberFormat numberFormat = NumberFormat.getInstance();
boolean exists = false;
System.out.println("Depositing money.\n");
while (true) {
if (employees.isEmpty()) {
System.out.print("\033[H\033[2J");
System.out.println("There are no employees to deposit money to. Returning to the main menu..");
break;
}
try {
System.out.print("Enter the employee's account number: ");
int accountNumber = Integer.parseInt(scanner.nextLine());
for (Employee employee : employees) {
if (employee.getAccountNumber() == accountNumber) {
exists = true;
System.out.print("\nEnter the amount to deposit: $");
try {
int amount = Integer.parseInt(scanner.nextLine());
employee.deposit(amount);
String balance = numberFormat.format(employee.getBalance());
System.out.println("\n$" + amount + " deposited successfully. The new balance is $" + balance + ".");
break;
} catch (NumberFormatException e) {
System.out.print("\033[H\033[2J");
System.out.println("Invalid input. Please enter a number for the deposit amount.\n");
} catch (Exception e) {
System.out.print("\033[H\033[2J");
System.out.println(e.getMessage() + " Please enter a valid deposit.\n");
}
}
}
if (!exists) {
System.out.println("\nEmployee not found.");
}
System.out.println("\nPress any key to return to the main menu.");
scanner.nextLine();
System.out.print("\033[H\033[2J");
break;
} catch (NumberFormatException e) {
System.out.print("\033[H\033[2J");
System.out.println("Please enter a valid account number.\n");
}
}
}
public static void withdrawMoney(Scanner scanner) {
NumberFormat numberFormat = NumberFormat.getInstance();
boolean exists = false;
System.out.println("Withdrawing money.\n");
while (true) {
if (employees.isEmpty()) {
System.out.println("There are no employees to withdraw money from. Returning to the main menu..");
break;
}
try {
System.out.print("Enter the employee's account number: ");
int accountNumber = Integer.parseInt(scanner.nextLine());
for (Employee employee : employees) {
if (employee.getAccountNumber() == accountNumber) {
exists = true;
System.out.print("\nEnter the amount to withdraw: $");
try {
int amount = Integer.parseInt(scanner.nextLine());
employee.withdraw(amount);
String balance = numberFormat.format(employee.getBalance());
String amountFormatted = numberFormat.format(amount);
System.out.println("\n$" + amountFormatted + " withdrawn successfully. The new balance is $" + balance + ".");
break;
} catch (NumberFormatException e) {
System.out.print("\033[H\033[2J");
System.out.println("Invalid input. Please enter a number for the withdrawal amount.\n");
} catch (Exception e) {
System.out.print("\033[H\033[2J");
System.out.println(e.getMessage() + " Please enter a valid withdrawal.\n");
}
}
}
if (!exists) {
System.out.println("\nEmployee not found.");
}
System.out.println("\nPress any key to return to the main menu.");
scanner.nextLine();
System.out.print("\033[H\033[2J");
break;
} catch (NumberFormatException e) {
System.out.print("\033[H\033[2J");
System.out.println("Please enter a valid account number.\n");
}
}
}
public static void checkEmployeeAccount(Scanner scanner) {
NumberFormat numberFormat = NumberFormat.getInstance();
boolean exists = false;
System.out.println("Checking employee account.\n");
while (true) {
if (employees.isEmpty()) {
System.out.println("There are no employees to check. Returning to the main menu..");
break;
}
try {
System.out.print("Enter the employee's account number: ");
int accountNumber = Integer.parseInt(scanner.nextLine());
for (Employee employee : employees) {
if (employee.getAccountNumber() == accountNumber) {
exists = true;
String balance = numberFormat.format(employee.getBalance());
System.out.println("\nThe account number of the employee " + employee.getName() + " " + employee.getLastName() + " is " + employee.getAccountNumber() + ", their balance is $" + balance + " and the account type is " + employee.getAccountType() + ".");
break;
}
}
if (!exists) {
System.out.println("\nEmployee not found.");
}
System.out.println("\nPress any key to return to the main menu.");
scanner.nextLine();
System.out.print("\033[H\033[2J");
break;
} catch (NumberFormatException e) {
System.out.print("\033[H\033[2J");
System.out.println("Please enter a valid account number.\n");
}
}
}
public static void listAllEmployees(Scanner scanner) {
NumberFormat numberFormat = NumberFormat.getInstance();
while (true) {
if (employees.isEmpty()) {
System.out.println("There are no employees to list. Returning to the main menu..");
break;
}
System.out.println("Listing all employees. Total: " + employees.size() + " employees.\n");
for (Employee employee : employees) {
int index = employees.indexOf(employee) + 1;
String balance = numberFormat.format(employee.getBalance());
System.out.println(index + ")" + " Name: " + employee.getName() + " " + employee.getLastName() + " | Account number: " + employee.getAccountNumber() + " | Balance: $" + balance + " | Account type: " + employee.getAccountType());
}
System.out.println("\nPress any key to return to the main menu.");
scanner.nextLine();
System.out.print("\033[H\033[2J");
break;
}
}
}