Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //menu of employees with sorting types and orders (using delegate)
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Project_2
- {
- class Program
- {
- private static int index = 0;
- static List<Employee> employees = new List<Employee>();
- //public static ArrayList employees = new ArrayList();
- static EmployeeSort employeeSort = new EmployeeSort();
- static EmployeeSortReverse employeeSortReverse = new EmployeeSortReverse();
- public delegate void SortType();
- public static SortType sortType;
- static void Main(string[] args)
- {
- //Employee[] arr = new Employee[]
- //{
- // new Employee{ID = 1, Salary = 50 },
- // new Employee{ID = 2, Salary = 10 },
- // new Employee{ID = 3, Salary = 30}
- //};
- //int result = arr[1].CompareTo(arr[2]);
- //Array.Sort(arr);
- List<string> menuItems = new List<string>() { "New", "Display", "Sort", "Exit" };
- Console.CursorVisible = false;
- while (true)
- {
- string selectedMenuItem = drawMenu(menuItems);
- if (selectedMenuItem == "New")
- {
- Console.Clear();
- newEmployee();
- }
- else if (selectedMenuItem == "Display")
- {
- Console.Clear();
- displayEmployee();
- }
- else if (selectedMenuItem == "Sort")
- {
- Console.Clear();
- sortType = new SortType(sortEmployees);
- sortType.Invoke();
- }
- else if (selectedMenuItem == "Exit")
- {
- Environment.Exit(0);
- }
- }
- }
- private static string drawMenu(List<string> items)
- {
- for (int i = 0; i < items.Count; i++)
- {
- if (i == index)
- {
- Console.BackgroundColor = ConsoleColor.Gray;
- Console.ForegroundColor = ConsoleColor.Black;
- Console.WriteLine(items[i]);
- }
- else
- {
- Console.WriteLine(items[i]);
- }
- Console.ResetColor();
- }
- ConsoleKeyInfo ckey = Console.ReadKey();
- if (ckey.Key == ConsoleKey.DownArrow)
- {
- if (index == items.Count - 1)
- {
- //index = 0;
- }
- else
- {
- index++;
- }
- }
- else if (ckey.Key == ConsoleKey.UpArrow)
- {
- if (index <= 0)
- {
- //index = menuItems.count - 1;
- }
- else
- {
- index--;
- }
- }
- else if (ckey.Key == ConsoleKey.Enter)
- {
- return items[index];
- }
- else
- {
- return "";
- }
- Console.Clear();
- return "";
- }
- static void newEmployee()
- {
- string name;
- int id, salary;
- Console.WriteLine("Enter employee information.");
- Console.Write("ID: "); id = int.Parse(Console.ReadLine());
- Console.Write("Name: "); name = Console.ReadLine();
- Console.Write("Salary: "); salary = int.Parse(Console.ReadLine());
- Employee e = new Employee(id, name, salary);
- employees.Add(e);
- }
- static void sortEmployees()
- {
- Console.WriteLine("Choose your sorting order:\n1. Ascending\n2. Descending");
- int orderChoice = int.Parse(Console.ReadLine());
- switch (orderChoice)
- {
- case 1:
- Console.WriteLine("Choose sorting type:\n1. ID\n2. Name\n3. Salary");
- int choice1 = int.Parse(Console.ReadLine());
- switch (choice1)
- {
- case 1:
- employeeSort.sortChoice = EmployeeSort.sortBy.ID;
- break;
- case 2:
- employeeSort.sortChoice = EmployeeSort.sortBy.name;
- break;
- case 3:
- employeeSort.sortChoice = EmployeeSort.sortBy.salary;
- break;
- default:
- Console.WriteLine("Error, invalid choice");
- break;
- }
- employees.Sort(employeeSort);
- break;
- case 2:
- Console.WriteLine("Choose sorting type:\n1. ID\n2. Name\n3. Salary");
- int choice2 = int.Parse(Console.ReadLine());
- switch (choice2)
- {
- case 1:
- employeeSortReverse.sortChoice = EmployeeSortReverse.sortBy.ID;
- break;
- case 2:
- employeeSortReverse.sortChoice = EmployeeSortReverse.sortBy.name;
- break;
- case 3:
- employeeSortReverse.sortChoice = EmployeeSortReverse.sortBy.salary;
- break;
- default:
- Console.WriteLine("Error, invalid choice");
- break;
- }
- employees.Sort(employeeSortReverse);
- break;
- }
- }
- static void displayEmployee()
- {
- foreach (var employee in employees)
- {
- Console.WriteLine("Employee ID: {0}\nEmployee Name: {1}\nEmployee Salary: {2}\n", employee.ID, employee.name, employee.salary);
- }
- //for (int i = 0; i < employees.Count; i++)
- //{
- // Console.WriteLine("Employee ID: {0}\nEmployee Name: {1}\nEmployee Salary: {2}\n", ((Employee)employees[i]).ID, ((Employee)employees[i]).name, ((Employee)employees[i]).salary);
- //}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement