0

So I am creating a bank account program that uses an ArrayList. The program displays a menu where a customer can deposit, withdraw, display account info and check balance. The array list stores the customer object and should be able to be updated if user deposits/withdraws etc.

Here is what I have so far, I am unsure how to call the methods for deposit, withdraw, display account info, and check balance that are in the Customer class and have them access the array list objects correctly.

Any help or advice at all as to what I am missing would be so helpful.

the errors I am getting right now are: cannot find symbol symbol : variable customerID location: class java.util.ArrayList for all of the variables in the display and balance method,

cannot find symbol symbol : constructor Customer() location: class taylor.Customer when I try to create a Customer instance,

display(java.util.ArrayList) in taylor.Customer cannot be applied to (taylor.Customer), when I try to call the methods and pass in the array list accounts

tester class:

package taylor; 
import java.util.ArrayList;
import java.util.Scanner;

public class TestBank{

  public static void main(String args[]){


    ArrayList<Customer> accounts = new ArrayList<Customer>();

    Customer customer1 = new Customer(1, "Savings", "US Dollars", 800);
    Customer customer2 = new Customer(2, "Checking", "Euro", 1900);
    Customer customer3 = new Customer(3, "Checking", "US Dollars", 8000);

    accounts.add(customer1);
    accounts.add(customer2);
    accounts.add(customer3);


    int customerID=4;
    String choice;
    int deposit;
    int withdraw;
    Scanner in = new Scanner(System.in);
    Customer operation = new Customer();
    boolean flag = true;

    String accountType;
    String currencyType;
    int balance;

    while(flag){
      System.out.println("Select a choice:");
      System.out.println("1. Existing customer");
      System.out.println("2. New customer");   
      System.out.println("3. Quit");


      String input = in.next();

        //existing user
        if(input.equals("1")){

          System.out.println("Enter CustomerID: ");
          customerID = in.nextInt();


          System.out.println("Would you like to: ");
          System.out.println("1. Deposit ");
          System.out.println("2. Withdraw ");
          System.out.println("3. Display account info ");
          System.out.println("4. Check balance ");

          choice = in.next();

          if(choice.equals("1")){
            System.out.println("How much would you like to deposit?");
            deposit = in.nextInt();
            operation.deposit(deposit);
          }

          else if (choice.equals("2")){
           System.out.println("How much would you like to withdraw?");
            withdraw = in.nextInt(); 
            operation.withdraw(withdraw);

          }

          else if (choice.equals("3")){
            operation.display(accounts.get(customerID));
          }

          else if (choice.equals("4"))
            operation.balance(accounts.get(customerID));

          else
            System.out.println("Invalid");
        }



        //new user
         else if(input.equals("2")){
          //add new user to arraylist

           int newID = customerID + 1;

           System.out.println("Enter account type: ");
           accountType = in.next(); 
           System.out.println("Enter currency type: "); 
           currencyType = in.next();
           System.out.println("Enter initial balance: ");
           balance = in.nextInt(); 

           System.out.println("Your customer ID will be: " + newID);


           accounts.add(new Customer(newID, accountType, currencyType, balance));


        }

        else if(input.equals("3")){

          System.out.println("Thanks for using this bank!");
          flag = false;
        }


        else{

          System.out.println("Invalid");

        }
      }

    }
}

customer class:

package taylor; 
import java.util.Scanner; 
import java.util.ArrayList;

public class Customer{

  String accountType, currencyType, info; 
  int customerID, balance, amount;
  Scanner input = new Scanner(System.in);


  public Customer(int customerID, String accountType, String currencyType,  int balance){
    this.accountType = accountType;
    this.currencyType = currencyType; 
    this.customerID = customerID;
    this.balance = balance; 
    this.amount = amount; 
  }

  public int deposit(int amount){

    amount = input.nextInt();
    if (amount >= 500) {
      System.out.println("Invalid");

    }
    else{
      balance = balance + amount;

    }
    return balance;
  }

  public int withdraw(int amount){

    if (balance < amount) {
      System.out.println("Invalid amount.");
    }
    else if (amount >= 500) {
      System.out.println("Invalid");
    }
    else {
      balance = balance - amount;

    }
    return balance;
  }


  public void display(ArrayList<Customer> accounts) {
    System.out.println("CustomerID:" + accounts.customerID);
    System.out.println("Account Type:" + accounts.accountType);
    System.out.println("Currency Type: " + accounts.currencyType); 
    System.out.println("Balance:" + accounts.balance);

  }

  public  void balance(ArrayList<Customer> accounts) {
    System.out.println("Balance:" + accounts.balance);
  }





}
Jess Anastasio
  • 687
  • 5
  • 18
  • 26
  • 3
    That's a lot of code... What exactly is the problem? – Mureinik Jun 22 '15 at 21:45
  • 3
    In Java operations/methods are called on instance using the dot(.) operator. So in your case simply call BankAccount instanceName.methodName(pass any param if required) – Juned Ahsan Jun 22 '15 at 21:48
  • If you're having trouble *finding* the `BankAccount` instance you want, consider using a `Map` instead of an `ArrayList` and have it map to `BankAccount` using `customerID`. (Although this could be trivially achieved by making each `customerID` sequential and simply indexing into your `ArrayList` using them.) – River Jun 22 '15 at 21:50
  • Also, your constructor should not be `public void BankAccount(...` it should be `public BankAccount(...`. – Ricky Mutschlechner Jun 22 '15 at 22:41

2 Answers2

0

The problem is that you never initialize any of the fields in BankAccount. Remove the void from the BankAccount constructor to make it a constructor and use that constructor when you build the class in TestBank.

Since these fields are duplicates of the ones in Customer, it seems that it would be best to do something like customer.deposit(amount), etc. Remove your BankAccount class and move your methods to Customer and add the parameter int amount to the methods that use amount. Then in your TestBank class change the deposit, etc. method calls to have an amount parameter.

Also, you can get rid of the getter and setter methods in Customer.

jpyams
  • 4,030
  • 9
  • 41
  • 66
  • how would I go about displaying the balance and info thats saved in my array list without getter and setter methods? I can't get this to work at all for some reason, any other help would be so great! – Jess Anastasio Jun 23 '15 at 14:42
  • You can simply do `customer.field` where `field` is whichever field you wish to view for that `Customer` instance. Assign it to something to mimic a setter, or simply use as is to mimic a getter. – River Jun 23 '15 at 14:58
  • thanks for your help River. I just edited the post to have the updated two files - I am still having errors calling methods and trying to pass through the array list.. I'm not sure why I'm having so much trouble any other help would be so appreciated – Jess Anastasio Jun 23 '15 at 15:35
0
/*in this program you can deposit, withdraw, and also show the statement of what you did with the help of ArrayList */
 

import java.util.*;

class ExcerciseCollection1 {

    static float totalBalance;
    static float availableBalance;

    void deposit(float amount) {
        totalBalance += amount;
        
    }

    void withdraw(float money) throws Exception {
        if (totalBalance < money) {
            System.out.println("insufficient balance");
        } else {
            totalBalance = totalBalance - money;
            availableBalance = totalBalance;
        }
    }

    void showOption() {
        System.out.println("Operations");
        System.out.println("1)Deposit\n" + "2)Withdraw\n" + "3)View Statement\n" + "4)exit");
        System.out.println("Choose your option=");

    }

    public static void main(String arg[]) {

        try {
            ExcerciseCollection1 b = new ExcerciseCollection1();
            Scanner sc = new Scanner(System.in);

            List<String> statement = new ArrayList<>();

            int option;
            do {

                b.showOption();
                option = sc.nextInt();
                switch (option) {
                case 1:
                    System.out.println("Enter Your Amount For Deposite : ");
                    float deposite = sc.nextFloat();
                    
                    b.deposit(deposite);
                    statement.add("Deposite=" + deposite);
                    break;
                case 2:
                    System.out.println("Enter Your Amount for withdraw : ");
                    float withdraw = sc.nextFloat();
                    
                    System.out.println("Withdrawing amount : " + withdraw);
                    b.withdraw(withdraw);
                    statement.add("Withdraw=" + withdraw);
                    break;

                case 3:
                    for (String s : statement) {
                        System.out.println(s);
                    }
                    System.out.println("Available Balance=" + availableBalance);
                    break;
                case 4:
                    System.out.println("Thank you");
                    System.exit(0);
                    break;
                }

            } while (option != 4);


        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }
}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 02 '23 at 15:24