I have an assignment where I am creating a BankAccount program. I have to create an ArrayList of BankAccounts and have the program ask the user how many accounts to create and then have a loop to ask the user for the accountId, name, and initial balance for each account and the add the bank account to the ArrayList. Below is what I have so far but I am stuck on how to do the loop to ask the user for the information and put it into the ArrayList.

BankAccount.java

public class BankAccount {

    private int accountId;
    private String name;
    private double balance;

    public BankAccount(int accountID, String personName, double initialBalance) {
        accountId = accountID;
        name = personName;
        balance = initialBalance;
    }

    public void deposit() {

    }

    public void withdraw() {

    }

    public int getAccountId() {
        return accountId;
    }

    public String getName() {
        return name;
    }

    public double getBalance() {
        return balance;
    }

    public String toString() {
        return "BankAccount" + name + accountId + balance;
    }
}

BankAccountTester.java

import java.util.ArrayList;
import java.util.Scanner;

public class BankAccountTester {

    public static void main(String[] args) {

        BankAccount b = new BankAccount(int accountID, String name, double initialBalance);
        int n = 0;
        int count;

        ArrayList<String> bankAccounts = new ArrayList<String>(n);

        Scanner in = new Scanner(System.in);
        System.out.print("How many accounts are you going to enter? ");
        n = in.nextInt();

        for (int i = 0; i < n; i++)
        {   

            System.out.print("Enter the account name: ");
            String name = in.next();

            System.out.print("Enter the account number: ");
            int assignment = in.nextInt();

            System.out.print("Enter the initial balance: ");
            double balance = in.nextInt();

            System.out.println(b.toString);
        }



    }
}

how to do the loop to ask the user for the information and put it into the ArrayList.

Which part are you having problems with?
The loop
getting the data from the user
creating an object
putting the object into the arraylist

1)What ArrayList data type are you creating in order to keep BankAccount data? Currently, you are using String which is incorrect. You need another class that can hold BankAccount data.
2)You have a loop up to n tims to collect bank account data. Once you have all information (name, number, and initial balance), you need to use the information to create a BankAccount object. Then append it to your ArrayList using add() method. This will happen while you are still in the loop.

That's all I would tell you what you currently have and what you need to do. Do you understand what ArrayList is? You could read its API here as well.

So I have the arraylist working where it is populating the information. Now I need to create another loop that asks the user for a withdraw and deposit for each account. The program is asking but is storing it into the arraylist and I'm not sure what I need to do to get it to change the balance of the accounts based on the user input. The last thing I need to do is create a third loop to display all of the contents of each bank account using the toString method. I'm not sure how to execute that either. Below is what I have for each sections.

BankAccount.java

public class BankAccount {

    private int accountId;
    private String name;
    private double balance;

    public BankAccount(int accountID, String personName, double initialBalance) {
        accountId = accountID;
        name = personName;
        balance = initialBalance;
    }

    public void deposit(double amount) {

        double newBalance = balance + amount;
        balance = newBalance;

    }

    public void withdraw(double amount) {

        double newBalance = balance + amount;
        balance = newBalance;

    }

    public int getAccountId() {
        return accountId;
    }

    public String getName() {
        return name;
    }

    public double getBalance() {
        return balance;
    }

    public String toString() {
        return "BankAccount" + accountId + name + balance;
    }
}

BankAccountTester.java

import java.util.ArrayList;
import java.util.Scanner;

public class BankAccountTester {

    public static void main(String[] args) {

        int n = 0;
        int count;

        ArrayList<String> bankAccounts = new ArrayList<String>(n);

        Scanner in = new Scanner(System.in);
        System.out.print("How many accounts are you going to enter? ");
        n = in.nextInt();


        for (int i = 0; i < n; i++)
        {   


            System.out.print("Enter the account number: ");
            String accountId = in.next();
            bankAccounts.add(accountId);

            System.out.print("Enter the account name: ");
            String name = in.next();
            bankAccounts.add(name);

            System.out.print("Enter the initial balance: ");
            String balance = in.next();
            bankAccounts.add(balance);


        }

        for (int i = 0; i < n; i++)
        {
            System.out.print("Enter the withdrawal amount:");
            String withdraw = in.next();
            bankAccounts.add(withdraw);

            System.out.print("Enter the deposit amount:");
            String deposit = in.next();
            bankAccounts.add(deposit);      
        }

        for (int i = 0; i < n; i++)
        {
            System.out.println(ArrayList.toString(bankAccounts));
            }      

    }
}

Do you have any specific questions about the features you are having problems with?
Take each feature you want to add, make a list of the steps the program needs to do to accomplish it and write the code for the steps.

I am not sure how to code so that the program adds or substracts from the initial balance if a withdraw or deposit is made. I tried

System.out.println("Enter the withdraw amount:");
Double withdraw = in.nextDouble();
bankAccounts.deposit(withdraw);

But that does not work either. I need help on how to get amounts from the user and apply them to the bank accounts in the arraylist.

But that does not work either

Please explain what "does not work" means? If there are errors, please post the full text of the error message.

Did you make make a list of the steps the program needs to do to make a withdrawal?
Can you post the list here so we can make sure you have all the steps needed BEFORE you try to write any code. Writing code before knowing the steps the program is supposed to do ia waste of time.

Can the deposit() method reduce the balance?
How is the variabe: bankAccounts set to refer to the account that the withdrawal should be done to?

Edited 4 Years Ago by NormR1

I am not sure how to code so that the program adds or substracts from the initial balance if a withdraw or deposit is made. I tried

System.out.println("Enter the withdraw amount:");
Double withdraw = in.nextDouble();
bankAccounts.deposit(withdraw);

But that does not work either. I need help on how to get amounts from the user and apply them to the bank accounts in the arraylist.

As I said earlier, you are not creating an ArrayList to hold BankAccount object but rather String object...

Line 11, you need to change from String to BankAccount type. ArrayList<BankAccount> bankAccounts = new ArrayList<BankAccounts>(); Then, line 24, 28, and 32 must be merged into one line to create a BankAccount object using those 3 string variables you have. bankAccounts.add(new BankAccount(accountId, name, balance));

Next when you want to deal with a bank account, you need to know which element in the ArrayList you are dealing with. Use get(index) method to access the specific element that can call the member method i.e. bankAccounts.get(0).withdraw(withdraw); where 0 is the first element index of the ArrayList.

All in all, you are not familiar with syntax, objects and array list yet.

Edited 4 Years Ago by Taywin

With the above code I get the error message
*The method deposit(String) is undefined for the type ArrayList<String>

Here are the steps that the program needs to do to make a withdraw
1. Receive withdraw amount from user
2. Apply withdraw to balance for account

Earlier I asked:

Which part are you having problems with?
The loop
getting the data from the user
creating an object
putting the object into the arraylist

Where in are you doing the last two steps in the list: create object & add object to the list?

I know that currently a depost can reduce the account balance and I know how to add in the code to create the exception for that I'm just trying to get the portions I am having trouble with finished first.

You need to go back to the steps:

getting the data from the user
creating an object
putting the object into the arraylist

before going on. Get these steps to work before going on.
For testing, input some data into the list and print out the contents of the list.

That was in reference to my first question. I have figured out how to get the loop working to accept the data from the user and create the accountId, name and balance objects and put them in the arraylist. That I do here:

for (int i = 0; i < n; i++)
{
     System.out.print("Enter the account number: ");
    String accountId = in.next();
    bankAccounts.add(accountId);

    System.out.print("Enter the account name: ");
    String name = in.next();
    bankAccounts.add(name);

    System.out.print("Enter the initial balance: ");
    String balance = in.next();
    bankAccounts.add(balance);
}

Or is that not correct? When I display the information it displays in an Arraylist.

What I can't figure out is how to connect the withdraw and deposit amounts to the balance and perform the operation as opposed to adding it as another object in the arraylist.

That is not correct. Where do you create the BankAccount object with the data input by the user?
There were 3 steps in the list I posted, you need to do the second and third.

The arraylist needs to hold BankAccount objects, not Strings.

Edited 4 Years Ago by NormR1

I don't think I understand what you are saying. When I print the arraylist on the screen it gives me back all of the user input isn't it doing what you are telling me?

You need to create BankAccount objects and add them to the arraylist.
If the data for each account is not contained in a class, you will have a lot of problems working with it.

Edited 4 Years Ago by NormR1

Are you saying to create an object that then stores each accountId, name and balance. So if the user wants to enter three accounts they will have three objects and each object will have it's own accountId, name and balance?

Each account would have its own instance of the Bankccount class that contains all the information for that account. If there were 50 accounts, there would be 50 objects.

Ok I understand what you are saying but I'm not sure how to do that. I do it before the for loop to get the information correct? I tried something like this:

ArrayList<String> bankAccounts = new ArrayList<String>(n);

        Scanner in = new Scanner(System.in);
        System.out.print("How many accounts are you going to enter? ");
        n = in.nextInt();
        bankAccounts.add(data);

Are you saying just that in my arraylist I need to do this:

ArrayList<BankAccount> bankAccounts = new ArrayList<BankAccount>(n);

Yes.

Problem with forum: I'm not seeing this after I post it???

Edited 4 Years Ago by NormR1

You have an error in that code. It should not have n inside the parenthesis when you declare an empty ArrayList.

should not have n

That's legal syntax for the ArrayList constructor (as long as n is an int with a valid value).
See the API doc

Edited 4 Years Ago by NormR1

Oh, I'm sorry for that misleading statement. Anyhow, it should not need n in it because the n is 0 and that is a useless argument. Thanks to NormR1 for correcting me.

Ok...I think I finally understand what you are saying...this is what I have

import java.util.ArrayList;
import java.util.Scanner;

public class BankAccountTester {

    public static void main(String[] args) {

        int n = 0;

        ArrayList<BankAccount> bankAccounts = new ArrayList<BankAccount>();

        Scanner in = new Scanner(System.in);
        System.out.print("How many accounts are you going to enter? ");
        n = in.nextInt();

        for (int i = 0; i < n; i++) {

            System.out.println("Enter the account ID: ");
            int accountId = in.nextInt();

            System.out.println("Enter the account name: ");
            String name = in.next();

            System.out.println("Enter the account balance: ");
            double balance = in.nextDouble();

            bankAccounts.add(new BankAccount(accountId, name, balance));

        }

        for (int i = 0; i < n; i++) {
            System.out.print("Enter the withdraw amount:");
            String withdraw = in.next();

            System.out.print("Enter the deposit amount:");
            String deposit = in.next();

        }

    }
}

So not I am creating BankAccount objects and entering in the three items to each object. Now I need to get the withdraw and deposits working but I have no idea where to start.

When will the program make a deposit or withdrawal? Will the user be involved?

What are the steps the program needs to do to make either a deposit or a withdrawal?
Can you make a list of the steps to be done?

Edited 4 Years Ago by NormR1

After the user create the different accounts in the arraylist they will be prompted to enter the deposit and withdraw amount for each user. This is currently how my program is working.

To make a deposit or withdraw the program must:
1. Create the accounts in the arraylist
2. Prompt the user for amounts
3. Send amounts to each bank account in the arraylist and perform the calculations

What I am having trouble with is how to send the withdraw and deposit amounts where they need to be.

How do you determine what account(s) a withdrawal or deposit is to be applied to?
The list of steps you posted skips that part unless you mean that any deposit or withdrawal is to be applied to ALL accounts which is unusual. I would expect a transaction to be for one account not all of them.

This article has been dead for over six months. Start a new discussion instead.