Results 1 to 5 of 5
Thread: Array problems
- 10-09-2010, 11:31 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
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; } }
- 10-09-2010, 12:15 PM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
Braddy,
You are reinitializing history each time you add a new account. This should only be initialized once, say at customer creation.
Regards.
- 10-09-2010, 12:22 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
Hi Ronin
I understand that something like that is happening, but could you advise me what actions needed to avoid that? Thank you.
- 10-09-2010, 12:44 PM #4
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
The best place for it is as soon as the account type is determined.
e.g.
Regards.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]}
- 10-09-2010, 01:18 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Array problems!
By Addez in forum New To JavaReplies: 4Last Post: 08-29-2009, 06:56 PM -
Array problems..
By smokeviolent in forum New To JavaReplies: 1Last Post: 04-17-2009, 06:45 AM -
Array Index problems
By ragnor2004 in forum New To JavaReplies: 4Last Post: 03-26-2009, 07:53 PM -
Array problems
By Hosticus in forum New To JavaReplies: 2Last Post: 01-18-2009, 02:48 AM -
array problems need your help ASAP!
By notherand in forum New To JavaReplies: 1Last Post: 06-29-2008, 08:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks