0

How do I add input to the HashSet in the given problem. I mainly want to use the addEmployee, removeEmployee, printEmployee methods in the Manager class, but I can't figure out what to write in the main method so that the addEmployee, removeEmployee and printEmployee methods of Manager class are invoked

public class EmployeeTest
{
   public static void main(String[] args) 
   {
      Manager mgr = new Manager(207, "Barbara Johnson", "054-12-2367", 109_501.36, "US Marketing");
      Employee e=new Employee(127, "Kyle Jenner", "023-42-5368", 123_243.90); // newly edited
      mgr.addEmployee(e); //newly edited
      mgr.printEmployee(e); //newly edited
      printEmployee(mgr);
   }
   public static void printEmployee(Employee emp) 
   {
      System.out.println(); 
      System.out.println("Employee id:         " + emp.getEmpId());
      System.out.println("Employee name:       " + emp.getName());
      System.out.println("Employee Soc Sec #:  " + emp.getSsn());
      System.out.println("Employee salary:     " + NumberFormat.getCurrencyInstance().format((double) emp.getSalary()));          
   }
}

public class Employee 
{
  private int Id;
  private String name;
  private String ssn;
  private double salary;
  public Employee(int Id, String name, String ssn, double salary)
  {
     this.Id = Id;
     this.name = name;
     this.ssn = ssn;
     this.salary = salary;
  }
  public int getEmpId() 
  {
     return Id;
  }
  public String getName() 
  {
     return name;
  }
  public String getSsn()
  {
     return ssn;
  }
  public double getSalary() 
  {
     return salary;
  }
  public void setName(String name) 
  {
     if (name != null && !name.equals("")) 
     {
         this.name = name;
     }
  }
  public void raiseSalary(double increase) 
  {
     if (increase > 0) 
     {
         salary += increase;

     }
  }
}

public class Manager extends Employee 
{
  private String dept;
  public Manager(int Id, String name, String ssn, double salary, String dept)
  {
     super(Id, name, ssn, salary);
     this.dept = dept;
  }
  **private Set<Employee> staff=new HashSet<>();**  //This is where I need help
  **public void addEmployee(Employee e)**  //This is where I need help
  {
     staff.add(e);
  }
  **public void removeEmployee(Employee e)**  //This is where I need help
  {
     staff.remove(e);
  }
  **public void printEmployee(Employee e)**  //This is where I need help
  {
     for (Employee emp: staff)
     {
        System.out.println("Name : "+getName()+" "+"ID : "+getEmpId());
     }
  }
  public String getDeptName()
  {
     return dept;
  }
}

Output Of Program

Name : Barbara Johnson ID : 207 //Desired Output:- Name : Kyle Jenner ID : 127

Employee id: 207

Employee name: Barbara Johnson

Employee Soc Sec #: 054-12-2367

Employee salary: Rs.109,501.36

Testing raiseSalary and setName on Manager:

Employee id: 207

Employee name: Barbara Johnson-Smythe

Employee Soc Sec #: 054-12-2367

Employee salary: Rs.119,501.36

4
  • You could ... create some Employees and then call methods like mgr.addEmployee( myEmployee ) ... Commented Oct 15, 2015 at 20:24
  • I have done as you asked.....created an instance of employee class then called the methods in main method like you said but it adds and prints the same name and id i.e Barbara Johnson in this case Commented Oct 16, 2015 at 5:26
  • Can you show your current code and the output? Otherwise, we have to guess. The code above only asks for the manager to be printed, because you're calling EmployeeTest.printEmployee(), not Manager.printEmployee(). If you want the manager's employees printed, use mgr.printEmployee(). Commented Oct 16, 2015 at 13:31
  • I have edited my code and I have indicated it using 3 tags saying "//newly edited". Also, I have included the output of the code. Here I am assuming Kyle Jenner which is passed to Employee class is actually a staff under Manager class(an employee under Manager) and I want to print its detail as shown in the desired output above. Commented Oct 16, 2015 at 23:35

2 Answers 2

1

Since the static type of emp in printEmployee() is Employee you can use only the method which are defined in Employee (or defined there and overriden in Manager). Since printEmployee() is defined in Manager it can be used only in variables declaed as Manager

Sign up to request clarification or add additional context in comments.

Comments

0

Your output shows the managers name because Manager.printEmployee() is calling get getName() and getEmpID() on the Manager object, not on its employees.

**public void printEmployee(Employee e)**  //This is where I need help
{
  for (Employee emp: staff)
  {
     System.out.println("Name : "+getName()+" "+"ID : "+getEmpId()); 
  } 
}

(In addition, this method receives a reference e to an Employee object that it then ignores.)

Remember how you modified printEmployee(e); to mgr.printEmployee(e); to call the method on the Manager object referenced by mgr? You'll need to do the same thing to call the method's on the Employee objects.

public class Manager extends Employee {
   **public void printEmployee()**  //This is where I need help
   {
      for (Employee emp: staff)
      {
         System.out.println("Name : "+emp.getName()+" "+"ID : "+emp.getEmpId()); 
      } 
   }
   ...

Here's the key thing to remember. You're not just calling a set of functions. You're interacting with objects by calling methods on them. If you want to interact with an object, you have to call methods through a reference to that object -- as you've done with mgr.addEmployee() in the main() method.

2 Comments

thanks it solved my problem. one more thing I wanted to ask can we downcast an object of a class. like for e.g Employee e=new Employee(something...) and then Manager mgr1=(Manager)e and finally to mgr1.addEmployee(e) and mgr1.printEmployee(e) but the problem is when i try this code it throws class cast exception error.
No, you can't do that. Casting only changes the type of the reference, not the type of object to which the reference points. At compile time, the syntax is legal, because e might point to a Manager object at runtime. But at runtime, when you try to cast, Java detects that e is not currently pointing to Manager object. Since the code cannot do what it's been asked to do, it throws an exception. [If you're familiar with C++, although the syntax is similar to raw casts, the behavior is closer to dynamic_cast() of a reference.]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.