Java ArrayList银行账户

问题描述:

所以我创建了一个使用ArrayList的银行账户程序。该程序显示一个菜单,客户可以存入,提取,显示帐户信息并查看余额。数组列表存储客户对象,并且应该能够在用户存款/取款等情况下更新。Java ArrayList银行账户

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

任何关于我失去的帮助或建议都会非常有帮助。

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

找不到符号 符号:构造客户() 位置:类taylor.Customer当我尝试创建一个客户实例,

显示器(java.util.ArrayList中)在taylor.Customer不能适用于(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); 
    } 





} 
+3

这是很多代码......究竟是什么问题? – Mureinik

+3

在使用点(。)运算符实例调用Java操作/方法时。所以在你的情况下,只需调用BankAccount instanceName.methodName(如果需要,传递任何参数) –

+0

如果您在查找* BankAccount实例时遇到问题,请考虑使用Map而不是ArrayList。使用'customerID'映射到'BankAccount'。 (虽然这可以通过使每个'customerID'顺序并简单地使用它们索引到你的ArrayList中来实现)。 – River

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

由于这些字段是在客户的那些的副本,似乎这将是最好做这样的事情customer.deposit(amount)等删除您BankAccount类,移动你的方法,以客户和参数int amount添加到使用的方法量。然后在您的TestBank类中更改存款等方法调用以获得金额参数。

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

+0

我怎么去显示保存在我的数组列表中没有getter和setter方法的平衡和信息?我无法完全解决这个问题,任何其他的帮助都会非常棒! –

+0

你可以简单地在'customer.field'中选择'field'是你希望为那个'Customer'实例查看的领域,将它分配给模拟一个setter的东西,或者直接用它来模仿一个getter。 – River

+0

thanks for你的帮助River。我只是编辑了这篇文章,以获得更新的两个文件 - 我仍然有错误调用方法,并试图通过数组列表..我不知道为什么我有太多的麻烦,任何其他帮助将非常感激 –