import cs1.Keyboard; /** Simulate a bank account manager. @author Greg Vogl last modified 2003-10-11 */ public class BankAccounts { /** Use the BankAccount class to manage multiple bank accounts. */ public static void main (String[] args) { // Explain the program System.out.println("This program simulates a bank account manager."); // create an array of accounts accounts = new BankAccount[MAX_ACCOUNTS]; int choice; // user's menu choice // loop until the user exits do { displayActiveAccount(); displayMenu(); choice = Keyboard.readInt(); // get user's choice switch (choice) // view choice and perform action { case MENU_CREATE: create(); break; case MENU_DEPOSIT: deposit(); break; case MENU_WITHDRAW: withdraw(); break; case MENU_SELECT: select(); break; case MENU_DISPLAY_ACTIVE: displayActiveAccount(); break; case MENU_DISPLAY_ALL: displayAllAccounts(); break; case MENU_EXIT: break; default: System.out.println("Invalid menu selection, please try again."); break; } } while (choice != MENU_EXIT); } /** Display a menu of choices. */ public static void displayMenu () { System.out.println("Bank Account Menu"); String[] choices = { "Create Account", "Deposit", "Withdrawal", "Select Account", "Display Active Account", "Display All Accounts", "Exit" }; for (int i = 1; i <= choices.length; i++) System.out.println(i + " " + choices[i-1]); System.out.println("Type a number and press Enter."); } /** Create an account. */ public static void create() { if (naccounts < MAX_ACCOUNTS) { System.out.println("Enter the account name."); String name = Keyboard.readString(); activeAccount = naccounts; accounts[naccounts++] = new BankAccount(name); } else { System.out.println("Error: Not enough space in array to create a new account."); } } /** Deposit a non-negative amount into an account. */ public static void deposit() { if (activeAccount == -1) { System.out.println("Error: No accounts exist."); return; } System.out.println("Enter a deposit"); int amount = Keyboard.readInt(); if (accounts[activeAccount].deposit(amount)) System.out.println("Deposit succeeded."); else System.out.println("Deposit must be a positive number."); } /** Withdraw a non-negative amount from an account. */ public static void withdraw() { if (activeAccount == -1) { System.out.println("Error: No accounts exist."); return; } System.out.println("Enter a withdrawal"); int amount = Keyboard.readInt(); if (accounts[activeAccount].withdraw(amount)) System.out.println("Withdrawal succeeded."); else System.out.println("Withdrawal must be a positive number and not exceed the balance."); } /** Select an account. */ public static void select() { System.out.println("Enter an account number."); int account = Keyboard.readInt(); if (account < 0 || account >= naccounts) System.out.println("Account does not exist."); else activeAccount = account; } /** Display the active account. */ public static void displayActiveAccount() { System.out.println("\nActive Account: " + (activeAccount == -1 ? "None" : accounts[activeAccount].toString())); } /** Display all existing accounts. */ public static void displayAllAccounts() { System.out.println(naccounts + " Bank Accounts:"); for (int i=0; i