Java ArrayList Bank Account

20.9k views Asked by At

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





}
2

There are 2 answers

3
jpyams On

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.

1
Dhruv Shantilal Patel On
/*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());
        }

    }
}