First, my code as it is completely works. But I need to alter my code because I
ID: 3906129 • Letter: F
Question
First, my code as it is completely works. But I need to alter my code because I was to have AT LEAST TWO CLASSES and methods in my final program. My question is HOW DO I ALTER MY CODE TO HAVE TWO CLASSES AND ADDITIONAL METHODS WITHOUT COMPLETELY RE-WRITING MY PROGRAM?
package newzooauthentication;
import java.util.Scanner;
import java.security.MessageDigest;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
public class NewZooAuthentication {
/**
*
* @param args the command line arguments
* @throws java.io.IOException
* @throws java.security.NoSuchAlgorithmException
*
**/
public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
//<---->Scanner object to read input from the console
Scanner readUserInput = new Scanner(System.in);
//<---->Variable to keep track of the number of log-in attempts
int loginAttempts = 0;
//<---->While Loop until break encountered
//<---->(user exits system or 3 unsuccessful log-in attempts are made)
while (true) {
//<---->Request user input username
System.out.print("Enter username: ");
//<---->Set String variable equal to user input
String userName = readUserInput.nextLine();
//<---->Request user input password
System.out.print("Enter password: ");
//<---->Set String variable equal to user input
String userPwd = readUserInput.nextLine();
//<---->Copied and pasted code section to convert the
//<---->password with MessageDigest-5 (MD5) hash
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(userPwd.getBytes());
byte[] digest = md.digest();
StringBuilder sb = new StringBuilder();
for (byte b : digest)
{
sb.append(String.format("%02x", b & 0xff));
}
//<----->End copied and pasted code section
//<---->Boolean variable declared to verify authentication
boolean authenticated = false;
//<---->Scanner object to read credentials.txt file
Scanner readCFile = new Scanner(new File("Credentials.txt"));
//<---->While Loop to read user information in the
//<---->credentials.txt file until End Of File (EOF)
while (readCFile.hasNextLine()) {
//<-------->Parse the contents of credentials.txt file
String credInfo = readCFile.nextLine();
String parsedInfo [] = credInfo.split(" ");
String nameValue = parsedInfo[0].trim();
String hashValue = parsedInfo[1].trim();
String roleValue = parsedInfo[3].trim();
//<-------->Comparing parsed nameValue to username input
if (nameValue.equalsIgnoreCase(userName)) {
//<------------>Comparing parsed hashValue to password input
if (hashValue.equals(sb.toString())) {
//<---------------->If nameValue and hashValue match user input
//<---------------->set Boolean value authenticated to true
//<---------------->to indicate log-in was successful
authenticated = true;
//<---------------->Open and read the matching role text file returned from roleValue
//<---------------->(i.e. admin.txt; veterinarian.txt; zookeeper.txt)
Scanner readRFile = new Scanner(new File(roleValue.concat(".txt")));
//<---------------->Display message stored in the matching role text file
//<---------------->(i.e. admin.txt; veterinarian.txt; zookeeper.txt)
while (readRFile.hasNextLine()) {
System.out.println("");
System.out.print(readRFile.nextLine());
}
break; //End while loop at End Of File (EOF)
}
}
}
//<----->If the user log-in was successful and the matching role message
//<----->has been displayed, user is asked if they wish to exit the system
if (authenticated) {
System.out.print(" Do you want to exit the system? (y/n): ");
String choice = readUserInput.nextLine();
//<-------->Exit the system if the user chooses to do so
//<-------->If the user chooses to stay in the system
//<-------->loop back to original log-in request
if (choice.toLowerCase().charAt(0) == 'y') {
System.out.println(" User logged off! ");
break; //Program halted by user
}
else {
loginAttempts = 0; //Reset log-in attempts
System.out.println("");
}
}
//<---->If the log-in fails, increment variable loginAttempts
else {
loginAttempts++;
//<-------->If loginAttempts threshold IS met, tell the user so and exit the system
if (loginAttempts == 3) {
System.out.println(" Maximum number of failed attempts reached! Exiting system... ");
break;
}
//<-------->If loginAttempts threshold is NOT met allow the user additional log-in
//<-------->attempts until log-in IS successful OR loginAttempts threshold is met
//<-------->Let the user know how many attempts they have left until system exits
else {
System.out.println(" Invalid username or password..." +
" Please enter valid credentials!" +
" You have" + " " + (3 - loginAttempts) + " " +
"attempt(s) left.");
System.out.println("");
}
}
}
}
}
TEXT FILES:
credentials.txt
griffin.keyes 108de81c31bf9c622f76876b74e9285f "alphabet soup" zookeeper
rosario.dawson 3e34baa4ee2ff767af8c120a496742b5 "animal doctor" admin
bernie.gorilla a584efafa8f9ea7fe5cf18442f32b07b "secret password" veterinarian
donald.monkey 17b1b7d8a706696ed220bc414f729ad3 "M0nk3y business" zookeeper
jerome.grizzlybear 3adea92111e6307f8f2aae4721e77900 "grizzly1234" veterinarian
bruce.grizzlybear 0d107d09f5bbe40cade3de5c71e9e9b7 "letmein" admin
admin.txt
Hello, System Admin!
As administrator, you have access to the zoo's main computer system. This allows you to monitor users in the system and their roles.
veterinarian.txt
Hello, Veterinarian!
As veterinarian, you have access to all of the animals' health records. This allows you to view each animal's medical history and current treatments/illnesses (if any), and to maintain a vaccination log.
zookeeper.txt
Hello, Zookeeper!
As zookeeper, you have access to all of the animals' information and their daily monitoring logs. This allows you to track their feeding habits, habitat conditions, and general welfare.
Explanation / Answer
import java.io.File;
import java.security.MessageDigest;
import java.util.Scanner;
/*
*
* Authenticate class is used to authorize the user
*/
class Authenticate {
// validates the user id and password
public boolean authorize(String userName, String password) throws Exception {
Scanner readCFile = new Scanner(new File("Credentials.txt"));
boolean authenticated = false;
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(password.getBytes());
byte[] digest = md.digest();
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
// <---->While Loop to read user information in the
// <---->credentials.txt file until End Of File (EOF)
while (readCFile.hasNextLine()) {
// <-------->Parse the contents of credentials.txt file
String credInfo = readCFile.nextLine();
String parsedInfo[] = credInfo.split(" ");
String nameValue = parsedInfo[0].trim();
String hashValue = parsedInfo[1].trim();
String roleValue = parsedInfo[3].trim();
// <-------->Comparing parsed nameValue to username input
if (nameValue.equalsIgnoreCase(userName)) {
// <------------>Comparing parsed hashValue to password
// input
if (hashValue.equals(sb.toString())) {
// <---------------->If nameValue and hashValue match
// user input
// <---------------->set Boolean value authenticated to
// true
// <---------------->to indicate log-in was successful
authenticated = true;
// <---------------->Open and read the matching role
// text file returned from roleValue
// <---------------->(i.e. admin.txt; veterinarian.txt;
// zookeeper.txt)
Scanner readRFile = new Scanner(new File(roleValue.concat(".txt")));
// <---------------->Display message stored in the
// matching role text file
// <---------------->(i.e. admin.txt; veterinarian.txt;
// zookeeper.txt)
while (readRFile.hasNextLine()) {
System.out.println("");
System.out.print(readRFile.nextLine());
}
break; // End while loop at End Of File (EOF)
}
}
}
readCFile.close();
return authenticated;
}
}
public class NewZooAuthentication {
/**
*
* @param args
* the command line arguments
* @throws Exception
*
**/
public static void main(String[] args) throws Exception {
Authenticate au = new Authenticate();
// <---->Scanner object to read input from the console
Scanner readUserInput = new Scanner(System.in);
// <---->Variable to keep track of the number of log-in attempts
int loginAttempts = 0;
// <---->While Loop until break encountered
// <---->(user exits system or 3 unsuccessful log-in attempts are made)
while (true) {
// <---->Request user input username
System.out.print("Enter username: ");
// <---->Set String variable equal to user input
String userName = readUserInput.nextLine();
// <---->Request user input password
System.out.print("Enter password: ");
// <---->Set String variable equal to user input
String userPwd = readUserInput.nextLine();
// <---->Copied and pasted code section to convert the
// <---->password with MessageDigest-5 (MD5) hash
// <----->End copied and pasted code section
// <---->Boolean variable declared to verify authentication
boolean authenticated = au.authorize(userName, userPwd);
// <---->Scanner object to read credentials.txt file
// <----->If the user log-in was successful and the matching role
// message
// <----->has been displayed, user is asked if they wish to exit the
// system
if (authenticated) {
System.out.print(" Do you want to exit the system? (y/n): ");
String choice = readUserInput.nextLine();
// <-------->Exit the system if the user chooses to do so
// <-------->If the user chooses to stay in the system
// <-------->loop back to original log-in request
if (choice.toLowerCase().charAt(0) == 'y') {
System.out.println(" User logged off! ");
break; // Program halted by user
} else {
loginAttempts = 0; // Reset log-in attempts
System.out.println("");
}
}
// <---->If the log-in fails, increment variable loginAttempts
else {
loginAttempts++;
// <-------->If loginAttempts threshold IS met, tell the user so
// and exit the system
if (loginAttempts == 3) {
System.out.println(" Maximum number of failed attempts reached! Exiting system... ");
break;
}
// <-------->If loginAttempts threshold is NOT met allow the
// user additional log-in
// <-------->attempts until log-in IS successful OR
// loginAttempts threshold is met
// <-------->Let the user know how many attempts they have left
// until system exits
else {
System.out.println(" Invalid username or password..." + " Please enter valid credentials!"
+ " You have" + " " + (3 - loginAttempts) + " " + "attempt(s) left.");
System.out.println("");
}
}
}
}
}
Note : Please check and confirm , if you need any changes I will make it further
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.