【问题标题】:Java ArrayList Bank AccountJava ArrayList 银行账户
【发布时间】:2015-09-08 12:27:54
【问题描述】:

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

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

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

我现在遇到的错误是:找不到符号 符号:可变客户ID 位置:显示和平衡方法中所有变量的类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);
  }





}

【问题讨论】:

  • 代码太多了……到底是什么问题?
  • 在 Java 中,操作/方法是使用点 (.) 运算符在实例上调用的。因此,在您的情况下,只需调用 BankAccount instanceName.methodName(如果需要,传递任何参数)
  • 如果您在查找所需的BankAccount 实例时遇到问题,请考虑使用Map 而不是ArrayList,并将其映射到BankAccount使用customerID。 (尽管这可以通过使每个customerID 顺序并简单地使用它们索引到您的ArrayList 来轻松实现。)
  • 另外,你的构造函数不应该是public void BankAccount(...它应该是public BankAccount(...

标签: java class arraylist account bank


【解决方案1】:

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

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

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

【讨论】:

  • 如果没有 getter 和 setter 方法,我将如何显示保存在我的数组列表中的余额和信息?由于某种原因,我根本无法让它工作,任何其他帮助都会非常棒!
  • 您可以简单地执行customer.field,其中field 是您希望查看该Customer 实例的任何字段。将其分配给某物以模仿 setter,或者简单地按原样使用以模仿 getter。
  • 感谢您的帮助河。我刚刚编辑了帖子以更新两个文件 - 我仍然有错误调用方法并试图通过数组列表..我不确定为什么我遇到这么多麻烦任何其他帮助都会非常感激
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-10
  • 2014-10-09
  • 2014-08-27
  • 1970-01-01
相关资源
最近更新 更多