我的老師向我們展示瞭如何使用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);
}
}
}
}
}
我更新的代碼
我希望我給了足夠的信息,任何幫助將不勝感激 – matt 2014-09-26 02:39:03
銀行帳戶數組列表,然後呢?我們不需要你的老師教什麼? – codebot 2014-09-26 03:47:44
你將如何檢查用戶輸入?我猜想switch語句將會是更好的選擇,如果將來也會如此。不是嗎? – Krishna 2014-09-26 03:54:02