【问题标题】:Java: ArrayList item not returning when searched forJava:搜索时不返回 ArrayList 项
【发布时间】:2021-04-26 22:20:11
【问题描述】:

我正在尝试在我的主要方法中概述银行账户的ArrayList,调用对该列表中所有账户的搜索,如果该账户在那里......存款。程序的实际规格:

设计并实现一个按以下方式执行的程序:

• 程序启动时,会创建两个银行帐户,使用名称和 写入代码的数字;

• 然后要求用户输入帐号,然后输入金额 存入该帐户;

• 相应帐户的余额随后会相应更新—或者如果 输入了错误的帐号,因此会显示一条消息; 规范的这个搜索部分是什么让我失望。

• 然后询问用户是否希望进行更多存款;

• 如果用户回答确实希望进行更多存款,则流程继续;

• 如果用户不希望进行更多存款,则两个帐户的详细信息 (帐号、账户名称和余额)。

当我运行代码时,它会在找不到帐户时返回输出...显然,我定义的帐户并没有像我预期的那样被转移到ArrayList

BankAccount 类:

import java.util.ArrayList;

public class BankAccount {

     // the attributes
       private String accountNumber;
       private String accountName;
       private double balance;
       private char choice;
       
       //Notice the static attribute
       private static double interestRate;

       // the methods

       // the constructor
       public BankAccount(String numberIn, String nameIn)
       {
          accountNumber = numberIn;
          accountName = nameIn;
          balance = 0;
       }

       // methods to read the attributes
       public String getAccountName()
       {
          return accountName;
       }

       public String getAccountNumber()
       {
          return accountNumber;
       }

       public double getBalance()
       {
          return balance;
       }

       // methods to deposit and withdraw money
       public void deposit(double amountIn)
       {
          balance = balance + amountIn;
       }

       public void withdraw(double amountIn)
       {
         if (amountIn > balance) 
         {
             System.out.println("Sorry, there is an insufficient amount in your account to complete this withdrawal");
         } 
         else 
            {
             System.out.println("Withdrawal Successful");
             balance = balance - amountIn;
            }
       }
       
       public void setInterestRate(double rateIn)
       {  
          interestRate = rateIn;
       }
       
        public double getInterestRate()
       {  
          return interestRate;
       }
       
        public void addInterest()
       {  
         balance = balance + (balance *interestRate)/100;
       }
        
        
      
}

银行类:

import java.util.ArrayList;

public class Bank {
    
    ArrayList<BankAccount> list = new ArrayList<>();
    
    
    // helper method to find the index of a specified account
    
        private int search(String accountNumberIn)
        {
            for(int i = 0; i <= list.size() - 1; i++)
            {
                
            BankAccount tempAccount = list.get(i); // find the account at index i
            String tempNumber = tempAccount.getAccountNumber(); // get account number
            
                if(tempNumber.equals(accountNumberIn)) // if this is the account we are looking for
                {
                return i; // return the index
                }
            }
            return -999;
        }
        
    // return the total number of items
        
        public int getTotal()
        {
        return list.size();
        }
        
    // return an account with a particular account number
        
        public BankAccount getItem(String accountNumberIn)
        {
            int index = search(accountNumberIn);
            if(index != -999) // check that account exists
            {
                return list.get(index);
            }
            else
            {
                return null; // no such account
            }
    }
        
    // add an item to the list
        
        public boolean addAccount(String accountNumberIn, String nameIn)
        {
            if(search(accountNumberIn) == -999) // check that account does not already exist
            {
                list.add(new BankAccount(accountNumberIn, nameIn)); // add new account
                return true;
            }
            return false;
        }
        
    // deposit money in a specified account
        
        public boolean depositMoney(String accountNumberIn, double amountIn)
        {
            BankAccount acc = getItem(accountNumberIn);
            if(acc != null)
            {
                acc.deposit(amountIn);
                return true; // indicate success
            }
            else
            {
                return false; // indicate failure
            }
        }
        
    // withdraw money from a specified account
        
        public boolean withdrawMoney(String accountNumberIn, double amountIn)
        {
            BankAccount acc = getItem(accountNumberIn);
            if(acc != null && acc.getBalance() >= amountIn)
            {
                acc.withdraw(amountIn);
                return true; // indicate success
            }
            else
            {
                return false; // indicate failure
            }
        }
        
    // remove an account
        
        public boolean removeAccount(String accountNumberIn)
        {
            int index = search(accountNumberIn); // find index of account
            if(index != -999) // if account exists account
            {
                list.remove(index);
                return true; // remove was successful
            }
            else
            {
                return false; // remove was unsuccessful
            }
        }
    }


最后是带有 main 方法的测试类:

import java.util.ArrayList;

public class BankAccountTester {
    
     public static void main(String args[])
       {
          
         Bank myBank = new Bank();   
         ArrayList<BankAccount> accountList = new ArrayList<>();
            
           
         accountList.add(new BankAccount("123","Susan Richards"));
         accountList.add(new BankAccount("44567109","Delroy Jacobs"));
         accountList.add(new BankAccount("46376205","Sumana Khan"));
            
            char choice;
           
            
           do {
             
            System.out.println("Please enter your account number:");
            String myAcc = EasyScanner.nextString();
                
            BankAccount account = myBank.getItem(myAcc);
                
            System.out.println("Please enter an amount to deposit: ");
            double depIn = EasyScanner.nextDouble();
            
            boolean found = myBank.depositMoney(myAcc, depIn);
            
            if (found) 
            {
                System.out.println("Deposit made");
                
            } else 
            {
                System.out.println("Invalid account number"); //this is running everytime
            }
            
            System.out.println("Would you like to deposit again? (y/n)");
            choice = EasyScanner.nextChar();
            
           } while (choice == 'y' || choice == 'Y');
            
          System.out.println("Account Details..... \n");
          
          for(BankAccount item : accountList)
            {
                System.out.println("Account number: " + item.getAccountNumber());
                System.out.println("Account name: " + item.getAccountName());
                System.out.println("Current balance: " + item.getBalance());
                System.out.println();
            
            }
          
       
       }  
     
     
 }

我得到的输出如下:


Please enter your account number:
123
Please enter an amount to deposit: 
10
**Invalid account number**
Would you like to deposit again? (y/n)

我不知道为什么帐号没有被提取,虽然我可以猜到正在搜索的 ArrayList 没有保存已输入的帐户信息。

任何帮助将不胜感激!

【问题讨论】:

  • 您将新帐户添加到您的main 的列表中,但您从未通过addAccount() 将该帐户添加到您的myBank。这就是您的myBank 找不到该帐户的原因。
  • 另外:最好展示一个我们可以测试的例子。我们不知道 EasyScanner 是什么,也不知道它的作用

标签: java search arraylist account


【解决方案1】:

您必须添加到 myBank 中的 accountList 而不是 main。

     myBank.addAccount ("123","Susan Richards"));
     myBank.addAccount ("44567109","Delroy Jacobs"));
     myBank.addAccount ("46376205","Sumana Khan"));

【讨论】:

    猜你喜欢
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多