Please answer this question ( only ) with a method that is related to these topi
ID: 3591139 • Letter: P
Question
Please answer this question ( only ) with a method that is related to these topics :1- intro to java 2- branches 3- loops 4- methods
Pc : please do not solve by a methods of high level or doesn’t include the previous topics.
Question 2 (50 points): User Authentication Create a text file UserPassword.txt with the following two lines: Alice P4ssword Write a program to complete the following tasks: . Upon execution, the program will prompt the user to type in 2. The program will compare with the typed in name with the 3. If the name does not match, the program will prompt user to his/her name. name stored in UserPassword.txt (the first line of the file). reenter the name; if the name match, the program will proceed to ask for the password. 4. Give the user 3 times to enter password, and compare the typed in value with the password stored in UserPassword.txt (the second line of the file). If the password is correct, display a greeting message and terminate the program; otherwise, display an error message and terminate the program. Following is an sample screen of the program in execution: Welcome! Please enter User Name: Bob User Name does not exist! Welcome! Please enter User Name: Alice Welcome back, Alice! Please enter User Password: 123 Password for Alice entered incorrectly! Please enter User Password: Congratulations! You have successfully logged in. (Or: You have entered incorrect password for 3 times, system is shutting down...) Your file will have the following documentation comments before the class header:
Explanation / Answer
/******************************************UserAuthentication.java******************************************/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
/**
* The Class UserAuthentication.
*
*/
public class UserAuthentication {
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try {
int unsuccessfullAttempt = 0;
BufferedReader br = new BufferedReader(new FileReader("UserPassword.txt"));
String username = br.readLine();
String password = br.readLine();
String name = "";
// while loop for user name
// continue until unless correct user name
while (true) {
System.out.println("Welcome! Please Enter User Name:");
name = input.nextLine();
if (!username.equals(name)) {
System.out.println("User Name does not exist!");
} else {
System.out.println("Welcome back, " + name);
break;
}
}
// while loop for password
// it will break if 3 time unsuccessful attempt
while (true) {
System.out.println("Please enter User Password: ");
String upassword = input.nextLine();
if (!password.equals(upassword)) {
System.out.println("Password for " + name + " entered incorrectly!");
unsuccessfullAttempt++;
} else {
System.out.println("Congratulations! you have successfully logged in.");
break;
}
if (unsuccessfullAttempt == 3) {
System.out.println("You have entered incorrect password for 3 times, system is shutting down...");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
input.close();
}
}
}
/***************************************output***************************************************/
Welcome! Please Enter User Name:
Bob
User Name does not exist!
Welcome! Please Enter User Name:
Alice
Welcome back, Alice
Please enter User Password:
123
Password for Alice entered incorrectly!
Please enter User Password:
P4ssword
Congratulations! you have successfully logged in.
/************************************UserPassword.txt*********************************************/
Alice
P4ssword