Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
703 views
in Technique[技术] by (71.8m points)

search - Java: ArrayList item not returning when searched for

I am trying to outline an ArrayList of bank accounts in my main method, call a search on all accounts in that list, if the account is there...deposit money. Actual specs of program:

Design and implement a program that performs in the following way:

? When the program starts, two bank accounts are created, using names and numbers which are written into the code;

? The user is then asked to enter an account number, followed by an amount to deposit in that account;

? The balance of the appropriate account is then updated accordingly—or if an incorrect account number was entered a message to this effect is displayed; this search portion of the spec is whats throwing me.

? The user is then asked if he or she wishes to make more deposits;

? If the user answers does wish to make more deposits, the process continues;

? If the user does not wish to make more deposits, then details of both accounts (account number, account name and balance) are displayed.

When i run the code it returns output for when the account is not found... clearly the accounts I am defining arent being transferred to the ArrayList as im expecting.

BankAccount class:

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;
       }
        
        
      
}

Bank Class:

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
            }
        }
    }


And finally the testing class with main method:

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..... 
");
          
          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();
            
            }
          
       
       }  
     
     
 }

The output i get is as follows:


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

I cant figure out why the account number isnt being picked up, although I can guess that the ArrayList being searched doesnt hold the account information that has been input.

Any help would be appreciated!

question from:https://stackoverflow.com/questions/65845334/java-arraylist-item-not-returning-when-searched-for

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to add to accountList in myBank not in main.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...