I have a problem trying to understand how to get one out of the arraylist print out
say this one "noel - 2001 - 5446.0 // is a passbook account " and use it i.e make a deposit
into it or a withdrawal and then update the record resave it back into the arraylist??
if you can give me some examples it would be great thank you Noel
super class is abstract BankAccount.
SavingAccount extends BankAccount
their are three children classes
1.PassBookAccount extends SavingAccount
2.InvestmentAccount extends SavingAccount
3.ChequeAccount extends SavingAccount
this 3 are added to the arraylist as objects using the main contructor parms
ArrayList<BankAccount> savingAccountList =newArrayList<BankAccount>();
if(openingDep > 0.01 && CustomerName != "" )
{
savingAccountList.add(new PassBookAccount(CustomerName,openingDep));
System.out.println("Thank Account has been added");
}
noel - 2001 - 5446.0 // is a passbook account
jane - 2002 - 5443.0 // is a passbook account
null - 1001 - 234434.0 // is a investment account
bill - 3001 - 345.0 // is a cheque account
______________________
| - PLEASE SELECT - |
| Enter Number only |
|_____________________|
| 0.Quit |
| 1.Add new account |
| 2.Withdraw |
| 3.Deposit |
| 4.Update overdraft |
| 5.Add interest |
| 6.Print balance |
| 7.Print all |
| 8.New Customer | // currently used to display arraylist
|_____________________|
if (s == 8)
{
// using the bankaccount as the super
for(BankAccount q : savingAccountList)
{
System.out.println(q.getAccountName()+ " - " + q.getAccountNumber() + " - " + q.getCurrantBalance());
}
}// end if == 8
}while(s !=0);
badly need arraylist help please
Started by noel, Mar 19 2010 07:51 PM
6 replies to this topic
#1
Posted 19 March 2010 - 07:51 PM
|
|
|
#2
Posted 20 March 2010 - 01:08 AM
does you BankAccount have these methods?
If not, then you should add them as abstract methods.
q.getAccountName()+ " - " + q.getAccountNumber() + " - " + q.getCurrantBalance()
If not, then you should add them as abstract methods.
#3
Posted 20 March 2010 - 01:36 AM
yes it does but i just don't know have to get the right account out of the arraylist and work with it and put it back in the is not a lot about it in books or online. that i could find
#4
Posted 20 March 2010 - 02:53 AM
you'll have to read every object from the arraylist in a loop until you find the object (account) you need
than you put the object somewhere else, you delete it from the arraylist (using the index you found while searching the object)
you change the object (withdraw money for example)
when you're finished you simply add the object back to the arraylist
than you put the object somewhere else, you delete it from the arraylist (using the index you found while searching the object)
you change the object (withdraw money for example)
when you're finished you simply add the object back to the arraylist
#5
Posted 20 March 2010 - 02:55 PM
thank you for that but could you please give me an example in code (with three values) so i can tranlate it to my script needs as this is the area that i have little knowlege in thank you Noel
#6
Posted 21 March 2010 - 01:59 AM
Whats wrong with
for(BankAccount q : savingAccountList)
{
if(q.getAccountNumber() == 1001) {
//Change q via any of its methods. ... eg. q.setBalance(2000);
// break; if required
}
}
I believe as long as its the same object from the arraylist you are modifying ... the changes will be propagated inside arraylist too. It'd be wrong if you do something like ... q = new SavingAccount(1002); and expect the object to be updated inside it.
Also, i am presuming that BankAccount has all these abstract methods you're calling on the object.
for(BankAccount q : savingAccountList)
{
if(q.getAccountNumber() == 1001) {
//Change q via any of its methods. ... eg. q.setBalance(2000);
// break; if required
}
}
I believe as long as its the same object from the arraylist you are modifying ... the changes will be propagated inside arraylist too. It'd be wrong if you do something like ... q = new SavingAccount(1002); and expect the object to be updated inside it.
Also, i am presuming that BankAccount has all these abstract methods you're calling on the object.
#7
Posted 22 March 2010 - 10:31 PM
Thank you all for your help i have worked it out and all is well :)


Sign In
Create Account


Back to top









