Results 1 to 5 of 5

Thread: Array problems

  1. #1
    braddy is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default Array problems

    I try to make an account program (console), with a customer class and 2 account classes that inherit from an abstract account class. My problem is that I'm trying to store historic in an array, and I only get one value when I retrieve it.

    For example if I make two deposits to one account and then I want to print both, I only get one.

    Sample from my code...
    Java Code:
    class Customer
    {
    private Account history[];
    private static final int HISTORY = 10;
    
    	public void makeDeposit(int p_account, double p_amount, String date) .
    	{
         //Search in my arraylist for that customer account
    		for (Account a: accountList)
    		{
         //Search for the right account if the customer has more than one...
    			if (p_account == a.getAccountNo()) 
    			{
    				a.setAmount(p_amount);
    				a.setDate(date);
    				a.Deposit(p_amount);
    				System.out.println("Deposit amount: " + p_amount);
    				
    				if (a.getType().equals("Saveaccount"))
    				{
    					history = new SavingsAccount[HISTORY];
    					setHistory(a);
    				}
    				else
    				{
    					history = new CreditAccount[HISTORY];
    					setHistory(a);
    				}
    			}	
    		}
    	}
    
    	private void setHistory (Account account)
    	{
    		int finder = findNr(history);
    		
    		if (finder == -1)
    		{
    			moveTrans(history); //This method is no issue yet...
    			history[HISTORY-1] = account;
    		}
    		else
    		{
    			history[finder] = account;
    		}
    	}
    	
    	private int findNr(Account[] history)
    	{
    		int finder = -1;
    		
    		for(int i = 0; i < HISTORY; i++)
    		{
    			if (history[i] == null)
    			{
    				finder = i;
    				break;
    			}
    		}
    		return finder;
    	}
    }

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    351
    Rep Power
    4

    Default

    Braddy,

    You are reinitializing history each time you add a new account. This should only be initialized once, say at customer creation.

    Regards.

  3. #3
    braddy is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi Ronin

    I understand that something like that is happening, but could you advise me what actions needed to avoid that? Thank you.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    351
    Rep Power
    4

    Default

    The best place for it is as soon as the account type is determined.

    e.g.
    Java Code:
    public Customer(String type) // Array initialized in constructor
    {[INDENT]setType(type);
    
    if(type.equals("Saveaccount"))
    [INDENT]history = new SavingsAccount[HISTORY];[/INDENT]
    else
    [INDENT]history = new CreditAccount[HISTORY];[/INDENT]		[/INDENT]}
    Regards.

  5. #5
    braddy is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Array problems!
    By Addez in forum New To Java
    Replies: 4
    Last Post: 08-29-2009, 06:56 PM
  2. Array problems..
    By smokeviolent in forum New To Java
    Replies: 1
    Last Post: 04-17-2009, 06:45 AM
  3. Array Index problems
    By ragnor2004 in forum New To Java
    Replies: 4
    Last Post: 03-26-2009, 07:53 PM
  4. Array problems
    By Hosticus in forum New To Java
    Replies: 2
    Last Post: 01-18-2009, 02:48 AM
  5. array problems need your help ASAP!
    By notherand in forum New To Java
    Replies: 1
    Last Post: 06-29-2008, 08:59 PM

Tags for this Thread

Posting Permissions

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