The program below takes values entered by the user and stores them into a HashMap . I am specifically having issues with option #3 " Retrieve an Employee" . All my data gets entered into the HashMap correctly , but I run option #3 I will get an incorrect result ? from the output the data displayed is the last input form the user ? When I debug the program the "value" in the map is correct with what was entered.......so if my "values" are correct and I am using the "containsvale" method , why is my result returning the last value entered by the user ? Thank you for any suggestions !
so if my "values" are correct and I am using the "containsvale" method , why is my result returning the last value entered by the user ?
It is not "returning" the last value entered by the user -- you just never assigned the firstname and lastname variables, before printing them. So, basically, it is printing the last value that was assigned, which was probably when a user was added.
Fist of all I would advice to reread the documentation of Map. It stores key, value pairs. You would like to use SSN as a key but you put it as a value into the map.
Here you use firstname and lastname as keys, so whenever the user enters a name which's firstname or lastname is already used, then it'll change the map entry.
This mistake makes the whole program incorrect.
To answer your question:
You got the last entered name as an answer for a retrieve, since you print out the last entered name, you do nothing with the map in userDescision 3:
HashMap<Integer,List<String>> would store all the information of an employee (firstname,lastname,department,desig. etc) in a list identified by unique key SSN. I don't know whether just first name and last name needs to be stored but this approach will store values in structured way and retrieving would not be a problem.
in the choice 1 (add Employee) create a list for each SSN and then store it as
This way while retrieving if you try ssnKeys.containsKey(SSN), it should return the whole list. Also maintaining methods(addEmployee(),deleteEmployee() etc.) for each of the options would be a good idea.
I think a better(more object oriented) aproach would be to store employee data in an Emlpoyee object.
I saw that you already have an Employee class but you haven't posted the code. I advice you to creaet a Map<Integer, Employee> to store the employees. Create an employee instance when user creates a new employee, then put it to the map using SSN as the key.
You are not really using the HashMap to its full potential right now. By using the containsValue() method to look up values, you are using it as little more than a glorified List. I believe someone else has suggested you should use a employee's ssn as the key and create a employee object that hold the employee's name (and probably ssn) and that that as the value. You should be pretty good then.