Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have to create this authentication system in Java. These are the tasks I have

ID: 3705804 • Letter: I

Question

I have to create this authentication system in Java. These are the tasks I have to do.

1. Prompt user for a username

2. Prompt user for a password

3.Check if the username and password is a match within the credentials file.

4. Once it is checked. Each user has a role. So if the user is an admin, a message from a file should show up.

5.The user only has 3 attempts to type in their password and username. After that the program should exit out.

6.Allow a user to log out afterwards.

How do I go about doing this. Do I need another class to be able to check the credentials from a different file? Should I use a While loop for the 3 attempts? How do I allow a user to log out?

Thank you.

Explanation / Answer

If you think of doing login from putty in some unix terminal, it's behaviour is similar to somewhat what is described in question. So first you can have a java class representing authentication process like this :

AuthenticationTest.java :

import java.util.Scanner;

public class AuthenticationTest {

   private static Scanner inConsole = new Scanner(System.in);

  

   public static void main(String[] args) {

       CredentialsFile.populateDetailsFromFile("provide you file path here");

       int noOfAttempts = 0;

       String username="";

       String password ="";

       User user = null;

       while(noOfAttempts<4) {

           System.out.println("Enter user name");

           username = inConsole.next();

           System.out.println("Enter password");

           password = inConsole.next();

           user = CredentialsFile.getUser(username, password);

           if (user == null) {

               System.out.println("UserName = "+username+" or password = "+password+" is wrong please try again");

           } else {

               greetAndWaitForLogout(user);

               //when exit is typed user comes here

              System.exit(0);

           }

           noOfAttempts++;

       }

       if (noOfAttempts == 4) {

           System.out.println("Sorry you are out of no. of attempts to login");

       }

   }

  

   public static String greetAndWaitForLogout(User user) {

       if ("admin".equalsIgnoreCase(user.getRole())) {

           System.out.println(user.getGreetMessage());

       }

       String command = "";

       do {

           System.out.println("what you want to do next, if nothing please type exit");

           command = inConsole.next();

           //do some logic if command is not exit according to requirement

       }while (!"exit".equalsIgnoreCase(command));

       return command;

   }

  

}

Now the User class that we need for having all details about user should be something like this :

User.java :

public class User {

   private String userName;

   private String password;

   private String role;

   private String greetMessage;

  

   public User(String userName, String password, String role, String greetMessgage) {

       this.userName = userName;

       this.password = password;

       this.role = role;

       this.greetMessage = greetMessgage;

   }

   public String getUserName() {

       return userName;

   }

   public void setUserName(String userName) {

       this.userName = userName;

   }

   public String getPassword() {

       return password;

   }

   public void setPassword(String password) {

       this.password = password;

   }

   public String getRole() {

       return role;

   }

   public void setRole(String role) {

       this.role = role;

   }

   public String getGreetMessage() {

       return greetMessage;

   }

   public void setGreetMessage(String greetMessage) {

       this.greetMessage = greetMessage;

   }

  

}

And about reading credentials details from the file you can have a different class as a util which will do the all loading and reading and closing the file at the time of program initiation. Program can be like CredentialsFile.java. Now based on what kind of file you can implement logic like :

import java.util.ArrayList;

import java.util.List;

public class CredentialsFile {

   private static List<User> list;

  

   public static List<User> populateDetailsFromFile(String filePath) {

       list = new ArrayList<User>();

       //add logic here for reading user details from file.

       //If it is an excel file then use apache poi api

       //If it is an csv file then use csv reader api

       //If properties file then use properties reader api

       //If simple text file then you can use filereader and read line by line as String

       //and then split using delimiter like comma and populate user details till the end of file.

      return list;  

   }

  

   public static User getUser(String userName, String password) {

       User user = null;

       for (User u: list) {

           if (userName.equalsIgnoreCase(u.getUserName()) && password.equalsIgnoreCase(u.getPassword())) {

               user = u;

               break;

           }

       }

       return user;

   }

  

}