No tienes acceso a esta clase

隆Contin煤a aprendiendo! 脷nete y comienza a potenciar tu carrera

Aplicando el principio abierto/cerrado

8/16
Recursos

Aportes 7

Preguntas 2

Ordenar por:

驴Quieres ver m谩s aportes, preguntas y respuestas de la comunidad?

o inicia sesi贸n.

La respuesta del reto implementando la 鈥渋nterface鈥

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()} ");
    }
}

El concepto de clase Abstracta siempre me ha costado trabajo. Este ejercicio, deja m谩s claro de qu茅 se trata

asi quisiera ganar yo al mes馃槩鈥 $4,800,000.0

En el CalculateSalaryMonthly() de la clase EmployeePartTime hace falta las lineas de c贸digo para calcular las horas extras cuando es mayor a 160

if (HoursWorked > 160) {
                decimal effortCompensation = 5000M;
                int extraDays = HoursWorked - 160;
                salary += effortCompensation * extraDays;
            }

Yo cambie la clase abstracta porque el salario considero debe ir dentro

namespace OpenClose; 

public abstract class Employee 
{
    public string Fullname { get; }
    public int HoursWorked { get; }
    public decimal HourValue { get; }

    public Employee(string fullname, int hoursWorked, decimal hourValue)
    {
        Fullname = fullname; 
        HoursWorked = hoursWorked; 
        HourValue = hourValue; 
    }
    
    public abstract decimal CalculateMonthlySalary(); 
}
public class EmployeeFullTime : Employee
    {

        public EmployeeFullTime(string fullname, int hoursWorked, decimal hourValue)
        : base(fullname, hoursWorked, hourValue)
        {
        }

        public override decimal CalculateMonthlySalary()
        {
            return HourValue * HoursWorked;
        }
    }
public class EmployeePartTime : Employee
    {
        public int hoursLimitForExtraCompensation { get; set; } = 160; 
        public decimal effortCompensation  { get; set; } = 5000M; 

        public EmployeePartTime(string fullname, int hoursWorked, decimal hourValue)
        : base(fullname, hoursWorked, hourValue)
        {
        }

        public override decimal CalculateMonthlySalary()
        {
            var salary = HourValue * HoursWorked;
            if (HoursWorked > hoursLimitForExtraCompensation) {
                int extraDays = HoursWorked - hoursLimitForExtraCompensation;
                salary += effortCompensation * extraDays;
            }
            return salary;
        }
    }

La respuesta al reto:

interface IEmployee
    {
        decimal CalculateSalaryMonthly();
        string Fullname { get; set; }
    }
    public abstract class Employee: IEmployee
    {
        public string Fullname { get; set; } = "";
        public int HoursWorked { get; set; }
        public abstract decimal CalculateSalaryMonthly();
    }

Entonces la subclase queda asi:

 public class EmployeeContractor: Employee
    {
        public EmployeeContractor(string fullname, int hoursWorked)
        {
            Fullname = fullname;
            HoursWorked = hoursWorked;
        }  
        public override decimal CalculateSalaryMonthly()
        {
            decimal hourValue = 34000M;
            decimal salary = hourValue * HoursWorked;

            return salary;
        }
    }

Y el programa:

ShowSalaryMonthly(new List<IEmployee>() {
    new EmployeeFullTime("Pepito P茅rez", 160),
    new EmployeeContractor("Mariana G贸mez", 140),
    new EmployeePartTime("Manuel Lopera", 180)
});

Gracias, ahora si pude comprender lo de las Clases Abstractas, les dejo una pagina muy interesante

https://dotnettutorials.net/lesson/open-closed-principle/