forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionMenu.java
More file actions
131 lines (114 loc) · 5.02 KB
/
OptionMenu.java
File metadata and controls
131 lines (114 loc) · 5.02 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
import java.text.DecimalFormat; // Import for formatting balance outputs as currency
import java.util.HashMap; // Import for storing customer number and PIN pairs
import java.util.Scanner; // Import for capturing user input from the console
public class OptionMenu extends Account {
Scanner menuInput = new Scanner(System.in); // Scanner object for reading user input
DecimalFormat moneyFormat = new DecimalFormat("'$'###,##0.00"); // Currency formatter
HashMap<Integer, Integer> data = new HashMap<>(); // Stores customer number as key, PIN as value
// Handles login process
public void getLogin() {
int x = 1;
do {
try {
// Predefined dummy accounts
data.put(952141, 191904);
data.put(989947, 717976);
System.out.println("Welcome to ATM");
System.out.println("Enter your Customer Number");
setCustomerNumber(menuInput.nextInt()); // Reads and sets customer number
System.out.println("Enter your PIN Number");
setPinNumber(menuInput.nextInt()); // Reads and sets PIN
}
catch(Exception e) {
// Handles non-numeric input
System.out.println("\nInvalid Characters Only Numbers Allowed\n" + e);
x = 2; // Prevents loop continuation on error
}
int cn = getCustomerNumber();
int pn = getPinNumber();
// Validates login credentials
if (data.containsKey(cn) && data.get(cn) == pn) {
getAccountType(); // Proceed to account menu
}
else {
System.out.println("\nWrong Customer Number or Wrong PIN Number\n\n");
}
} while (x == 1); // Loop continues until valid login or exception
}
// Displays account type options
public void getAccountType() {
System.out.println("Select Account Type you want to Access");
System.out.println("Type 1 - Checking Account");
System.out.println("Type 2 - Savings Account");
System.out.println("Type 3 - Exit");
int selection = menuInput.nextInt();
// Handles user selection
switch (selection) {
case 1 -> getChecking(); // Redirects to checking account menu
case 2 -> getSaving(); // Redirects to saving account menu
case 3 -> System.out.println("Thank you for using ATM, BYE\n"); // Exit message
default -> System.out.println("\n Invalid Choice \n"); // Handles invalid input
}
}
// Menu for checking account options
public void getChecking() {
System.out.println("Checking Account");
System.out.println("Type 1 - View Balance");
System.out.println("Type 2 - Withdraw Money");
System.out.println("Type 3 - Deposit Funds");
System.out.println("Type 4 - Exit");
int selection = menuInput.nextInt();
// Handles user selection
switch (selection) {
case 1 -> {
// Display balance
System.out.println("Checking Account Balance: " + moneyFormat.format(getCheckingBalance()));
getAccountType(); // Return to main menu
}
case 2 -> {
getCheckingWithdrawInput(); // Perform withdrawal
getAccountType();
}
case 3 -> {
getCheckingDepositInput(); // Perform deposit
getAccountType();
}
case 4 -> System.out.println("Thank you for using ATM, Bye"); // Exit message
default -> {
System.out.println("\nInvalid Choice\n");
getChecking(); // Re-prompt on invalid input
}
}
}
// Menu for saving account options
public void getSaving() {
System.out.println("Saving Account");
System.out.println("Type 1 - View Balance");
System.out.println("Type 2 - Withdraw Money");
System.out.println("Type 3 - Deposit Funds");
System.out.println("Type 4 - Exit");
System.out.print("Choice: ");
int selection = menuInput.nextInt();
// Handles user selection
switch (selection) {
case 1 -> {
// Display balance
System.out.println("Saving Account Balance: " + moneyFormat.format(getSavingBalance()));
getAccountType(); // Return to main menu
}
case 2 -> {
getSavingWithdrawInput(); // Perform withdrawal
getAccountType();
}
case 3 -> {
getSavingDepositInput(); // Perform deposit
getAccountType();
}
case 4 -> System.out.println("Thank you for using ATM, Bye\n"); // Exit message
default -> {
System.out.println("\nInvalid Choice\n");
getChecking(); // Re-prompt (note: this may be intended to redirect to saving, consider reviewing)
}
}
}
}