static void Main(string[] args) { ///Employee class array with each of employees data List <Employee> employeeList = Read(); // To use IComparer Employee.EmployeeComparer compareEmployee = Employee.GetComparer(); // Display data options and invoke collection sorting method // Main method MainSelectionMethod(employeeList, compareEmployee); }
///////////////////////////////////////////////////////////////////////////////// /// <summary> /// Based off the user selection, this method will invoke one of cases /// To compare data and sort using IComparer /// </summary> /// <param name="employeeList">a class with employee data</param> /// <param name="selection">user selection input</param> /// <param name="compareEmployee">Employee.EmployeeComparer</param> static void CollectionSort(List <Employee> employeeList, int selection, Employee.EmployeeComparer compareEmployee) { switch (selection) { //Sorting method for Name ASC case 1: compareEmployee.WhichComparison = Employee.EmployeeComparer.ComparisonType.Name; employeeList.Sort(compareEmployee); break; //Sorting Method For number ASC case 2: compareEmployee.WhichComparison = Employee.EmployeeComparer.ComparisonType.Number; employeeList.Sort(compareEmployee); break; //Sorting Method For RATE DESC case 3: compareEmployee.WhichComparison = Employee.EmployeeComparer.ComparisonType.Rate; employeeList.Sort(compareEmployee); employeeList.Reverse(); break; //Sorting Method For Hours DESC case 4: compareEmployee.WhichComparison = Employee.EmployeeComparer.ComparisonType.Hours; employeeList.Sort(compareEmployee); employeeList.Reverse(); break; //Sorting Method For Gross DESC case 5: compareEmployee.WhichComparison = Employee.EmployeeComparer.ComparisonType.Gross; employeeList.Sort(compareEmployee); employeeList.Reverse(); break; } }
/// <summary> /// LOOP IF user selection is not 6 /// Get user input from the console /// IF user selection is from 1 to 5 /// THEN invoke Sort() method to sort employee data depending on user option /// PRINT OUT employee data /// ELSE IF user selection is equal to 6 then close console /// ELSE PRINT OUT invalid input message /// </summary> /// <param name="employeeList">list of employees</param> /// <param name="compareEmployee">EmployeeComparer</param> private static void MainSelectionMethod(List <Employee> employeeList, Employee.EmployeeComparer compareEmployee) { int selection = 0; //Figure out what option user selects while (selection != 6) { DisplayOptions(); string userChoice = Console.ReadLine(); if (int.TryParse(userChoice, out selection)) { Console.Clear(); //Clear console for each run if (selection > 0 && selection < 6) { Console.WriteLine(PrintTableValues()); //print out table header //before displaying data, sort the data using IComparer CollectionSort(employeeList, selection, compareEmployee); for (int printE = 0; printE < employeeList.Count(); printE++) { Console.WriteLine(employeeList[printE]); } Console.WriteLine(); } else if (selection == 6) { Environment.Exit(0); //close console function } else { Console.WriteLine("\r\n*** Invalid Choice - Try Again ***\r\n"); } } else { Console.WriteLine("\r\n*** Invalid Choice - Try Again ***\r\n"); } } }