Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

0 I'm working on creating BankAccount class, but when I run this code, seems like the new input is not added to the ArrayList. I

image text in transcribedimage text in transcribedimage text in transcribed

0

I'm working on creating BankAccount class, but when I run this code, seems like the new input is not added to the ArrayList. I think I made the addaccount method correctly, but it doesn't work, so could you tell me where I'm wrong. Also I'm asked to create the constructor to throw a BankAccountException with appropriate message, if the account number passed to the constructor is not a 4-digit number and throw a BankAccountException with appropriate message, if the initialBalance is less than zero. however, how can we throw the multiple exception using same exception? Please tell if I miss some infomation.

here is my code so far

Bank.java

import java.util.ArrayList; public class Bank { private ArrayList accounts; public Bank() { accounts = new ArrayList(); } public void addAccount(BankAccount a) { accounts.add(a); } public double getTotalBalance() { double total = 0; for(BankAccount a : accounts) { total = total + a.getBalance(); } return total; } /** Counts the number of bank accounts whose balance is at least a given value. @param atLeast the balance required to count an account @return the number of accounts having least the given balance */ public int count(double atLeast) { int matches = 0; for(BankAccount a : accounts) { if (a.getBalance() >= atLeast) matches++; } return matches; } /** Finds a bank account with a given number. @param accountNumber the number to find @return the account with the given number, or null if there is no such account */ public BankAccount find(int accountNumber) { for(BankAccount a : accounts) { if (a.getAccountNumber() == accountNumber) return a; } return null; } public BankAccount deposit(int accountNumber, double amounts, double balance) { for(BankAccount a : accounts) { if(a.getAccountNumber() == accountNumber) { a.deposit(amounts); System.out.println("Amount deposited : "+ balance); //amount deposited } } return null; } public BankAccount withdraw(int accountNumber, double amounts, double balance) { for(BankAccount a : accounts) { if(a.getAccountNumber() == accountNumber) { a.withdraw(amounts); System.out.println("Amount withdrawn : "+ balance); //amount deposited } } return null; } /** Gets the bank account with the largest balance. @return the account with the largest balance, or nullpointerexception if the bank has no accounts */ public BankAccount getMaximum() { if (accounts.size() == 0) { throw new NullPointerException("the bank has no accounts"); } BankAccount largest = accounts.get(0); for (int i = 1; i  largest.getBalance()) largest = a; } System.out.println("largest balance of BankAccount: "+ largest); return largest; } /** Gets the bank account with the smallest balance. @return the account with the smallest balance, or nullpointerexception if the bank has no accounts */ public BankAccount getMinimum() { if (accounts.size() == 0) { throw new NullPointerException("the bank has no accounts"); } BankAccount smallest = accounts.get(0); for (int i = 1; i  

BankAccount.java

import java.io.IOException; public class BankAccount { private static double balance; private static int accountNumber; public BankAccount(int anAccountNumber)throws IOException { IOException exception1 = new IOException("account number is not 4 digits"); IOException exception2 = new IOException("initial balance is less than 0"); accountNumber = anAccountNumber; balance = 0; } public BankAccount(int anAccountNumber, double initialBalance){ balance = initialBalance; accountNumber = anAccountNumber; } public double getBalance() { return balance; } public int getAccountNumber(){ return accountNumber; } public void deposit(double amount){ balance += amount; } public void withdraw(double amount)throws RuntimeException { balance -= amount; if(balance 

BankAccountException.java

public class BankAccountException extends RuntimeException { public BankAccountException(){} public BankAccountException(String message){ super(message); } }

BankException.java

public class BankException extends RuntimeException { public BankException(){ } public BankException(String message){ super(message); } }

BankTester.java

import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class BankTester { public static void main(String[] args) throws IOException { //Read user input using scanner Scanner sc=new Scanner(System.in); int choice=0; //Create Bank object Bank Bank=new Bank(); //Loop until user quits while(choice!=7) { System.out.println("Menu 1. Add a new BankAccount to the Bank 2. Get the balance of a BankAccount based on the account number. 3. " + "Deposit money to an existing BankAccount 4. Withdraw money from an existing BankAccount" + " 5. Find a BankAccount with the highest balance 6. Find a BankAccount with the lowest balance 7. Quit"); System.out.print("Enter your choice(or 7 to quit): "); choice=sc.nextInt(); sc.nextLine(); switch(choice) { //Add a new BankAccount to the Bank case 1: try { System.out.print("Enter the account number: "); int accountNumber = sc.nextInt(); if (accountNumber  9999) { throw new IOException("Account Number is invalid!"); } System.out.println("Enter the initial balance: "); double initial = sc.nextDouble(); if(initial 9999) { throw new IOException("Account Number is invalid!"); } else { BankAccount x = Bank.find(num); if (x != null) { System.out.println("Account infomation: " + x.toString()); } else { throw new NullPointerException("The Bank dosen't have such a account"); } } } catch(IOException exception) { System.out.println("Account Number is invalid"); } catch(NullPointerException exception) { System.out.println("The Bank dosen't have such a account"); } break; case 3: //Deposit money to an existing BankAccount try { System.out.print("Enter Account Number: "); int num=sc.nextInt(); sc.nextLine(); //Student number starts with 1000 if(num  9999) { throw new IOException("Account Number is invalid!"); } else { BankAccount x = Bank.find(num); if (x != null) { System.out.println("Enter the amount of money you want to deposit"); double amounts = sc.nextDouble(); double balance = x.getBalance(); Bank.deposit(num,amounts,balance); } else { throw new NullPointerException("The Bank doesn't have such a account"); } } } catch(IOException exception) { System.out.println("Account Number is invalid"); } catch(NullPointerException exception) { System.out.println("The Bank doesn't have such a account"); } break; case 4: //Withdraw money from an existing BankAccount try { System.out.print("Enter Account Number: "); int num = sc.nextInt(); sc.nextLine(); //Student number starts with 1000 if (num  9999) { throw new IOException("Account Number is invalid!"); } else { BankAccount x = Bank.find(num); if (x != null) { System.out.println("Enter the amount of money you want to withdraw"); double amounts = sc.nextDouble(); double balance = x.getBalance(); Bank.withdraw(num, amounts, balance); } else { throw new NullPointerException("The Bank doesn't have such a account"); } } } catch(NullPointerException exception) { System.out.println("The Bank doesn't have such a account"); } catch(IOException exception) { System.out.println("Account Number is invalid"); } catch(RuntimeException exception) { System.out.println("The balance becomes less than zero"); } break; case 5: //Find a BankAccount with the highest balance try { double atLeast = 0; Bank.count(atLeast); Bank.getMaximum(); } catch(NullPointerException exception) { System.out.println("the bank has no accounts"); } break; case 6: //Find a BankAccount with the lowest balance try { Bank.getMinimum(); } catch(NullPointerException exception) { System.out.println("the bank has no accounts"); } break; case 7: System.out.println("End"); break; default: System.out.println("Invalid choice! Please retry!"); } } sc.close(); } }
O Create a Bank object. Provide a menu for the user to: Add a new BankAccount to the Bank. Get the balance of a BankAccount based on the account number. o Deposit money to an existing BankAccount. Withdraw money from an existing BankAccount. Find a BankAccount with the highest balance. Find a BankAccount with the lowest balance. O Complete the implementation of BankAccount class as follow: o Modify the BankAccount constructors to throw a BankAccountException with appropriate message, if the account number passed to the constructor is not a 4-digit number. o Modify the BankAccount constructor to throw a BankAccountException with appropriate message, if the initialBalance is less than zero. o Modify the withdraw method to throw a BankAccountException with appropriate message, if the balance becomes less than zero. O O Create a program the BankTester class with a main method, which creates a Bank object and manipulates BankAccount objects based on user input. Modularize the main program. In this BankTester program, you are to ask the user for input using the Scanner class. Print a 'menu' for the user to know what the possible commands are. Handle all exceptions generated by the BankAccount and Bank methods for invalid input. In this program, most of the errors are detected by the BankAccount and Bank methods. O O O

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Entity Alignment Concepts Recent Advances And Novel Approaches

Authors: Xiang Zhao ,Weixin Zeng ,Jiuyang Tang

1st Edition

9819942527, 978-9819942527

More Books

Students also viewed these Databases questions