-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
157 lines (138 loc) · 5.51 KB
/
Copy pathMain.java
File metadata and controls
157 lines (138 loc) · 5.51 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Load data from file
BankManger bankManager = new BankManger();
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to BroBro's Banking! Quick curve-side banking");
while (true) {
int count = 0;
System.out.println("\nWould you like to:");
System.out.println("1) Create an Account");
System.out.println("2) Make A Transaction");
System.out.println("3) Exit");
System.out.print("Action: ");
// Handle invalid input
if (!scanner.hasNextInt()) {
System.out.println("Invalid input. Please enter a number.");
scanner.next(); // Clear invalid input
continue;
}
int action = scanner.nextInt();
// Clear buffer
scanner.nextLine();
if (action == 1) {
createAccount(bankManager,scanner);
count++;
}
if (action == 2) {
makeTransaction(bankManager,scanner);
count++;
}
if(action == 3){
System.out.println("Thank you for using BroBro's Banking! Goodbye.");
// Save data before exiting
bankManager.shutdown();
break;
}
// If user choose invalid option
if(count ==0){
System.out.println("Invalid option. Please try again.");
}
}
scanner.close();
}
public static void createAccount(BankManger bankManager, Scanner scanner){
String firstName;
String lastName;
String password;
System.out.print("Enter First and Last Name: ");
String line = scanner.nextLine();
String[] fullName = line.split(" ");
// If user only enters first Name or doesn't enter First and Last xorrectly
if (fullName.length < 2) {
System.out.println("Please enter both first and last names.");
return;
}
firstName = fullName[0];
lastName = fullName[1];
// Prompt user to enter password
System.out.print("Please Enter your password: ");
password = scanner.nextLine();
// Make account
//Generate the random account number for new Customer
String accountNum = bankManager.setAccountNumber();
// create Account then add account to Customer Data
AccountManger account = new AccountManger(firstName, lastName, password, accountNum);
bankManager.addAccount(account);
System.out.println("Account created successfully!");
System.out.println(account.toString());
}
public static void makeTransaction(BankManger bankManger,Scanner scanner){
// Prompt user to enter four-digit account code to confirm if account exists
String accountPin;
System.out.print("Enter your Four-Digit account number: ");
accountPin = scanner.nextLine().trim();
// Now search for account Pin in customer data and if account is found then progress
if(bankManger.searchCustomer(accountPin)){
searchForCustomer(bankManger, scanner);
}
//If account isn't found within database
else{
while (true) {
int choice;
System.out.println("Account not found: \n1) Re-enter Pin\n2) Exit");
System.out.print("Action:");
choice = scanner.nextInt();
if(choice == 1){
System.out.print("Enter your Four-Digit account number: ");
scanner.nextLine(); // clear buffer
accountPin = scanner.nextLine().trim();
if(bankManger.searchCustomer(accountPin)){
searchForCustomer(bankManger, scanner);
}
}
if(choice ==2){
break;
}
}
}
}
//Hepler Method when searching for Customer Account
public static void searchForCustomer(BankManger bankManger, Scanner scanner){
while (true) {
int count =0;
int choice;
System.out.println("1) View Account Balance \n2) Make Deposit \n3) Withdraw \n4) exit ");
System.out.print("Action: ");
choice = scanner.nextInt();
if (choice == 1) {
System.out.println("Account Balance: " + bankManger.getBalance());
count++;
}
if (choice == 2) {
double amount;
System.out.print("Deposit Amount: ");
amount = scanner.nextDouble();
bankManger.setBalance(amount);
System.out.println("Account Balance: " + bankManger.getBalance());
count++;
}
if (choice == 3) {
double withdrawal;
System.out.print("Withdrawal Amount: ");
withdrawal = scanner.nextDouble();
bankManger.withdrawal(withdrawal);
System.out.println("Account Balance: " + bankManger.getBalance());
count++;
}
if(choice == 4){
break;
}
// if the user doesn't select one the options
if(count ==0) {
System.out.println("Invalid option. Please try again.");
}
}
}
}