Java ArrayList 银行账户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30990366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:56:05  来源:igfitidea点击:

Java ArrayList Bank Account

javaclassarraylistaccountbank

提问by Jess Anastasio

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.

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

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.

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

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,

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

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

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

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

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

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





}

回答by jpyams

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

问题是您从未初始化 BankAccount 中的任何字段。void从 BankAccount 构造函数中删除以使其成为构造函数,并在您在 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 amountto the methods that use amount. Then in your TestBank class change the deposit, etc. method calls to have an amount parameter.

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

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

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