Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# using IComparable<> and IComparer

I'm having a bit of trouble properly using IComparable<> interface. I've also created a class that implements IComparer<>. I pretty much copied the exact coding example from a book but reworked it to fit my situation but it doesn't seem to function properly. I keep getting an error message The type name EmployeeComparer does not exist in the type Lab4a.Employee. As far as I can tell, the method is there and exists so I'm not sure what is triggering this. Thanks for any advice

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab4a
{
    class Employee : IComparable<Employee>
    {
        private string _name = ""; // holds the employee name
        private int _number = 0; // employee's unique id number
        private decimal _rate = 0.0m; // employees pay rate
        private double _hours = 0.0d; // hours worked by the employee
        private decimal _gross = 0.0m; // gross pay for a single employee

        public Employee(string name, int number, decimal rate, double hours)
        {
            // initialize variables
            this._name = name;
            this._number = number;
            this._rate = rate;
            this._hours = hours;

            // if the employee works more than 40 hours, calculate the overtime pay
            if (_hours > 40)
            {
                double overtimeHours = _hours - 40; // calculates overtime hours worked
                decimal overtimePay = (_rate * 1.5m) * (decimal)overtimeHours; // calculates pay for overtime hours worked
                _gross = Math.Round((_rate * 40) + overtimePay, 2); // calculates regular pay plus overtime pay
            }
            else // otherwise, calculate regular pay
            {
                _gross = Math.Round(_rate * (decimal)_hours, 2);
            }
        }

        // Static method to get a comparer object
        public static EmployeeComparer GetComparer()
        {
            return new Employee.EmployeeComparer();
        }

        // Default CompareTo method
        public int CompareTo(Employee rhs)
        {
            return this._number.CompareTo(rhs._number);
        }

        // Special implementation to be called by custom comparer
        public int CompareTo(Employee rhs, Employee.EmployeeComparer.ComparisonType Which)
        {
            switch (Which)
            {
                case Employee.EmployeeComparer.ComparisonType.name:
                    return this._name.CompareTo(rhs._name);
                case Employee.EmployeeComparer.ComparisonType.empID:
                    return this._number.CompareTo(rhs._number);
                case Employee.EmployeeComparer.ComparisonType.rate:
                    return this._rate.CompareTo(rhs._rate);
                case Employee.EmployeeComparer.ComparisonType.hours:
                    return this._hours.CompareTo(rhs._hours);
                case Employee.EmployeeComparer.ComparisonType.gross:
                    return this._gross.CompareTo(rhs._gross);
            }
            return 0;
        }


        public override string ToString()
        {
            string employeeString = String.Format("{0,-20} | {1, -10} | {2, -7} | {3, -7:#.00} | {4, 10}", _name, _number, _rate, _hours, _gross);
            return employeeString;
        }

        public void PrintEmployee()
        {
            // displays employee data with proper spacing and divider bars
            System.Console.WriteLine(String.Format("{0,-20} | {1, -10} | {2, -7} | {3, -7:#.00} | {4, 10}", _name, _number, _rate, _hours, _gross));
        }
    }

    // Nested class which implements IComparer
    public class EmployeeComparer : IComparer<Employee>
    {
        public Employee.EmployeeComparer.ComparisonType WhichComparison { get; set; }

        // Enumeration of comparison types
        public enum ComparisonType
        {
            name,
            empID,
            rate,
            hours,
            gross
        };

        public int Compare(Employee lhs, Employee rhs)
        {
            return lhs.CompareTo(rhs, WhichComparison);
        }
    }
}
like image 955
khmer2040 Avatar asked Feb 21 '26 13:02

khmer2040


1 Answers

The EmployeeComparer type is not nested into the Employee class right now. If you want it nested move its declaration within the Employee class.

like image 89
Crono Avatar answered Feb 24 '26 03:02

Crono