A company has many employees & each employee is led by only 1 person except for the CEO (who has no boss). The cost to the company of an employee is the sum of his salary plus the cost to the company of all the people led by him. Given is the following structure :EmployeeData members:NameSalaryisBossnameOfBossMethods:getSalarygetNamea) Define the class/structureb) Write a function getCostToCompany() to calculate the cost to the company of an employee whose name is passed as a parameter to the function getCostToCompany()public float getCostToCompany(String name)Note: Don’t write any input functions such as main() & assume that the data structures have already been defined.

Showing Answers 1 - 9 of 9 Answers

import java.util.HashMap;

class A {

 static HashMap employees = new HashMap();

 public static void main(String[] args) {

  Employee e0 = new Employee(true, "ceo", null, 10.00f);
  employees.put("ceo", e0);

  Employee e1 = new Employee(true, "amit", "ceo", 10.00f);
  employees.put("amit", e1);

  Employee e2 = new Employee(true, "sumit", "amit", 10.00f);
  employees.put("sumit", e2);

  Employee e3 = new Employee(false, "devendra", "sumit", 10.00f);
  employees.put("devendra", e3);

  System.out.println(new A().getCostToCompany("amit"));

 }

 float cost = 0.0f;

 public float getCostToCompany(String name) {

  Employee e = (Employee) employees.get(name);

  if (e.nameOfBoss != null) {
   cost = cost + getCostToCompany(e.nameOfBoss);

  }
  return cost + e.getSalary();

 }

}

class Employee {

 private String name;

 private float salary;

 public boolean isBoss;

 public String nameOfBoss;

 public Employee(boolean boss, String name, String bossName, float salary) {
  super();
  // TODO Auto-generated constructor stub
  isBoss = boss;
  this.name = name;
  nameOfBoss = bossName;
  this.salary = salary;
 }

 public String getName() {
  return name;
 }

 public float getSalary() {
  return salary;
 }

}

ephemeral89

  • Jun 25th, 2010
 

This is a classic example of a situation where you can use Trees data structure. I have not solved the problem yet but believe this can be done using very efficiently using Tress and there traversal algorithms...

  Was this answer useful?  Yes

The solutions given here already are wrong. They just do the opposite of what is needed. e.g. in the solution, if you call out the salary for ceo, that comes 10.0 and if you call out salary for devedra, that is coming highest. So it is just doing the reverse of what is needed.

I have given a try by changing it to:

package com.ibm.test;

import java.util.HashMap;

class A {

    static HashMap employees = new HashMap();

    public static void main(String[] args) {
//        Employee(boolean hasDRs, String name, Employee[] drs, float salary)
        Employee e01 = new Employee(false, "madhur", null, 10.00f);
        Employee e02 = new Employee(false, "vivek", null, 10.00f);
        Employee e03 = new Employee(false, "sunil", null, 10.00f);
        Employee e0 = new Employee(true, "lokesh", new Employee[]{e01, e02, e03}, 10.00f);
       
        Employee e11 = new Employee(false, "a", null, 10.00f);
        Employee e12 = new Employee(false, "b", null, 10.00f);
        Employee e13 = new Employee(false, "c", null, 10.00f);
        Employee e1 = new Employee(true, "monica", new Employee[]{e11, e12, e13}, 10.00f);
       
        Employee e2 = new Employee(true, "ranjit", new Employee[]{e0, e1}, 10.00f);
       
        employees.put("madhur", e01);
        employees.put("vivek", e02);
        employees.put("sunil", e03);

        employees.put("lokesh", e0);
       
        employees.put("a", e11);
        employees.put("b", e12);
        employees.put("c", e13);
       
        employees.put("monica", e1);
       
        employees.put("ranjit", e2);
       
        System.out.println(new A().getCostToCompany("ranjit"));

    }

    float cost = 0.0f;

    public float getCostToCompany(String name) {

          Employee e = (Employee) employees.get(name);

          if (e.hasDirectReportees()) {
              for (int i = 0; i < e.getDrs().length; i++) {
                Employee e1 = e.getDrs()[i];
                cost = cost + e1.getSalary();
                getCostToCompany(e1.getName());
            }
          

          }
          return cost + e.getSalary();

         }

}

class Employee {

    private String name;

    private float salary;

    public boolean directReportees;

    private Employee[] drs;
    public Employee(boolean hasDRs, String name, Employee[] drs, float salary) {
        super();
        // TODO Auto-generated constructor stub
        directReportees = hasDRs;
        this.name = name;
        this.drs = drs;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public float getSalary() {
        return salary;
    }

    /**
     * @return Returns the directReportees.
     */
    public boolean hasDirectReportees() {
        return directReportees;
    }
    /**
     * @param directReportees The directReportees to set.
     */
    public void setDirectReportees(boolean directReportees) {
        this.directReportees = directReportees;
    }
    /**
     * @return Returns the drs.
     */
    public Employee[] getDrs() {
        return drs;
    }
    /**
     * @param drs The drs to set.
     */
    public void setDrs(Employee[] drs) {
        this.drs = drs;
    }
}

In the example above, here is the hierarchy
ranjit sits on top. To him the people report are monica and lokesh.
Under monica, there are 3 employees - a, b and c
under lokesh, there are 3 employees - madhur, vivek and sunil

If you simply run the program, it gets the cost of company for ranjit which comes as 90 and that is correct.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions