-
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...
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;
}
}
-
Braddy,
You are reinitializing history each time you add a new account. This should only be initialized once, say at customer creation.
Regards.
-
Hi Ronin
I understand that something like that is happening, but could you advise me what actions needed to avoid that? Thank you.
-
The best place for it is as soon as the account type is determined.
e.g.
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.
-
Thank you very much, I got it working now...