I need help with this authentication system assignment. The program needs to req
ID: 3826162 • Letter: I
Question
I need help with this authentication system assignment. The program needs to request a user nameand verify a password. the user must be given 3 attempts to enter a passwod prior to closing the session. the prorgam wil continue to ask for user name and password until the user enters q to close the program.
Below is my code
import java.util.Scanner;
public class AuthenticationSystem {
public static void main(String[] args) {
String userNames = null;
String userPassword = null;
// TODO code application logic here
boolean quit = false;
Scanner scnr = new Scanner(System.in);
System.out.println("Welcome user! Press q to quit at anytime.");
do {
System.out.println("Please enter Your Username: ");
userNames = scnr.nextLine();
if(userNames != null){
if(userNames.length() == 1 && userNames.equalsIgnoreCase("q")) {
quit = true;
} else if (userNames.contains("amy.lee") || userNames.contains("rose.moore")||userNames.contains("chris.kelly") {
System.out.println("Welcome User!");
System.out.println("Please enter your password");
userPassword = scnr.nextLine();
if(userPassword != null){
int passwordAttempts = 0;
while(userPassword !="password1" || userPassword!="password2" || userPassword != "password3" && passwordAttempts < 3){
if (userPassword !="password"){
System.out.println("Please try again.");
userPassword = scnr.nextLine();
}else {
System.out.println("Too many password attempts. Session Terminated");
}
} while (!quit);
}
}
Explanation / Answer
The problem in the above code was mostly with the password part, where, only three attempts are allowed. Here is a small modification to the above code.
import java.util.Scanner;
public class AuthenticationSystem {
public static void main(String[] args) {
String userNames = null;
String userPassword = null;
// TODO code application logic here
boolean quit = false;
Scanner scnr = new Scanner(System.in);
System.out.println("Welcome user! Press q to quit at anytime.");
do {
System.out.println("Please enter Your Username: ");
userNames = scnr.nextLine();
if(userNames != null){
if(userNames.length() == 1 && userNames.equalsIgnoreCase("q")) {
quit = true;
} else if (userNames.contains("amy.lee") || userNames.contains("rose.moore")||userNames.contains("chris.kelly") {
System.out.println("Welcome User!");
System.out.println("Please enter your password");
userPassword = scnr.nextLine();
if(userPassword.equals("password1"))
accepted();
for(int i=0;i<3;i++) {
System.out.println("Please try again");
userPassword = scnr.nextLine();
if (userPassword.equals("yourpassword")){
accepted();
break;
}
}
System.out.println("Too many password attempts. Session Terminated");
quit = true;
}
}
} while (!quit);
}
}
A for loop (or while loop - whichever preferred ) with a variable " i " denoting number of attempts would go on till 3. If the password matches then a method accepted() would be called that may contain some welcome message or a new window as per your choice. If all the three attempts are wasted then the loop will eventually terminate stating "Too many password attempts ". Keeping rest of the assumptions intact, the code would work. But it would also be better to use methods for a different peice of code.