-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankApp.java
More file actions
137 lines (121 loc) · 4.9 KB
/
BankApp.java
File metadata and controls
137 lines (121 loc) · 4.9 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
import java.util.Scanner;
/**
* A class to test the BankAccount class. Uses a menu-driven system to
* allow the user to interactively test the BankAccount class.
*/
public class BankApp {
private static Scanner stdin = new Scanner(System.in);
public static void main(String[] args) throws IllegalArgumentException {
Bank bank = new Bank(100);
BankAccount account = new BankAccount(500.5,50505);
int choice;
double amount;
int accountNumber;
do {
choice = getUserChoice();
switch (choice) {
case 1:
amount = getAmount();
accountNumber = getAccountNumber();
try {
account = new BankAccount(amount, accountNumber);
bank.add(account);
System.out.println("Account info: " + account + "\n");
} catch (IllegalArgumentException exception) {
System.out.println("\n*****ERROR*****: " + exception.getMessage() + "\n");
}
break;
case 2:
amount = getAmount();
try {
account.deposit(amount);
System.out.println("Account info: " + account + "\n");
} catch (NullPointerException exception) {
System.out.println("\n*****ERROR*****: " + "No account! First find account"
+ " or create a new account\n");
} catch (IllegalArgumentException exception) {
System.out.println("\n*****ERROR*****: " + exception.getMessage() + "\n");
}
break;
//TODO TASK 3: add a InsufficientFunds catch clause below
case 3:
amount = getAmount();
try {
account.withdraw(amount);
System.out.println("Account info: " + account + "\n");
} catch (IllegalArgumentException exception) {
System.out.println("\n*****ERROR*****: " + exception.getMessage() + "\n");
} catch (NullPointerException e){
System.out.println("The Account does not exist!");
} catch (InsufficientFundsException e){
System.out.println("You can't withdraw more than you have!");
}
break;
case 4:
accountNumber = getAccountNumber();
BankAccount found = bank.find(accountNumber);
if (found != null) {
account = found;
System.out.println("Account info: " + account + "\n");
} else {
System.out.println("\n*****ERROR*****: Bank account " + accountNumber
+ " not found!\n");
}
break;
case 5:
System.out.print("\n\nThe accounts: \n" + bank + "\n\n");
break;
}
} while (choice != 0);
System.out.println("\n\nGoodbye!");
}
private static int getUserChoice() {
int choice;
do {
choice = -1;
System.out.println("Menu Options:");
System.out.println("0) Quit");
System.out.println("1) Create new account");
System.out.println("2) Deposit to current account");
System.out.println("3) Withdraw from current account");
System.out.println("4) Find account");
System.out.println("5) Print all accounts");
System.out.print("Enter your choice (0 - 5): ");
try {
choice = Integer.parseInt(stdin.nextLine());
} catch (NumberFormatException exception) {
}
if (choice < 0 || choice > 5)
System.out.println("Invalid choice");
} while (choice < 0 || choice > 5);
return choice;
}
private static double getAmount() {
System.out.print("Enter the amount: $ ");
double amount = -1;
boolean valid = false;
do {
try {
amount = Double.parseDouble(stdin.nextLine());
valid = true;
} catch (NumberFormatException exception) {
System.out.println("Make sure you enter a valid double!");
}
} while (!valid);
return amount;
}
private static int getAccountNumber() {
System.out.print("Enter the account number: ");
int amount = -1;
boolean valid = false;
do {
try {
amount = Integer.parseInt(stdin.nextLine());
valid = true;
} catch (NumberFormatException exception) {
System.out.println("Make sure you enter a valid integer!");
}
} while (!valid);
return amount;
}
}