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:
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With