Hey i am making an application that stores employee's details and the output is different than what it should be i was expecting the output to be the toString() in the employee class can anyone see anythin wrong:

//Imports:

//********************************************************************
//Employee Class.
public class Employee
{
//Variables.
    private String employeeName;
    private int employeeId;
    private String employeeEmail;
//********************************************************************  
//Constructor.
    public Employee(String employeeName, int employeeId, String employeeEmail) 
    {
        this.employeeName = employeeName;
        this.employeeId = employeeId;
        this.employeeEmail = employeeEmail;
    }
//********************************************************************
//Getters.
    public String getEmployeeEmail() {
        return employeeEmail;
    }
    public void setEmployeeEmail(String employeeEmail) {
        this.employeeEmail = employeeEmail;
    }
    public String getEmployeeName() {
        return employeeName;
    }
    public int getEmployeeId() {
        return employeeId;
    }
//********************************************************************
//toString method.
    @Override
    public String toString() {
        return "Employee [employeeName=" + employeeName + ", employeeId="
                + employeeId + ", employeeEmail=" + employeeEmail + "]";
    }
//********************************************************************





}




//Imports.
import java.util.HashMap;
//********************************************************************
import java.util.Map;

public class EmployeeStore 
{
    HashMap<String, Employee> map;

//Constructor.  
    public EmployeeStore()
    {
        map = new HashMap<String,Employee>();
    }
//********************************************************************
//Hashmap Methods.
//Add to the Hashmap : Employee.
    public void add(Employee obj)
    {

        map.put(obj.getEmployeeName(), obj);
    }
//********************************************************************
//Remove from the Hashmap : Employee.
    public void remove(String key)
    {
      //Remove the Employee by name.
        map.remove(key);
    }
//********************************************************************
//Print the Hashmap : Employee. 
    public void print()
    {
        System.out.println("\n********Employee's in the Company.********");
        for(Map.Entry<String, Employee> theEntry : map.entrySet())
        {
            System.out.println("Employee Name:\t" + theEntry.getKey());
            System.out.println("Employee Id:\n" + theEntry.getKey());
            System.out.println("E-mail:\n" + theEntry.getKey());

        }
    }


//********************************************************************  
//********************************************************************


}






public class MainApp
{

    public static void main(String[] args)
    {
        new MainApp().start();

    }
    public void start()
    {
        EmployeeStore Store = new EmployeeStore();
        Store.add(new Employee ("James O' Carroll", 18,"hotmail.com"));
        Store.print();

    }

}

Recommended Answers

All 2 Replies

What exactly were you expecting, and how exactly does the actual output differ?

As far as i can tell this code just returns the key three times? :

for(Map.Entry<String, Employee> theEntry : map.entrySet())
        {
            System.out.println("Employee Name:\t" + theEntry.getKey());
            System.out.println("Employee Id:\n" + theEntry.getKey());
            System.out.println("E-mail:\n" + theEntry.getKey());
        }

If you want to get the information you need to call it through the Employee object ie. your "Getter" methods. The hash map is set up <Key, value> so you can get the value, which in this case will be the employee object, with theEntry.getValue().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.