2015-06-22 128 views
-1

所以我創建了一個使用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

回答

0

的問題是,你永遠不會初始化任何的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。我只是編輯了這篇文章,以獲得更新的兩個文件 - 我仍然有錯誤調用方法,並試圖通過數組列表..我不知道爲什麼我有太多的麻煩,任何其他幫助將非常感激 –