Repository navigation
Expand file tree
/
Copy pathBankApp.java
More file actions
59 lines (56 loc) · 2.46 KB
/
Copy pathBankApp.java
File metadata and controls
59 lines (56 loc) · 2.46 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
import java.io.IOException;
import java.util.Scanner;
/**
* The BankApp class is the driver for the banking application.
* It lets the user create accounts (NewAccount) and deposit/withdraw (AccessAccount).
* The logged-in account is held in an Account variable, so Java picks AccessAccount's
* overriding deposit()/withdraw() at runtime (polymorphism / dynamic dispatch).
*/
public class BankApp {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.println("Enter your choice (1-5):");
System.out.println();
System.out.println(" 1. Create new account without deposit");
System.out.println(" 2. Create new account with deposit an amount");
System.out.println(" 3. Deposit an amount to an existing account");
System.out.println(" 4. Withdraw an amount from an account");
System.out.println(" 5. Exit");
System.out.println();
System.out.print(" Enter your choice: ");
if (!in.hasNextLine()) break; // input ended
String choice = in.nextLine().trim();
switch (choice) {
case "1":
NewAccount.createAccount(in, false);
break;
case "2":
NewAccount.createAccount(in, true);
break;
case "3": {
Account account = AccessAccount.login(in); // Account reference, AccessAccount object
if (account == null) break;
double amount = Account.readAmount(in, "Enter amount to be deposited: ");
account.deposit(amount); // runs AccessAccount's overriding version
break;
}
case "4": {
Account account = AccessAccount.login(in);
if (account == null) break;
double amount = Account.readAmount(in, "Enter amount you want to withdraw: ");
account.withdraw(amount);
break;
}
case "5":
System.out.println("Goodbye!");
running = false;
break;
default:
System.out.println("Invalid choice. Please enter a number from 1 to 5.");
}
}
in.close();
}
}