0

所以我正在创建一个使用 ArrayList 的银行账户程序。该程序显示一个菜单,客户可以在其中存款、取款、显示帐户信息和检查余额。数组列表存储客户对象,如果用户存款/取款等,应该能够更新。

这是我到目前为止所拥有的,我不确定如何调用 Customer 类中的存款、取款、显示帐户信息和检查余额的方法,并让它们正确访问数组列表对象。

关于我所缺少的任何帮助或建议都会非常有帮助。

我现在遇到的错误是:找不到符号符号:变量 customerID 位置:显示和平衡方法中所有变量的类 java.util.ArrayList,

找不到符号符号:构造函数 Customer() 位置:当我尝试创建 Customer 实例时,类 taylor.Customer,

当我尝试调用方法并传入数组列表帐户时,无法将 taylor.Customer 中的 display(java.util.ArrayList) 应用于 (taylor.Customer)

测试员类:

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

        }
      }

    }
}

客户类别:

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





}
4

1 回答 1

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.

于 2015-06-22T22:27:56.330 回答