Joseph McGuire Joseph McGuire - 1 month ago 4
Java Question

Username/Password lookup in Hashmap

So as a homework assignment I'm supposed to be able to accept both an username and a password, then lookup a Hashmap that's already been formed (from a FakePersonDatbase text document), to see if that username and password exist inside the Hashmap; the Hashmap's Key is a String which is the username and the value is an object called User, which is formed from the FakePersonDatabase.

This is my attempt at it, I started by creating the same method that's used to created the Hashmap in the Database class except changing the username and password in the object to the Strings that were entered in the beginning, and then from there I kind of get lost and don't know quite how to go about comparing the new User object to user_map.

This is the User class/object that's used in both in the Main and the Database class

public class User
{
public String first_name, last_name, email, country, username, password, ip;

public User(String[] parts)
{
first_name = parts[0];
last_name = parts[1];
email = parts[2];
country = parts[3];
username = parts[4];
password = parts[5];
}
}


This is the Database, that's used to create the user_map Hashmap.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class Database
{
public static Map<String, User> load()
{
Map<String, User> user_map = new HashMap<String, User>();
try
{
Scanner data_store = new Scanner(new File("fake-people-db.txt"));

while (data_store.hasNextLine())
{
String[] split_string = data_store.nextLine().split(",");
User u = new User(split_string);
user_map.put(u.username, u);
}
}

catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
return user_map;
}
}


This is the main, which has been my attempt so far.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class Gatekeeper
{
public static void main(String[] args)
{
/* parse the user database */
Map<String, User> user_map = Database.load();
/* You now have a map full of users.
* The key is the username and the value is the user object.
* How can you check to see if the given username/password is correct? */
Scanner keyboard = new Scanner(System.in);

System.out.println("Enter username");
String username = keyboard.nextLine();

if(user_map.containsKey(username))
{
System.out.println("Enter password");
String password = keyboard.nextLine();

Map<String, User> user_compare = new HashMap<String, User>();

try
{
Scanner data_store = new Scanner(new File("fake-people-db.txt"));

while (data_store.hasNextLine())
{
String[] split_string = data_store.nextLine().split(",");
split_string [4] = username;
split_string [5] = password;
User u = new User(split_string);
user_compare.put(u.username, u);
/* I have a feeling that this is where the issue is occuring,
* I don't know how to compare the object created above to the user_map Hashmap.*/
if(u.equals(user_map.get(u.username)))
{
System.out.println("Hello");
}
}
System.out.println("Incorrect password");
}

catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
}
else
{
System.out.println("That username does not exist.");
}
}
}


Apologies for the sophomoric looking code, I'm a CCC student barely learning the ropes. Any help with this would be much appreciated!

Answer

You don't need to compare the whole User object, you just want to validate if the password is the valid one for that user, right?

You can add a method to your User class:

 public boolean doesPasswordEqual(String password) {
     return this.password.equals(password);
 }

Then in your main, you can do:

...
System.out.println("Enter username");
String username = keyboard.nextLine();

if(user_map.containsKey(username))
{
   System.out.println("Enter password");
   String password = keyboard.nextLine();

   if (user_map.get(username).doesPasswordEqual(password)) {
       System.out.println("Hello");
   } else {
       System.out.println("Incorrect password");
   }
}
Comments