2014-09-26 230 views
-3

我的老師向我們展示瞭如何使用ArrayList來存儲信息,但我遇到了問題。我不明白的是如何引用我已經存儲在數組列表中的「賬戶」,然後對其應用一個方法。例如,我試圖在存儲在ArrayList中的帳戶上使用「存款」方法。銀行帳戶ArrayList

import java.util.Scanner; 
import java.util.ArrayList; 
public class Engine 
{ 
public static void Engine() 
{ 
    ArrayList<BankAccount> accounts = new ArrayList<BankAccount>(); 
    Scanner Reader = new Scanner(System.in); 
    BankAccount n = new BankAccount(); 
    String response = Reader.nextLine(); 
    boolean keepGoing = true; 
    while(keepGoing) 
    { 
     System.out.println("Welcome to The Bank of Money, what would you like to do?\n enter code hereenter n to create a new account, enter e to use an existing account, or  enter q to quit to main menu"); 

     response = Reader.nextLine(); 
     if(response.equals("q")) keepGoing = false; 
     if(response.equals("n")) accounts.add(new BankAccount()); 
     if(response.equals("e"))System.out.println("what is your account number?"); 
     String accountNum = Reader.nextLine(); 

    } 
    System.out.println("press 1 to deposit money"); 
    System.out.println("press 2 to withdraw money"); 
    System.out.println("press 3 to check your account balance"); 
    System.out.println("press 4 to check your account's interest"); 
    System.out.println("press 5 to quit"); 
    String response2 = Reader.nextLine(); 
    if (Reader.nextLine().equals("1")) 
    { 
     for(int i = 0; i<accounts.size();i++) 
     { 
      if (accounts.get(i).equals(accountNum)) 
      { 
       accounts.get(i).deposit(amount); 

      } 
     } 
    } 
} 
} 

我更新的代碼

+0

我希望我給了足夠的信息,任何幫助將不勝感激 – matt 2014-09-26 02:39:03

+0

銀行帳戶數組列表,然後呢?我們不需要你的老師教什麼? – codebot 2014-09-26 03:47:44

+0

你將如何檢查用戶輸入?我猜想switch語句將會是更好的選擇,如果將來也會如此。不是嗎? – Krishna 2014-09-26 03:54:02

回答

1

「我如何說 「如果用戶輸入1,使用存款的方法?」

if (Reader.nextLine().equals("1") 
{ 
    // Call deposit method on correct arraylist item. 
} 

現在,我不知道您的程序的上下文基於您所提供的代碼,但它看起來像是給每個BankAccount實例的銀行帳號..? 這是一個有用的標識符,因爲它們很可能都是聯合國神遊。 一旦你收到正確的輸入,你可以簡單地通過你的所有銀行賬戶ArrayList中,直到您循環查找具有正確的賬號,並呼籲其存款:

if (Reader.nextLine().equals("1") 
{ 
    for (int i = 0; i<accounts.size(); i++ 
    { 
     if (accounts.get(i).getAccountNum().equals(accountNum)) // Use a getter here 
     { 
      accounts.get(i).deposit(amount to deposit); 
     } 
    } 
} 

..或沿東西那些行...

+0

謝謝這真的很有用,只是另一個後續問題,我用過你上面寫的代碼,我認爲我明白了。我用「double amount」替換了「amount to deposit」,但是我得到了一個名爲'.class'的語法錯誤。我不知道如何解決這個問題,我相信答案是相當基礎的,我只是想了解基本面,任何幫助都會得到進一步的讚賞。爲什麼需要類名,我該如何解決? – matt 2014-09-28 02:46:51

+0

您正在調用對象的方法,因此您不需要將該類型傳遞給方法..只有變量。因此,一旦您將amount變量設置爲某個值,請將該變量傳遞給該方法。換句話說,存款(金額)... – robzillaDev 2014-09-28 03:21:35

+0

好的 - 在這一行中,在銀行帳戶類公共無效存款(雙金額)我定義的變量「金額」作爲類型雙參數? – matt 2014-09-28 03:46:45

0

你怎麼知道從ArrayList中獲取哪個BankAccount?有多個BankAccount,這就是爲什麼你要將它們存儲在ArrayList中?你想在ArrayList中的所有帳戶上運行存款方法?需要更清晰。 仍然我會建議(給出這種情況下)使用開關的情況下,其中案例是像情況「1」:/ /調用存款方法。等等等等。 看看這是否有幫助。讓我知道你是否需要進一步的幫助。

0

//我怎麼說,「如果用戶輸入1,使用的存款方式?

代碼一樣的,你需要輸入要添加新帳戶或使用exist.then你可以得到什麼樣的行動用戶要創建一個帳戶後執行。

如果是現有的帳戶考慮數字作爲輸入,並從ArrayList中

找到它,如果您使用此代碼

for(int i = 0; i<accounts.size();i++) 
{ 
accounts.get(i).deposit(double amount); 
} 
你不能得到帳戶

並創建一個明確的賬戶號碼爲