主类:
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
}
}
}
我正在努力创建一个相当复杂的银行系统。在这里,我打算让用户输入他们的帐号,希望与Array List匹配,当它与ArrayList中的号码相匹配时,它们会显示菜单,例如取款,存款等。
这里的问题是我不知道如何在将菜单与AccountLoginClass链接之前创建一个对象,但这并没有真正起作用。
答案 0 :(得分:1)
您在此处遇到的问题与其他问题相同。
AccountInput.equals(AccountList.get(0))
您正在将类java.lang.String的实例与类java.util.Scanner的实例进行比较。你的意思是:
(AccountInput.nextLine()).equals(AccountList.get(0))
这将有效,您将能够将帐号与元素 ArrayList匹配(不是列表本身,如您所写)
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.