import java.io.File;

import java.security.MessageDigest;

import java.util.Scanner;

public class AuthenticationSystemZoo {

     public static void main(String[] args) throws Exception {

         //Read input from the console via a scanner
             Scanner readInput=new Scanner(System.in);

             //Declare a variable to track 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) {

                 //Obtain username input from user via method nextLine
                 System.out.print("Enter user name: ");
                 String uName=readInput.nextLine();

                 //Obtain password input from user via method nextLine
                 System.out.print("Enter password: ");  
                 String original = readInput.nextLine();

                 //Use a message digest five (MD5) hash to convert password
                 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));
                 }

                 /*Declare a boolean variable to keep track whether
                 a login is successful or not*/
                 boolean authenticationSuccess=false;

                 //Open the credentials file
                 Scanner readCred=new Scanner(new File("Credentials.txt"));

                 //Look for user credentials in the credentials file via method hasNextLine
                 while(readCred.hasNextLine()) {

                      String record=readCred.nextLine();    //Read a record via method nextLine
                      String columns[]=record.split("\t");  //Create columns to reference file data by splitting the record into fields

                      /*Validate the credentials against the values 
                      provided in the credentials file*/

                      //Check user name input by user against first column.
                      if(columns[0].trim().equals(uName)) {

                           /*If user name matches, validate whether the Converted password matches the
                           message digest five hashed password in the second column*/
                           if(columns[1].trim().equals(sb.toString())) {

                                /*If the passwords are same, set the boolean value
                                AuthenticationSuccess to true. Login is a success.*/
                                authenticationSuccess=true;     

                                //Determine the user's role by reading the fourth column
                                Scanner readRole=new Scanner(new File(columns[3].trim()+".txt"));

                                //Display the information stored in the role file using hasNextLine method
                                while(readRole.hasNextLine()) {
                                     System.out.println(readRole.nextLine());
                                }

                                break;
                           }

                      }

                 }

                 //If login successful, determine if user wants to log out

                 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
                      AuthenticationSuccess 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!\nExiting...");

                           break;

                      }

                      //otherwise, prompt to enter credentials again
                      else {
                           System.out.println("Please enter correct credentials!");     

                      }

                 }

             }

        }

}