我的老师向我们展示了如何使用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