Question: In Java: Here we have two classes, a bank and an account. In the main method in Bank.java, theres an infinite loop that lists all
In Java:
Here we have two classes, a bank and an account. In the main method in Bank.java, theres an infinite loop that lists all the accounts, prints out their balances, and prompts you to do a transfer from one account to another. By default the bank has 5 accounts in NUM_ACCOUNTS, which are in an array of Account objects. The account, in Account.java has an account id, and a balance. There should be methods to get the id and the balance, as well as methods to withdraw money and deposit money. deposit(double amount) should add money to the balance. withdraw(double amount) should subtract money to the balance if there is enough money in the account, and return true if so. If the withdrawal exceeds the balance, the withdraw method should return false.
every time you create an account, it should generate a new id.
The control code is in the main method in Bank.java, so you can see what it does. Your job is to fill in the code for the Bank and Account objects to make it happen.
When you create a Bank object, it should by default create 5 accounts with a balance of $1000.
In Bank.java, I have written all of the code in the main method. I have provided skeleton methods for some but not all of the methods and fields you must implement given the UMLs. Its your responsibility to implement everything in the UMLs.
The UMLs are as follows. A static field in all caps indicates it is final (ie, a constant):


import java.util.Scanner;
public class Bank {
public static final int NUM_ACCOUNTS = 5; public Bank() { // create NUM_ACCOUNTS accounts in an array // with each account having a starting balance // of $1000 } public Account getAccountById(int id) { // this should take an id, search // through the array of accounts until // it finds the account matching the id // and returns it, returning null if // it is not found return null; } public int numAccounts() { // returns the number of accounts: // ie, the length of the array accounts return 0; } public static void main(String[] args) { Scanner in = new Scanner(System.in); Bank bank = new Bank(); for (;;) { System.out.println("Accounts:"); Account[] accounts = bank.getAllAccounts(); for (int i=0;i
The bank class is to certain extende pre-wrote for convenience and guidelines, the question is asking for both the bank class and the account class.
Bank +NUM ACCOUNTS: int -accounts: Account() +Bank() +getAccountById(id: int): Account +numAccounts(): int +getAllAccounts(): Account[] Account -account count: int -balance: double -id: int +Account() +Account(startingBalance: double) +withdraw(amount: double): boolean +getBalance(): double +deposit(amount:double): void +getld(): int
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
