Home > OS >  I am trying to implement this algorithm to display the name of a person who has the highest salary,
I am trying to implement this algorithm to display the name of a person who has the highest salary,

Time:10-17

The method is returning the name but it's returning the name in the first posion of the array: but i want it to return Part Time Employee with highest salary: Please help here is what I tried, this is the method:

public string PartTimeEmployeeWithHighestSalary()
    {
        string name = employees[0].GetName();
        double salary = employees[0].GetSalary();

        for (int i = 0; i < count; i  )
        {
            if (employees[i].GetSalary() > salary && employees[i] is PartTimeEmployee)
            {
                salary = employees[i].GetSalary();
                name = employees[i].GetName();
            }
        }
        return name;
    }

and here is the program codes:The expected result is "Wills" but im getting "Muwana"

static void Main(string[] args)
    {
        Employee EmpS1 = new ServiceEmployee("S123", "Muwana", "Sein",1000);
        Employee EmpS2 = new ServiceEmployee("S124", "Prince", "Jack",1500);
        Employee EmpS3 = new ServiceEmployee("S125", "Matengu", "Prince",6000);
        Employee EmpP1 = new PartTimeEmployee("P133", "Mad", "Max",2500);
        Employee EmpP2 = new PartTimeEmployee("P134", "Wills", "Sanjay",3000);
        Employee EmpP3 = new PartTimeEmployee("P135", "Rick", "Sanchez",1000);
        Employee EmpM1 = new ManagementEmployee("M143", "Morty", "Smith",1500);
        Employee EmpM2 = new ManagementEmployee("M143", "Summer", "Smith",6500);
        Employee EmpM3 = new ManagementEmployee("M144", "Beth", "Smith",6500);

        HRDepartment company = new HRDepartment(500);

        company.AddEmployee(EmpS1);
        company.AddEmployee(EmpS2);
        company.AddEmployee(EmpS3);
        company.AddEmployee(EmpP1);
        company.AddEmployee(EmpP2);
        company.AddEmployee(EmpP3);
        company.AddEmployee(EmpM1);
        company.AddEmployee(EmpM2);
        company.AddEmployee(EmpM3);

        //-1. PartTime employee with highest salary
        Console.WriteLine("PartTime employee with highest salary:{0}",company.PartTimeEmployeeWithHighestSalary());

CodePudding user response:

Your code is not that bad, but this is wrong:

string name = employees[0].GetName();
double salary = employees[0].GetSalary();

This would work only if you could guarantee that employees[0] is a part-time employee.

Instead, initialize salary with -1. That should work. Of course you should then also check for whether salary is still -1 after the execution of the loop, which means that the loop has not encountered any part-time employee.

CodePudding user response:

As it has been pointed out in the comments the problem most likely is with the count variable that's used in your loop condition. It's value is either <0, 0, or 1. Hence the loop is never entered or it stops after the first iteration.

There are several ways to address this:

  • Check the value of count and make sure it aligns with the length of employees.
  • Do not implement such a search by yourself and use the standard library for that:
public string PartTimeEmployeeWithHighestSalary() => employees
    .OfType<PartTimeEmployee>()
    .MaxBy(x => x.GetSalary())
    .GetName();
// or in case employees might not contain any part-timers:
public string? PartTimeEmployeeWithHighestSalary() => employees
    .OfType<PartTimeEmployee>()
    .MaxBy(x => x.GetSalary())?
    .GetName();
  • Use the Count property of the employees list (or Length if it's an array; or Count() if you're not sure) instead of the count variable/member:
for (int i = 0; i < employees.Count; i  )

Another problem was pointed out in this answer: your initialization of name and salaray are wrong. This could be fixed like so:

public string? PartTimeEmployeeWithHighestSalary()
{
    string? name = null;
    double salary = double.MinValue;

    for (int i = 0; i < employees.Count(); i  )
    {
        // same as before
    }
    
    return name;
}

Now PartTimeEmployeeWithHighestSalary() will return null if there is no part-time employee in the company.

CodePudding user response:

Try updating your PartTimeEmployeeWithHighestSalary with the below implementation

public string PartTimeEmployeeWithHighestSalary()
{
   return employees.OfType<PartTimeEmployee>().OrderByDescending(x => x.Salary).First().Name;
}
  • Related