Java ArrayList银行帐户

时间:2015-06-22 21:44:00

标签: java class arraylist account bank

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

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

对于我所遗漏的内容,任何帮助或建议都会非常有帮助。

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

无法找到符号 符号:构造函数Customer() location:class taylor.Customer当我尝试创建一个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);
  }





}

1 个答案:

答案 0 :(得分:0)

问题是您永远不会初始化BankAccount中的任何字段。从BankAccount构造函数中删除void以使其成为构造函数,并在TestBank中构建类时使用该构造函数。

由于这些字段与Customer中的字段重复,因此最好执行customer.deposit(amount)等操作。删除BankAccount类并将方法移至Customer并添加参数{{1使用金额的方法。然后在您的TestBank类中更改存款等方法调用以获得金额参数。

此外,您可以摆脱Customer中的getter和setter方法。