Question: Java programming)- I am having issues where the code I've written isn't displaying the user's role information (admin.txt, zookeeper.txt, veterinarian.txt). My suspicion is that the

Java programming)- I am having issues where the code I've written isn't displaying the user's role information (admin.txt, zookeeper.txt, veterinarian.txt). My suspicion is that the program isn't gathering the user's role correctly through the scanner in public boolean userValidation, but I'm not sure. Can anyone help me with this issue?

____________________________________________________________________________________________________________________________________________________

THE CODE-

authenticationsystem.java(main program):

package authenticationsystem;

import java.util.Scanner; import java.io.IOException; import java.security.NoSuchAlgorithmException;

public class AuthenticationSystem {

public static void main(String[] args) throws NoSuchAlgorithmException, Exception { Scanner scnr = new Scanner(System.in); int failedAttempts = 0; int maxAttempts = 3; boolean logOut; String userName; String userPass; UserInfo newUser = new UserInfo();

while (failedAttempts <= 3) { System.out.println("Please enter your USERNAME:"); userName = scnr.nextLine(); System.out.println("Please enter your PASSWORD:"); userPass = scnr.nextLine(); userPass = newUser.getHashedPass(userPass);

if (newUser.userValidation(userName, userPass) == true) { newUser.getUserRole(); logOut = false; while (!logOut) { System.out.println("OPTIONS:"); System.out.println("_______"); System.out.println("Enter \"log out \" to return to log in screen."); System.out.println("Enter \"quit\" to close application."); switch (scnr.nextLine()) { case "log out": System.out.println("Logging out."); logOut = true; break; case "quit": System.out.println("Closing application."); System.exit(0); } } } else { System.out.println("Incorrect Username or Password."); ++failedAttempts; --maxAttempts; System.out.println("You have " + maxAttempts + " log in attempts remaining."); }

if (failedAttempts == 3) { System.out.println("Log in attempts exceeded!"); System.out.println("Program closing."); System.exit(0); } } } } ____________________________________________________________________________________________________________________________________________________

userInfo.java:

package authenticationsystem;

import java.util.Scanner; import java.security.MessageDigest; import java.io.IOException; import java.io.FileInputStream; import java.security.NoSuchAlgorithmException; import java.io.File; import java.io.FileNotFoundException;

public class UserInfo {

private String userName; private String userPass; private String userRole;

public String getHashedPass(String password) throws NoSuchAlgorithmException { String original = password; MessageDigest md = MessageDigest.getInstance("MD5"); md.update(original.getBytes()); byte[] digest = md.digest(); StringBuilder sb = new StringBuilder(); for (byte b : digest) { sb.append(String.format("%02x", b & 0xff)); } this.userPass = sb.toString(); return userPass; }

public boolean userValidation(String user, String password) throws IOException { File credFile = new File("src/authenticationsystem/credentials.txt"); Scanner credInfo = new Scanner(credFile); String validUser, validPass; userName = user; userPass = password;

while (credInfo.hasNextLine()) { Scanner credLine = new Scanner(credInfo.nextLine()); validUser = credLine.next(); validPass = credLine.next(); userRole = credLine.next(); if (validUser.equals(userName) && validPass.equals(userPass)) { return true; }

} return false; } public String getUserPos() { return userRole; }

public void getUserRole() throws FileNotFoundException { if (this.getUserPos().equals("admin")) { File adminFile = new File("src/authenticationsystem/admin.txt"); Scanner adminInfo = new Scanner(adminFile); while (adminInfo.hasNextLine()) { System.out.println(adminInfo.nextLine()); } if (this.getUserPos().equals("veterinarian")) { File vetFile = new File("src/authenticationsystem/veterinarian.txt"); Scanner vetInfo = new Scanner(vetFile); while (vetInfo.hasNextLine()) { System.out.println(vetInfo.nextLine()); } if (this.getUserPos().equals("zookeeper")) { File zookeepFile = new File("src/authenticationsystem/zookeeper.txt"); Scanner zookeepInfo = new Scanner(zookeepFile); while (zookeepInfo.hasNextLine()) { System.out.println(zookeepInfo.nextLine()); } } } } } }

____________________________________________________________________________________________________________________________________________________

credential.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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!