Loop to deposit money into bank account array
Ask the user how much they would like to deposit into the first account and how much would they like to deposit into the second account. Update the accounts accordingly.
You must use a loop here to process the deposit for each account in the array.
How do i create a for loop that loops through the array and deposits into each account?
Code :
public class BankAccountTesting {
public static void main(String[] args) {
BankAccount[] account = new BankAccount[2];
account[0] = new BankAccount("12345","John Brown");
account[1] = new BankAccount("6789","Paul brown");
printAccounts(account);
System.out.println("How much to deposit to Account 1 ?");
double deposit1 = EasyScanner.nextDouble();
System.out.println("How much to deposit to Account 2 ?");
double deposit2 = EasyScanner.nextDouble();
for(int i = 0 ; i < account.length ; i++){
account[i].deposit();
}
printAccounts(account);
}
public static void printAccounts(BankAccount [] account){
for (int i = 0 ; i<2 ; i++){
System.out.println("Bank Account Details" + (i+1));
System.out.println("Account Name : " + account[i].getAccountName());
System.out.println("Account Number : " + account[i].getAccountNumber());
System.out.println("Balance : " + account[i].getBalance());
System.out.println(" ");}
}
}
Re: Loop to deposit money into bank account array
begin loop
get the next account object from the array
do the processing for that account
end loop
The do the processing step needs to be expanded into all the steps needed to process the account.
such as asking the owner some questions, getting his response and calling methods in the account object to update it
Re: Loop to deposit money into bank account array
Thanks. Should it take and store the variables then run the loop?
Code :
System.out.println("How much to deposit to Account 1 ?");
double deposit1 = EasyScanner.nextDouble();
System.out.println("How much to deposit to Account 2 ?");
double deposit2 = EasyScanner.nextDouble();
for(int i = 0 ; i < 2 ; i++){
account[i].deposit(Not sure what goes here);
}
Re: Loop to deposit money into bank account array
Everything should be done when looking at the account object inside the loop.
Re: Loop to deposit money into bank account array
Bit i cant figure out is how to move to the next deposit? Switch between deposit0 and deposit1 variables. Cant figure out the use of i.
Code :
for(int i = 0 ; i < 2 ; i++){
System.out.println("How much to deposit to Account " + (i + 1) + " ?");
double deposit0 ;
double deposit1 ;
deposit[i]= EasyScanner.nextDouble();
account[i].deposit(deposit[i]); }
Re: Loop to deposit money into bank account array
There should only be one deposit amount for the current account that is being processed. What if the array had 100s of accounts in it?
Re: Loop to deposit money into bank account array
Is this looking like its heading in the right direction?
Code :
for(int i = 0 ; i < 2 ; i++){
System.out.println("How much to deposit to Account " + (i + 1) + " ?");
double deposit ;
deposit = EasyScanner.nextDouble();
account[i].deposit(deposit);
}
Re: Loop to deposit money into bank account array
Try it and see what happens.
Re: Loop to deposit money into bank account array
Should have done that already :confused:
Seems to be working alright.
Norm youre a great help. Thanks a million.
One day ill look back and wonder how the hell i struggled with it but thats the way with learning anything i suppose.
Cheers
Re: Loop to deposit money into bank account array
Good attitude. Good luck in learning more java.