Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Loop to deposit money into bank account array

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

    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(" ");}       
        }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop to deposit money into bank account array

    Thanks. Should it take and store the variables then run the loop?

    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);
     
     
            }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop to deposit money into bank account array

    Everything should be done when looking at the account object inside the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.


            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]); }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop to deposit money into bank account array

    Is this looking like its heading in the right direction?

     
            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); 
             }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop to deposit money into bank account array

    Try it and see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop to deposit money into bank account array

    Should have done that already

    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

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop to deposit money into bank account array

    Good attitude. Good luck in learning more java.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. bank account
    By rutchel alferez in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 9th, 2013, 08:31 AM
  2. Deposit Money
    By spyxdaxworld in forum Java Theory & Questions
    Replies: 1
    Last Post: October 25th, 2012, 11:55 PM
  3. Bank Account HELP!!!! PLEASE.
    By metaleddie13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 16th, 2011, 08:06 PM
  4. Bank Account Program.
    By Punky0214 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 17th, 2010, 10:42 PM
  5. Please help - Bank account application
    By brandonssss in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 2nd, 2010, 03:10 PM