Results 1 to 2 of 2
  1. #1
    sammy123 is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default Java Arraylist help

    Right I have an Arraylist for bank accounts which is casuing me to bang my head several times below is an example of the class BankAccount.

    Java Code:
    public class BankAccount
    {
        private Name accountName;
        private Address accountAddress;
        private int balance;  // as pence
        private static int accountNumberGenerator = 10000001;
        private String accountNumber;
        
    
        /**
         * Constructor for BankAccount
         * @param number account number
         * @param name account name
         */
        public BankAccount(String firstName, String lastName,
                           String addr1, String addr2, String addr3, String postcode, double openingBalance)
        {
            accountNumber = "" + accountNumberGenerator++;
            accountName = new Name(firstName, lastName);
            accountAddress = new Address (addr1, addr2, addr3, postcode);
            balance = convertToPence(openingBalance);
        } 
        
        // accessors (queries)
    
        /**
         * @return the account number
         */
        public String getAccountNumber()
        { 
            return accountNumber;
        } 
    
        /**
         * @return the account name
         */
        public String getAccountName()
        { 
            return accountName.getFullName();
        } 
        
        /**
         * @return the account address
         */
        public String getAccountAddress()
        {
            return accountAddress.getFullAddress();
        }
    
        /**
         * @return the balance in the account
         */
        public String getBalance()
        { 
            return penceToPounds(balance);
        } 
        
        // mutators 
        
        /**
         * sets new value(s) for account name
         * 
         * @param firstName     first name
         * @param lastName      last name
         */
        public void changeAccountName(String firstName, String lastName)
        {
            accountName.setFirst(firstName);
            accountName.setLast(lastName);
        }
        
        /**
         * set new value(s) for account address
         * 
         * @param addr1     first line of address
         * @param addr2     second line of address
         * @param addr3     third line of address or ""
         * @param postcode  the postcode
         */
        
        public void changeAccountAddress(String addr1, String addr2, String addr3, String postcode)
        {
            accountAddress.setFullAddress(addr1, addr2, addr3, postcode);
        }
        
        /**
         * adds an amount to the balance
         * @param amount the amount to add to balance
         */
        public void deposit(double amount)
        { 
            balance = balance + convertToPence(amount);
        } 
    
        /**
         * withdraws a given amount from the account
         * @param amount the amount to withdraw
         * @return the new balance
         */
        public void withdraw(double amount)
        { 
            if (balance - convertToPence(amount) <= 0)
            {
                System.out.println("Transaction refused: Insufficient funds.");
            }
            else
            {
                balance = balance - convertToPence(amount);
            }
        } 
    
        // other methods
        private int convertToPence(double amount)
        {
            return (int)(100*amount);
        }
        
        private String penceToPounds(int pence)
        {
          int pounds;
          int remainingPence;
          String result;    
          pounds = pence / 100;
          remainingPence = pence % 100;    
          result = ""+pounds+".";    
          if (remainingPence < 10)
          {
            result = result+"0"+remainingPence;
          }
          else
          {
             result = result+remainingPence;
          }       
          return result;
        } 
        
         /**
         * Indicates whether some other object is "equal to" this one.
         * Checks for matches of accountNumber
         * @return true if a match found otherwise false
         */
        public boolean equals(Object o)
        {
            if ( o == this )
                return true;
            BankAccount account = (BankAccount)o;
            if ( (accountNumber.equals(account.getAccountNumber())))
                return true;
            return false;    
        }
        
        /**
         * @return all data values as a formatted string
         */
        public String toString()
        {
            String output = accountName.getFullName() +"\n" + accountAddress.getFullAddress() + "\n"
                            + "Account Number: " + accountNumber + "\tBalance: " + getBalance();
            
            return output;
        }
    
    }

    Below is the Arraylist class I am having problems with the RemoveAccount,makeDeposit and makeWithdrawl methods.

    I want them to use the findAccount method to return the index of a particular account based on the accountNumber. then make the changes within that method.

    Java Code:
    import java.util.*;
    
    public class BankAccountArrayList
    {
        private ArrayList < BankAccount > accounts;
    
        /**
         * Constructor for objects of class BankAccountArrayList
         */
        public BankAccountArrayList()
        {
            accounts = new ArrayList < BankAccount >();
        }
    
        public void openBankAccount(String firstName, String lastName,
        String addr1, String addr2, String addr3, String postcode, double openingBalance)
    
        {
    
            accounts.add(new BankAccount(firstName, lastName, addr1, addr2, addr3, postcode, openingBalance));
        }
    
        public void removeAccount(String accountNumber)
    
        {
            int index = 0;
            index = findAccount( accountNumber);
            
            accounts.remove(accountNumber);
    
        }
    
        public int getNumberOfBankAccounts()
    
        {
            return accounts.size();
    
        }
        
        
        public void printAccount(String accountNumber)
        
        {
             int index = 0;
             index = findAccount(accountNumber);
             
            
        }
        
        public String toString()
        
        {   String output = "";
            for ( BankAccount acc : accounts ) 
                output = output + acc +"\n";
            return output;
            
            
        }
        
        public void makeDeposit(String accountNumber,double amount)
        
        {
             int index = 0;
             index = findAccount(accountNumber);
            
             
             
            
            
        }
        
        public void makeWithdrawal(String accountNumber, double amount)
        
        {
             int index = 0;
             index = findAccount(accountNumber);
            
            
            
            
        }
        
       
        public int findAccount(String accountNumber)
        
            
      {  
       
            int index = 0;
            for ( BankAccount bankAccount : accounts)
           {
            if( bankAccount.getAccountNumber().equals(accountNumber))         
                return index;
                
                index++;
                
            
              }
            
            return index -1;
    
        
        
    }
    }

  2. #2
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Let us know what the error you are getting or whats the deviation you are getting.Try to be more specific so that guys in here can help you.

    What will this do .. accountNumber = "" + accountNumberGenerator++

    Try creating two accounts and print the account numbers for each. Try being more specific.

    Happy Coding ....

    warm regards
    Vinod M
    Last edited by Vinod Mukundan; 12-02-2010 at 07:35 AM.
    _______________________________________________
    give me beans .........

Similar Threads

  1. Replies: 7
    Last Post: 11-22-2010, 02:26 PM
  2. Java ArrayList : Generics & Sorting
    By Eranga in forum Java Tutorial
    Replies: 0
    Last Post: 10-19-2010, 05:43 AM
  3. java collection arraylist in jdk 1.4
    By jagdish_ in forum Advanced Java
    Replies: 1
    Last Post: 06-12-2010, 03:19 PM
  4. Java ArrayList out of bounds exception
    By grahamb314 in forum New To Java
    Replies: 5
    Last Post: 11-22-2008, 08:21 PM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •