简体   繁体   中英

Interview question on Class design

Recently I Attended an interview. This question was asked.

This is the scenario.

We have two type of employees. Regular and contract employee. Regular employees will be paid on a fixed basis at the end of the month. Contract employees will be paid weekly based on the number of hours they worked.

Managers will be assigned to these employees for supervision. A manager may have regular and contract employees under him.

This application will calculate the payroll for these employees.

They asked me to come up with the class design for this situation.

What answer is the interviewer expecting from me?. Pointers in this direction will be highly appreciated.

They are testing that you understand some basic tenets of good OO design. Specifically, they seem to be looking for:

  1. An understanding of Polymorphism (by using functionality of each Employee at abstract base level)
  2. An understanding of Specialisation (by extending the functionality of the base Employee class to produce different behaviour)

This is a question which would test you, if you have an understanding of the separation of concerns (calculating the wage of an employee is not a responsability of the employee itself), and to see if you understand abstractness and polymorphism: there are (at the moment) 2 different ways to calculate a wage. You could implement some kind of strategy design pattern in order to perform the calculation, so that each implementation of the algorithm is specified in a separate class. This helps maintainability and extension (if another kind of algorithm would be needed).

public class Employee
{
    public bool IsContractEmployee
    {
       get;
       set;
    }
}

public class WageCalculator
{

    private abstract class WageCalculAlgorithm
    { 
        public virtual decimal Calculate( Employee emp )
        {
             // regular calc goes here
        }
    }

    private class ContractorCalculator : WageCalculAgorithm
    {
        public override decimal Calculate( Employee emp )
        {
            // contractor calc goes here
        }
    }

    public static decimal CalculateWageFor( Employee emp )
    {
       if( emp.IsContractEmployee )
            return new ContractorCalculator().Calculate(emp);
       else
            return new WageCalculAlgorithm().Calculate(emp);
    }
}

following may be one of the design

design 1.

public class Employee
{
   public bool isContractEmployee
      { get; set;}

   public abstract float CalCulatePayroll();   
}


public class FullTimeEmp : Employee
{
   public override float CalCulatePayroll()
   {
   }
}

public class ContractEmp : Employee
{
  public int NoofHR
      {get; set;}

  public override float CalCulatePayroll()
   {
       sal = nohr*money;
   }
}

design 2.

public class employee
{
  public bool isContractEmployee
  { get; set;}

  public int NoofHR
  {get; set;}


  public  float CalCulatePayroll()
  {
    if(this.isContractEmployee)
    {
      //calculate sal on based hr
    }
    else
    {
      //calculate regurlare sal
    }
  }
}

I would go this way:

public class Employee
{        
     public abstract int CalculateSalary();
}



public class RegularEmployee : Employee
{
    public int NumOfWeeklyHours { get; set; }
    public int CalculateSalary()
    {
            // TODO: Implement
    }
}

public class ContractEmployee : Employee
{
    public int FixedBasis { get; set; }

    public override int CalculateSalary()
    {
        // TODO: Implement
    }
}

public class Manager
{
    public List<Employee> InChargeOf { get; set; }
}

public class PayRoll
{
    public int CalculateSalaries(List<Employee> le)
    {
         return le.Sum(e => e.CalculateSalary());
    }
}

The interviewer was probably expecting you to do your best to come up with a solution that you thought solved the problem.

He will have also expected you to explain some of the decisions you made while coming up with your solution.

Finally have expected you to have come up with the solution by yourself rather than asking for it on an internet forum.

It's hard to say technically what he expected as we don't know what level position you were going for. If you were going for your first developer job he might not have expected you to get anything like the correct answer but just wanted to see what your approach to problem solving was like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM