我正在创建非常简单的ATM机。
开始时,系统会询问用户登录名和密码。 如果密码正确,则用户可以进行一些操作。
现在它的操作如下: 1)取钱 2)存钱 3)当前值
问题是,每当我选择1)或2)或3)时,程序会再次询问我有关登录名和密码的信息。但是我想在开始时只被问过一次。
所以我需要解决方案,它允许我保存用户登录名/密码,并在我想要进行某些操作时读取它。
这是我的代码的一部分。
public class LoginVerification {
public static void login() throws Exception {
Scanner input = new Scanner(System.in);
System.out.println("Please enter USER ID:");
String userId = input.nextLine();
System.out.println("Please enter PIN CODE:");
String pass = input.nextLine();
try {
Connection con = ConnectionDB.getConnection();
String sql = "SELECT COUNT(*) FROM Info WHERE ClientID = ? AND ClientPass = ?";
PreparedStatement posted = con.prepareStatement(sql);
posted.setString(1, userId);
posted.setString(2, pass);
ResultSet resultSet = posted.executeQuery();
resultSet.next();
int rowCount = resultSet.getInt(1);
if (rowCount == 1) {
System.out.println("LOGIN SUCCESSFUL");
Helper.showMainMenu();
} else {
System.out.println("#LOGIN UNSUCCESSFUL, WRONG LOGIN OR PASSWORD");
System.out.println("\nTry again? \n1) yes \n2) no [quit]");
int choice = input.nextInt();
if (choice == 1) {
login();
} else {
System.exit(0);
}
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
问题如下。你可以看到该程序再次询问登录/通过,因为我不知道如何以不同的方式做到这一点。
public static void withdraw() throws Exception {
Scanner input = new Scanner(System.in);
System.out.println("please enter USER ID:");
String userId = input.nextLine();
System.out.println("please enter PIN CODE:");
String pass = input.nextLine();
System.out.println("how much you want to withdraw");
int money = input.nextInt();
try {
Connection con = ConnectionDB.getConnection();
String sql = "UPDATE BankDB.Info SET `Money`= `Money` - ? WHERE `ClientID`= ? AND `ClientPass` = ?";
PreparedStatement posted = con.prepareStatement(sql);
posted.setInt(1, money);
posted.setString(2, userId);
posted.setString(3, pass);
if (posted.executeUpdate() > 0) {
System.out.println("OPERATION SUCCESSFUL");
} else {
System.out.println("LOGIN UNSUCCESSFUL");
System.out.println("\nTry again? \n1) yes \n2) no [quit]");
int choice = input.nextInt();
if (choice == 1) {
withdraw();
} else {
System.exit(0);
}
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
你需要一个标志来告诉用户是否登录,
这可以在login()
中设置,可以从任何页面检查/使用
修改login()
,如下所示:
1-在LoginVerification
class
private static boolean loggedIn = false;
login()
中的第一行,检查该标志
public static void login() throws Exception {
if(loggedIn){
//do nothing, user already logged in
return;
}
//...rest of code ...
当您发现用户登录是否正确时添加此内容:
if (rowCount == 1) {
//add this line
loggedIn = true;
} else {
//add this line
loggedIn = false;
}
现在在其他需要用户登录的地方,请使用
LoginVerification.login();
ex,替换此
public static void withdraw() throws Exception {
Scanner input = new Scanner(System.in);
System.out.println("please enter USER ID:");
String userId = input.nextLine();
System.out.println("please enter PIN CODE:");
String pass = input.nextLine();
System.out.println("how much you want to withdraw");// rest of code
用这个:
public static void withdraw() throws Exception {
LoginVerification.login();
System.out.println("how much you want to withdraw"); // rest of code
如果用户尚未登录,将要求用户登录。