Repository navigation
Expand file tree
/
Copy pathMenu.java
More file actions
233 lines (206 loc) · 8.7 KB
/
Copy pathMenu.java
File metadata and controls
233 lines (206 loc) · 8.7 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
package bankmanagement;
import java.util.ArrayList;
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Menu {
Scanner keyboard = new Scanner(System.in);
Scanner scanner = new Scanner("customer.txt");
BankManagement bank = new BankManagement();
boolean exit;
int customerID = 0;
Date date = new Date();
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
public static void main(String[] args){
Menu menu = new Menu();
menu.startMenu();
}
public void startMenu(){
header();
while(!exit){
printMenu();
int select = getInput();
performAction(select);
}
}
private void header() {
System.out.println(" +==================================+");
System.out.println(" | Welcome to the Bank! |");
System.out.println(" +==================================+");
}
private void printMenu() {
System.out.println("+------------------------------------+");
System.out.println("|Please choose one of the following |");
System.out.println("| 1. Sign up for Account |");
System.out.println("| 2. Deposit |");
System.out.println("| 3. Withdraw |");
System.out.println("| 4. Loan |");
System.out.println("| 5. Check Balance Inquiry |");
System.out.println("| 0. Exit the App |");
System.out.println("+------------------------------------+");
}
private int getInput() {
int select = -1;
do{
System.out.print("\nPlease choose from the selection: ");
try{
select = Integer.parseInt(keyboard.nextLine());
}catch(NumberFormatException ex){
System.out.println("Invalid entry! Please enter a number of choice");
}
if(select < 0 || select > 5 ){
System.out.println("The selection is outside the range. Please enter a number of choice");
}
}while(select < 0 || select > 5 );
return select;
}
private void performAction(int select) {
switch(select){
case 0:
System.out.println("Thank you for banking with us.");
System.out.println("Have a great day!");
System.exit(0);
break;
case 1:
createAccount();
break;
case 2:
makeDeposit();
break;
case 3:
makeWithdraw();
break;
case 4:
makeLoan();
break;
case 5:
checkBalance();
break;
default:
System.out.println("Unknown error!");
}
}
private void createAccount() {
String firstName, lastName, ssn, phone, accountType = "";
double initialDeposit = 0;
boolean valid = false;
while(!valid){
System.out.print("Please enter an account type(checking/savings): ");
accountType = keyboard.nextLine();
if(accountType.equalsIgnoreCase("checking") || accountType.equalsIgnoreCase("savings")){
valid = true;
}else{
System.out.println("Invalid account type. Please enter checking or savings.");
}
}
System.out.print("Please enter your first name: ");
firstName = keyboard.nextLine();
System.out.print("Please enter your last name: ");
lastName = keyboard.nextLine();
System.out.print("Please enter your social security number: ");
ssn = keyboard.nextLine();
System.out.print("Please enter your phone number: ");
phone = keyboard.nextLine();
valid = false;
while(!valid){
System.out.print("Please enter an initial deposit: ");
try {
initialDeposit = Double.parseDouble(keyboard.nextLine());
}catch(NumberFormatException ex){
System.out.println("Deposit must be a number.");
}
if(accountType.equalsIgnoreCase("checking")){
if(initialDeposit < 200){
System.out.println("Checking accounts require a minimum of $200 dollars to open.");
}else{
valid = true;
}
} else if(accountType.equalsIgnoreCase("savings")){
if(initialDeposit < 100){
System.out.println("Savings accounts require a minimum of $100 dollars to open.");
}else{
valid = true;
}
}
}
Account account;
if(accountType.equalsIgnoreCase("checking")){
account = new Checking(initialDeposit);
}else{
account = new Savings(initialDeposit);
}
Customer customer = new Customer(firstName, lastName, ssn, phone, account);
bank.addCustomer(customer);
String accountInfo = account.toData() + "\t" + customer.getCustomerID();
String customerInfo = customer.toString();
String transaction = account.getAccountNum() + "\t" + dateFormatter.format(date) + "\t" + "Deposit" + "\t\t" + initialDeposit;
WriterReader.appendToTabDelimitedFile(customerInfo, "customer.txt");
WriterReader.appendToTabDelimitedFile(accountInfo, "account.txt");
WriterReader.appendToTabDelimitedFile(transaction, "transaction.txt");
customerID++;
System.out.println("Thank you! You have successfully created an account.\n");
}
private void makeDeposit() {
double amount = 0;
int account = selectAccount();
System.out.println("+---------------------------------------+");
System.out.print(" How much would you like to deposit: ");
try{
amount = Double.parseDouble(keyboard.nextLine());
}catch(NumberFormatException ex){
amount = 0;
}
System.out.println("+---------------------------------------+");
bank.getCustomer(account).getAccount().deposit(amount);
String transaction = bank.getCustomer(account).getAccount().getAccountNum() + "\t" + dateFormatter.format(date) + "\t" + "Deposit" + "\t\t" + amount;
WriterReader.appendToTabDelimitedFile(transaction, "transaction.txt");
}
private void makeWithdraw() {
double amount = 0;
int account = selectAccount();
System.out.println("+---------------------------------------+");
System.out.print(" How much would you like to withdraw: ");
try{
amount = Double.parseDouble(keyboard.nextLine());
}catch(NumberFormatException ex){
amount = 0;
}
System.out.println("+---------------------------------------+");
bank.getCustomer(account).getAccount().withdraw(amount);
String transaction = bank.getCustomer(account).getAccount().getAccountNum() + "\t" + dateFormatter.format(date) + "\t" + "Withdraw" + "\t" + amount;
WriterReader.appendToTabDelimitedFile(transaction, "transaction.txt");
}
private void makeLoan() {
}
private void checkBalance() {
double amount = 0;
int account = selectAccount();
if(account >= 0){
System.out.println(bank.getCustomer(account).getAccount());
}
}
private int selectAccount() {
ArrayList<Customer> customers = bank.getCustomers();
int account = -1;
if(customers.size() <= 0){
System.out.println("No customer in the system. Please create a new account.");
return -1;
}
for(int i = 0; i < customers.size(); i++ ){
System.out.println((i+1) + ") " + customers.get(i).info());
}
while(account < 0 || account > customers.size()){
System.out.print("Please select from the accounts: ");
try{
account = Integer.parseInt(keyboard.nextLine()) -1;
}catch(NumberFormatException ex){
account = -1;
}
if(account < 0 || account > customers.size()){
System.out.println("You have selected the wrong account selection.");
account = -1;
}
}
return account;
}
}