Interview question on Class design
Asked Answered
S

5

6

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.

Sempstress answered 28/3, 2011 at 9:46 Comment(4)
suggest you post you attempt to design the classes....Isidoro
Probably the interviewer was paying more attention to the questions you asked than the class layout you gave.Araliaceous
Captain Obvious' move: they've asked for class design so they expect to see how would you design classes relationship for this 'system'.Asset
Recently I attended an interview and they asked me to do some pre work which was similar to this. "What answer is the interviewer expecting from me?" or what anser was he expecting.Woermer
A
2

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
    }
  }
}
Anyplace answered 28/3, 2011 at 9:53 Comment(5)
Design 1 doesn't make much sense to me. Why having a bool flag AND inheritance?Lara
@Lara - its just for checking that employee is contract or full time while making callAnyplace
I don't know if that's discouraged in C#, but wouldn't a check for the instance be sufficient? if(empInst is FullTimeEmp){}Lara
Checking instance types or testing a boolean is anti-OO (worse still if it has a setter): use a polymorphic CalculatePayroll() method. CalculatePayroll() can simply ignore hours on a fulltime employee. Design1 is fine except that isContractEmployee is a) unneeded and b) bad style.Pentose
I think you also want a Manager class and instance which keeps a list of fulltime and contract employees, then its CalculatePayroll() method should return the total for all its subordinates (plus itself, optionally). Manager can be a subclass of FulltimeEmployee (with a mixin if it needs other special methods).Pentose
E
6

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)
Edythedythe answered 28/3, 2011 at 10:1 Comment(0)
M
3

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);
    }
}
Mallette answered 28/3, 2011 at 10:11 Comment(1)
I'm not really sure if this is the answer they were looking for. I think the were looking for polymorphism insteadSewer
A
2

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
    }
  }
}
Anyplace answered 28/3, 2011 at 9:53 Comment(5)
Design 1 doesn't make much sense to me. Why having a bool flag AND inheritance?Lara
@Lara - its just for checking that employee is contract or full time while making callAnyplace
I don't know if that's discouraged in C#, but wouldn't a check for the instance be sufficient? if(empInst is FullTimeEmp){}Lara
Checking instance types or testing a boolean is anti-OO (worse still if it has a setter): use a polymorphic CalculatePayroll() method. CalculatePayroll() can simply ignore hours on a fulltime employee. Design1 is fine except that isContractEmployee is a) unneeded and b) bad style.Pentose
I think you also want a Manager class and instance which keeps a list of fulltime and contract employees, then its CalculatePayroll() method should return the total for all its subordinates (plus itself, optionally). Manager can be a subclass of FulltimeEmployee (with a mixin if it needs other special methods).Pentose
W
0

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.

Woermer answered 28/3, 2011 at 14:44 Comment(0)
Y
0

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());
    }
}
Yes answered 6/2, 2017 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.