-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
127 lines (114 loc) · 4.02 KB
/
Copy pathMain.java
File metadata and controls
127 lines (114 loc) · 4.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
import java.util.Scanner;
public class Main {
// Add 'helper-methods'
public static void handleDeposit(Scanner sc, BankAccount currentAccount){
if (currentAccount != null){
System.out.println("Enter amount:");
try {
double amount = sc.nextDouble();
currentAccount.deposit(amount);
} catch (Exception e) {
System.out.println("Please enter a number!");
}
}
else{
System.out.println("Please login first!");
sc.nextLine(); //clear invalid input
return; // stop return early
}
}
public static void handleWithdraw(Scanner sc, BankAccount currentAccount){
if(currentAccount != null){
System.out.println("Enter amount to withdraw");
try {
double amount = sc.nextDouble();
currentAccount.withdraw(amount);
} catch (Exception e) {
System.out.println("Please enter a number!");
sc.nextLine(); // Clears invalid input
return; //Stops return early
}
}
else{
}
}
public static void handleTransactions(BankAccount curreAccount){
if(curreAccount != null){
curreAccount.showTransactions();
System.out.println("Thank you come again next time! ");
}else{
System.out.println("Please login first!");
}
}
public static void handleBalance(BankAccount currenAccount){
if(currenAccount != null){
System.out.println("Your Balance is: "+ currenAccount.getBalance());
System.out.println("Thank you!");
}
else{
System.out.println("Please login first!");
}
}
public static void main (String args []){
// Create an account using 'Object Instantiation',where 'bank' is a 'reference variable'
Bank bank = new Bank();
Scanner sc = new Scanner(System.in); //To allow user input
BankAccount currentAccount = null; //For creating account that stays logged in
System.out.println("Select your option...");
System.out.println("1. Create Account");
System.out.println("2. Login");
System.out.println("3. Deposit");
System.out.println("4. Withdraw");
System.out.println("5. Check Balance");
System.out.println("6. View Transactions");
System.out.println("7. Exit");
while (true) {
System.out.println("\n==== BANK MENU ====");
int choice ; // Waits for user to type a number
try {
choice = sc.nextInt();
}
catch(Exception e){
System.out.println("Invalid input!");
sc.nextLine(); // Clear bad input
continue; // Restart the loop
}
if (choice==1) {
System.out.println("Enter account number: ");
String accNo = sc.next();
System.out.println("Enter name: ");
String name = sc.next();
bank.createAccount(accNo, name);
System.out.println("Account created!");
}
else if(choice == 2){
System.out.println("Enter account number:");
String accNo = sc.next();
BankAccount acc = bank.getAccount(accNo);
if(acc != null){
currentAccount = acc; //set logged-in account
System.out.println("Login successful!");
}
else{
System.out.println("Account not found!");
}
}
else if(choice == 3){
handleDeposit(sc, currentAccount);
}
else if (choice == 4){
handleWithdraw(sc, currentAccount);
}
else if (choice == 5){
handleBalance(currentAccount);
}
else if(choice == 6){
handleTransactions(currentAccount);
}
else if(choice == 7){
System.out.println("Good bye!");
break;
}
}
}
}