Help with Arrays

Help with a few Array methods

Page 1 of 1

5 Replies - 1005 Views - Last Post: 07 December 2010 - 07:38 PM Rate Topic: -----

#1 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

Help with Arrays

Posted 07 December 2010 - 02:19 PM

Right I am back again for some advice on Arrays this time basically I am using Arrays to store details of a customers account. This program uses several classes all of which are correct I am having some issues with two Methods removeAccount() and MakeWithdrawl()

Basically for the removeAccount I need to remove a specfic account from a inpout accountNumber of type string I then need to rmeove that accont and also use the squash method for the index in the array.

The MakeWithdrawl method needs to withdraw an amount once again give the accountNumber and then enter an amount to withdrawl the withdrawl code is in another class so I just need to call up the accountNumber using the findAccount number then store that index and change the data for that.

Here is the code hope the above makes sense.


import java.util.*;

public class BankAccountArray
{
    private BankAccount [] accounts;
    private int nextAccountIndex;

    public BankAccountArray(int noAccounts)

    {
        accounts = new BankAccount[noAccounts];
        nextAccountIndex = 0;

    }

    public void openBankAccount(String firstName, String lastName,
    String addr1, String addr2, String addr3, String postcode, double openingBalance)

    {
        if (nextAccountIndex < accounts.length)

        {
            accounts[nextAccountIndex] = (new BankAccount(firstName, lastName, addr1, addr2, addr3, postcode, openingBalance));
            nextAccountIndex++;
            
        }
        

    }
    
    public void removeAccount(String accountNumber)
    
    {
        
        
        
    }
    
    /**
     * Allows user to make deposit to specified account Number
     */
   
    public void makeDeposit(String accountNumber,double amount)
    
    {
     
     
        
       
       
     }
     
      /**
     * Allows user to make withdrawl to specified account Number providing account has enough funds
     */
    
    public void makeWithdrawal(String accountNumber, double amount)
    
    {
         
        
     }
     
     public String printAccount(String accountNumber)
    
    {
        
        
         
        
    }

    public int getNumberOfBankAccount()

    {
        return nextAccountIndex;

    }
    
    public String toString()
    
    {
        String output =" ";
        for (BankAccount ba : accounts)
        output = output + accounts + "\n";
        return output;
    }
    
    

    private int findAccount(String accountNumber)
    {   
        int index = 0;
        boolean found = false; 
        while ( index < nextAccountIndex && !found)

        {
            if (accounts[index].equals(accountNumber))
            {
                found = true;
            }
            else
                index++;

        }
        if (found)
            return index;
        else
            return -1;
    }
    
    private void squash(int index)
    {
        if ( index>= 0)
        
        {
            for ( int i = index ; i < nextAccountIndex - 1 ; i++)
            accounts[i] = accounts [i+1];
            accounts [ nextAccountIndex - 1] = null;
            nextAccountIndex--;
        }
    }
            

}



This post has been edited by sammy123: 07 December 2010 - 02:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with Arrays

#2 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Help with Arrays

Posted 07 December 2010 - 02:22 PM

What you want to do is:
- loop through the array of accounts to see if the account number requested for deletion is valid
- if valid, you are to remove that account
- make a temporary array with n - 1 length
- fill up except for the account to be removed
- do system.arraycopy

It is always easier to use an ArrayList as it does the resizing automatically for you.
Was This Post Helpful? 0
  • +
  • -

#3 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

Re: Help with Arrays

Posted 07 December 2010 - 02:32 PM

I know its just we have to show we can use Arrays as well as ArrayLists I have done the later just struggling with the Arrays I'm still lost can you please give me abit more advice I am really new to Java and just been using it for a few weeks.

I just havent got a clue thats all I know in Arraylists I would use the get(index) method.
Was This Post Helpful? 0
  • +
  • -

#4 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

Re: Help with Arrays

Posted 07 December 2010 - 04:35 PM

Here is my updated code now when I compile it comes back saying cannot find constructor BankAccountArray() also the findAccount always returns null pointer exception.


public class BankAccountArray
{
    private BankAccount [] accounts;
    private int nextAccountIndex;

    public BankAccountArray(int noOfAccounts)

    {
        accounts = new BankAccount [noOfAccounts];
        nextAccountIndex = 0;

    }

    public void openBankAccount(String firstName, String lastName,
    String addr1, String addr2, String addr3, String postcode, double openingBalance)

    {
        if (nextAccountIndex < accounts.length)

        {
            accounts[nextAccountIndex] = (new BankAccount(firstName, lastName, addr1, addr2, addr3, postcode, openingBalance));
            nextAccountIndex++;
            
        }
        else
        System.out.println("Error more accounts than allowed");
        

    }
    
    public void removeAccount(String accountNumber)
    
    {
     int index = 0;
     index = findAccount(accountNumber);
     if(index == -1)
     System.out.println("Not a valid account number");
     else
     squash( index);
     
     
        
      
        
        
        
        
    }
    
    /**
     * Allows user to make deposit to specified account Number
     */
   
    public void makeDeposit(String accountNumber,double amount)
    
    {
        int index = findAccount(accountNumber) ;
        if ( index == -1 )
        System.out.println("No Account Number");    
        else
        
        accounts[index].deposit(amount);
            
           
    }


    
    
     
      /**
     * Allows user to make withdrawl to specified account Number providing account has enough funds
     */
    
    public void makeWithdrawal(String accountNumber, double amount)
    
    {   int index = findAccount(accountNumber) ;
        if ( index == -1 )
        System.out.println("No Account Number");    
        else
        
        accounts[index].withdraw(amount);
         
        
     }
     
     public String printAccount(String accountNumber)
    
    {
         
       int index = findAccount(accountNumber) ;
        if ( index == -1 )
        System.out.println("No Account Number");    
        else
        System.out.println(accounts);
        return
        "printed";
        
                
        
        
         
        
    }

    public int getNumberOfBankAccount()

    {
        return nextAccountIndex;

    }
    
    public String toString()
    
    {
        String output =" ";
        for (BankAccount ba : accounts)
        output = output + accounts + "\n";
        return output;
    }
    
    

  

     public int findAccount(String accountNumber)
   {   
         
   
        int index = 0;
        for ( BankAccount ba: accounts)
       {
            if( ba.getAccountNumber().equals(accountNumber))        
            return index;             
            index++;
             
         
              }
         
            return index -1;
    }





    
    private void squash(int index)
    {
        if ( index>= 0)
        
        {
            for ( int i = index ; i < nextAccountIndex - 1 ; i++)
            accounts[i] = accounts [i+1];
            accounts [ nextAccountIndex - 1] = null;
            nextAccountIndex--;
        }
    }
            

}



Was This Post Helpful? 0
  • +
  • -

#5 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Help with Arrays

Posted 07 December 2010 - 04:37 PM

I wrote a tutorial on a banking system, it may help with the methods in question and also using Arrays, it can be found here.
Was This Post Helpful? 0
  • +
  • -

#6 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

Re: Help with Arrays

Posted 07 December 2010 - 07:38 PM

Thanks for that had a look can anyone help with the above issues thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1