银行帐户程序元素未添加到数组列表中

问题描述

我正在创建 BankAccount 类,但是当我运行此代码时,新输入似乎没有添加ArrayList。我想我正确地制作了 addaccount 方法,但它不起作用,所以你能告诉我我哪里错了。如果传递给构造函数的帐号不是 4 位数字,我还被要求创建构造函数以抛出带有适当消息的 BankAccountException 并抛出带有适当消息的 BankAccountException,如果initialBalance 小于零。但是,我们如何使用相同的异常抛出多个异常?请告诉我是否遗漏了一些信息。

Bank.java

import java.util.ArrayList;

public class Bank {
    private ArrayList<BankAccount> accounts;

    public Bank()
    {

        accounts = new ArrayList<BankAccount>();
    }

    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 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 < accounts.size(); i++)
        {
            BankAccount a = accounts.get(i);
            if (a.getBalance() > 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 < accounts.size(); i++)
        {
            BankAccount a = accounts.get(i);
            if (a.getBalance() < smallest.getBalance())
                smallest = a;
        }
        System.out.println("smallest balance of BankAccount: "+ smallest);
        return smallest;
    }

    public String toString()
    {
        return "Bank " + accounts.toString();
    }
}

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<0)
        {
            throw new RuntimeException("The balance becomes zero");
        }
    }


    @Override
    public String toString(){

        return "Account Number: " + accountNumber + "  balance: " + 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\n1. Add a new BankAccount to the Bank\n2. Get the balance of a BankAccount based on the account number.\n3. " +
                    "Deposit money to an existing BankAccount\n4. Withdraw money from an existing BankAccount"
                    + "\n5. Find a BankAccount with the highest balance\n6. Find a BankAccount with the lowest balance\n7. 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 < 1000 || accountNumber > 9999)
                        {
                            throw new IOException("Account Number is invalid!");
                        }
                        System.out.println("Enter the initial balance: ");
                        double initial = sc.nextDouble();
                        if(initial<0)
                        {
                            throw new RuntimeException("initial balance is less than 0");
                        }
                        BankAccount a = new BankAccount(accountNumber,initial);
                        Bank.addAccount(a);

                        System.out.println("BankAccount added successfully!");
                    }
                    catch(IOException exception)
                    {
                        System.out.println("Account Number is invalid!");

                    }
                    catch(RuntimeException exception)
                    {
                        System.out.println("initial balance is less than 0");
                    }


                    break;

                case 2:
                    //Get the balance of a BankAccount based on the account number
                    try {
                        System.out.print("Enter the account number: ");
                        int num = sc.nextInt();
                        sc.nextLine();
                        //Student number starts with 1000
                        if (num < 1000 || num > 9999)
                        {
                            throw new IOException("Account Number is invalid!");
                        } else
                            {
                            BankAccount x = Bank.find(num);
                            if (x != null)
                            {
                                System.out.println("Account infomation: \n" + 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 < 1000 || 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 < 1000 || 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,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();
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)