2014-09-26 253 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); 
} 
你不能得到帐户

并创建一个明确的账户号码为