La respuesta del reto implementando la “interface”
Interface Employee
namespace OpenClose
{
public interface IEmployee
{
public string Fullname { get; set; }
public int HoursWorked { get; set; }
public decimal CalculateSalaryMonthly();
}
}
Y en las clases
Método EmployeePartTime
public class EmployeePartTime : IEmployee
{
public string Fullname { get ; set ; }
public int HoursWorked { get ; set ; }
public EmployeePartTime(string fullname, int hoursWorked)
{
Fullname = fullname;
HoursWorked = hoursWorked;
}
public decimal CalculateSalaryMonthly()
{
decimal hourValue = 20000M;
decimal salary = hourValue * HoursWorked;
if (HoursWorked > 160) {
decimal effortCompensation = 5000M;
int extraDays = HoursWorked - 160;
salary += effortCompensation * extraDays;
}
return salary;
}
}
Método EmployeeFullTime
public class EmployeeFullTime : IEmployee
{
public string Fullname { get ; set ; }
public int HoursWorked { get ; set; }
public EmployeeFullTime(string fullname, int hoursWorked)
{
Fullname = fullname;
HoursWorked = hoursWorked;
}
public decimal CalculateSalaryMonthly()
{
decimal hourValue = 30000M;
decimal salary = hourValue * HoursWorked;
return salary;
}
}
y program
using OpenClose;
CalculateSalaryMonthly(new List<IEmployee>() {
new EmployeeFullTime("Pepito Pérez", 160),
new EmployeePartTime("Manuel Lopera", 180)
});
void CalculateSalaryMonthly(List<IEmployee> employees)
{
foreach (var employee in employees)
{
Console.WriteLine($"Empleado: {employee.Fullname}, Pago: {employee.CalculateSalaryMonthly()} ");
}
}
¿Quieres ver más aportes, preguntas y respuestas de la comunidad?