2016-02-08 8 views
0

主類:如何在輸入驗證後顯示菜單?

package BankingSystem; 


import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner; 

public class Bank { 

    public static void main (String [] args){ 

     //User verifies Account 
     SignIn Valid = new SignIn(); 
     Valid.AccountLogin(); 
     Scanner choice = new Scanner (System.in); //allow user to choose menu option 

     int option = 0; //Menu option is set to 0 


        // Menu For User 


     do{ 
      System.out.println("Welcome"); 
      System.out.println(); 
      System.out.println("1:Deposit Cash"); 
      System.out.println("2: Withdraw Cash"); 
      System.out.println("3: View Current Account Balance"); 
      System.out.println("4: View Saving Account Balance"); 


      System.out.println("5: Cancel"); //When the User Quits the system prints out GoodBye 

      System.out.println("Please choose"); 
      option= choice.nextInt(); 



    }while (option < 6); 
} 


    } 

AccountLoginClass:

package BankingSystem; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner; 

public class SignIn { 
    public void AccountLogin(){ 
      List<String> AccountList = new ArrayList<String>(); 
      AccountList.add("45678690"); 

      Scanner AccountInput = new Scanner(System.in); 
     System.out.println("What is your account number?"); 
      AccountInput.next(); 
      boolean isExist = false; 

      for (String item : AccountList){ 
       if (AccountInput.equals(AccountList.get(0))){ 
        System.out.println("Hi"); 
        isExist = true; 
        break; 
       } 

      } 

      if (isExist){ 
       //Found In the ArrayList 
      } 
      } 



     } 

我想創建一個相當複雜的銀行系統。在這裏,我希望用戶輸入他們的賬號,希望與陣列列表匹配,並且當它與ArrayList中的號碼匹配時,它們與菜單一起顯示,例如退出,存款等。
這裏的問題是我我不知道我怎麼能做到這一點,我創建一個對象之前,它與AccountLoginClass鏈接它的菜單,但這並不真正的工作。

+0

你覺得呢'AccountInput.next();'呢?文檔的哪一部分讓你這麼想? – Pshemo

+0

我在類似的答案中看到了它,所以我決定只是把它放進去。 – Ahmed

+1

但是你認爲它有什麼作用(我在問,因爲它可能不正確)? – Pshemo

回答

1

你在這裏和你的其他問題有同樣的錯誤。

AccountInput.equals(AccountList.get(0)) 

您正在將java.lang.String類的實例與類java.util.Scanner的實例進行比較。您的意思是:

(AccountInput.nextLine()).equals(AccountList.get(0)) 

這工作,你將能夠匹配帳戶數的ArrayList 元件(未列表本身,爲u寫)

Java中的所有類的類派生Object(java.lang.Object)。所以有時你可能不會收到例外,因爲它們有不同的簽名。有時即使他們在常規意義上不相同,他們也可能是平等的。在好的時候,你會收到一個異常,會導致你的程序崩潰。

通常你需要確保你是不是比較蘋果橙子

它很容易檢查:只要看看它是什麼的聲明你是比較

Orange or1, or2; 
Apple ap1; 
... 
or1.equals(ap1)  // BAD 
or1.equals(or2)  // Good if equals() implemented for class Orange in 
        // in the way it satisfies you. 
相關問題