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

This project will implement a simple username/password lookup system using hash

ID: 3572707 • Letter: T

Question

This project will implement a simple username/password lookup system using hash tables in Java. You should create the Hash Table Class from scratch.Create a separate class called HashEntry, in which you can store a String username and String password. And a LogIn class.

The Hash Table will store these entries using the username as the key and the password as the value. Create a hash code using the hash key to determine where the value is stored in the Hash Table. Here is a sample hash method. Feel free to borrow it for your program: private int calcHashCode(String key){ int mod = key.hashCode() % size; return mod < 0 ? mod + size : mod; }

You will be provided with a data file containing comma separated usernames and passwords. Read the data in from the file and put it into the table. Once the table is populated, ask the user to log into the system. If their username and password match the data in the table, alert the user that access has been granted:

Example:

Login: trevor

User trevor not found.

Login: mary

Password: test

Authentication Failure

Login: mary

Password: contrary

Authentication Successful

Welcome mary Provided Data File (containing comma separated usernames and passwords)

_____________________________________________________________________________________________________________

Here is the file of user names and passwords to use in the hash table (password.dat)

jack,broken.crown
jill,tumblin'down
mary,contrary
bopeep,sheep!lost

This means the user name is jack and his password is broken.crown. Next, username jill and password tumblin'down. Third, mary and password contrary. Fourth, bopeep and password sheep!lost.

Please write it completely in java.

Explanation / Answer


public class HashEntry {
  
   private String userName;
   private String password;
   private int size=4;
   public int getSize() {
       return size;
   }
   public void setSize(int size) {
       this.size = size;
   }
   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 int calHashCode(String key) {
      
       int mod=key.hashCode()%size;
       return mod<0?mod+size:mod;
      
      
      
   }
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       HashEntry other = (HashEntry) obj;
       if (password == null) {
           if (other.password != null)
               return false;
       } else if (!password.equals(other.password))
           return false;
       if (userName == null) {
           if (other.userName != null)
               return false;
       } else if (!userName.equals(other.userName))
           return false;
       return true;
   }
  

}

--------------------------------------------------------------------------------------------------------------

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Scanner;

public class HashTableDemo {

   public static void main(String[] args) throws IOException {
       Enumeration usernames = null;

       HashEntry entry = null;
       Hashtable<String, String> table = new Hashtable<String, String>();
       File file = new File("D:\users.txt");
       BufferedReader br = new BufferedReader(new FileReader(file));
       String line1 = br.readLine();
       entry = new HashEntry();
       while (line1 != null) {
           String username = null;
           String password = null;
           String[] line2 = line1.split(",");
           for (int i = 0; i < line2.length; i++) {

               if (i == 0) {

                   username = line2[i];
                   entry.setUserName(username);

               }
               password = line2[1];

               entry.setPassword(password);
           }

           table.put(username, password);

           line1 = br.readLine();
       }

       Scanner scanner = new Scanner(System.in);

       System.out.println("enter the username");
       String username1 = scanner.next();

       usernames = table.keys();

       while (usernames.hasMoreElements()) {
           String key = (String) usernames.nextElement();
           /*
           * if(!key.trim().equals(username1.trim())) {
           * System.out.println("the "+username1+" "+"not found");
           *
           * }
           */ if (key.trim().equals(username1.trim())) {
               System.out.println("enter the password");
               String password = scanner.next();
               String password1 = table.get(key);
               if (password.trim().equals(password1)) {
                   System.out.println("authentication success");
                   break;
               } else {
                   System.out.println("password mismatched");
                   break;
               }

           }
       }

   }

}

output

enter the username
mary
enter the password
test
password mismatched

enter the username
mary
enter the password
contrary
authentication success

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote