Question: Hello everyone, I am trying to write a pseudocode for school to reflect my work. I have done a fair amount but I need to
Hello everyone,
I am trying to write a pseudocode for school to reflect my work. I have done a fair amount but I need to break down into two classes. I don't know what that is supposed to look like. If anyone can take a look at it I appreciate. The instruction in the class is that the pseudocode should match the code.
My pseudocode:
Create a security program to log in and out of the Zoo administration computer. Each user should have its own ID and password and the program should allow for three tries before booting the user out of the system. After the user log in the program should display a message accordingly to the function of the user.
IMPORT java.io.File
IMPORT java.security.MessageDigest
IMPORT java.util.Scanner
DECLARE public class authentication
INNITIALIZE scanner for impute reading
DECLARE integer to track user attempts
PRINT Enter user name
DECLARE the variable for user name and INITIALIZE variable.
DECLARE the variable for user password and INITIALIZE variable.
RUN through MessageDigest for password encoding
PRINT hash password
READ the username
IF username was entered, THEN continue to next step
ELSE, exit program
READ the users password
IF password is entered, THEN continue to next step
ELSE, exit program
OPEN credentials file.
IF file exists, THEN move to next step
ELSE, throw exception error and stop program
VERIFY that the user is authorized by checking the credentials file that a valid username and password is entered.
IF authentication is not successful, THEN
READ the username and password for a second time
VERIFY the user is authorized by checking the credentials file that a valid username and password was entered.
IF authentication is not successful, THEN
READ the username and password for a third time
VERIFY the user is authorized by checking the credentials file that a valid username and password was entered.
IF authentication is not successful, THEN IF authentication is successful, THEN DISPLAY a message and exit program.
ELSE,
DISPLAY the role information.
ALLOW the user to logout after reviewing the information
My code:
import java.io.File; import java.security.MessageDigest; import java.util.Scanner;
public class Authentication {
public static void main(String[] args) throws Exception {
//Create a scanner object to read input from the console Scanner readInput = new Scanner(System.in);
//Declare a variable to keep track the number of attempts int attempts = 0;
/*Repeat until a successful attempt has been made or three unsuccessful attempts have been made or a user chooses to exit*/ while (true) {
//Ask the user for a username System.out.print("Enter user name: ");
String uName = readInput.nextLine();
//Ask the user for a password System.out.print("Enter password: ");
String original = readInput.nextLine();
//Convert the password using a message digest five (MD5) hash MessageDigest md = MessageDigest.getInstance("MD5");
md.update(original.getBytes());
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
System.out.println("hash pass :" + sb.toString()); //Declare a boolean variable to keep track whether //a login is successful or not boolean authenticationSuccess = false;
//Open credentials file Scanner readCred; readCred = new Scanner(new File("credentials.txt"));
//Search for the user credentials in the credentials file while (readCred.hasNextLine()) {
String record = readCred.nextLine();//Read a record
String columns[] = record.split(" ");//Split the record into individual fields
/*Check the credentials against the valid credentials provided in the credentials file*/ //Check user name. if (columns[0].trim().equals(uName)) { System.out.println(columns[1].trim()); System.out.println(sb.toString()); /*If user name is matched, check whether the Converted password using a message digest five password with hashed password in the second column*/ if (columns[1].trim().equals(sb.toString()))//Check password { System.out.println("pass checked"); /*If the passwords are same, set the boolean value uthenticationSuccess to true*/ authenticationSuccess = true;//Login success
//Open the role file Scanner readRole; readRole = new Scanner(new File(columns[2].trim() + ".txt")); System.out.println("Role Reading"); //Display the information stored in the role file while (readRole.hasNextLine()) {
System.out.println(readRole.nextLine());
}
break;
}
}
}
//If login successful, ask the user whether the user wants to log out or not if (authenticationSuccess) {
System.out.println("Do you want to log out(y/n): ");
String choice = readInput.nextLine();
//If user wants to log out, exit the system. if (choice.toLowerCase().charAt(0) == 'y') {
System.out.println("Successfully loged out.");
break;
} //If user wants to continue, set the boolean value //uthenticationSuccess to true for new login else {
authenticationSuccess = false;
}
} //If login is not successful, update the number of attempts else {
attempts++;
//If maximum attempts reached, notify the user and exit the program if (attempts == 3) {
System.out.println("Maximum attempts reached! Exiting...");
break;
} //otherwise, prompt to enter credentials again else {
System.out.println("Please enter correct credentials!");
}
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
