Advertisement
Statev

Employees

Feb 1st, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace _3.Employees
  8. {
  9.     class Employees
  10.     {
  11.         private static void ConsolePositionsReader(Dictionary<string, int> positions)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             for (int i = 0; i < n; i++)
  15.             {
  16.                 string[] inputs = Console.ReadLine().Split('-');
  17.                 InputPositionReader(positions, inputs);
  18.             }
  19.         }
  20.  
  21.         private static void FilePositionsReader(Dictionary<string, int> positions)
  22.         {
  23.             StreamReader textReader = new StreamReader(@"../../Positions.txt");
  24.             using (textReader)
  25.             {
  26.                 int n = int.Parse(textReader.ReadLine());
  27.                 for (int i = 0; i < n; i++)
  28.                 {
  29.                     string[] inputs = textReader.ReadLine().Split('-');
  30.                     InputPositionReader(positions, inputs);
  31.                 }
  32.             }
  33.         }
  34.  
  35.         private static void InputPositionReader(Dictionary<string, int> positions, string[] inputs)
  36.         {
  37.             string name = inputs[0].Remove(inputs[0].Length - 1);
  38.             int value = int.Parse(inputs[1].Remove(0, 1));
  39.             if (positions.ContainsKey(name))
  40.             {
  41.                 //positions[name] = value;
  42.             }
  43.             else
  44.             {
  45.                 positions.Add(name, value);
  46.             }
  47.         }
  48.  
  49.         private static void ConsoleEmployeesReader(Dictionary<string, int> positions, List<Employee> AllEmployees)
  50.         {
  51.             int m = int.Parse(Console.ReadLine());
  52.             for (int i = 0; i < m; i++)
  53.             {
  54.                 string[] inputs = Console.ReadLine().Split('-');
  55.                 InputEmployeesReader(positions, AllEmployees, inputs);
  56.             }
  57.         }
  58.  
  59.         private static void FileEmployeesReader(Dictionary<string, int> positions, List<Employee> AllEmployees)
  60.         {
  61.             StreamReader textReader = new StreamReader(@"../../Employees.txt");
  62.             using (textReader)
  63.             {
  64.                 int m = int.Parse(textReader.ReadLine());
  65.                 for (int i = 0; i < m; i++)
  66.                 {
  67.                     string[] inputs = textReader.ReadLine().Split('-');
  68.                     InputEmployeesReader(positions, AllEmployees, inputs);
  69.                 }
  70.             }
  71.         }
  72.  
  73.         private static void InputEmployeesReader(Dictionary<string, int> positions, List<Employee> AllEmployees, string[] inputs)
  74.         {
  75.             Employee newEmplyee = new Employee();
  76.             string[] names = inputs[0].Remove(inputs[0].Length - 1).Split(' ');
  77.             newEmplyee.FirstName = names[0];
  78.             newEmplyee.LastName = names[1];
  79.             newEmplyee.PositionRank = positions[inputs[1].Remove(0, 1)];
  80.             AllEmployees.Add(newEmplyee);
  81.         }
  82.  
  83.         private static void EmployeesChanger(List<Employee> AllEmployees, int i, int j)
  84.         {
  85.             Employee dublicate = new Employee();
  86.             dublicate = AllEmployees[j];
  87.             AllEmployees[j] = AllEmployees[i];
  88.             AllEmployees[i] = dublicate;
  89.         }
  90.  
  91.         private static void EmployeesSort(List<Employee> AllEmployees)
  92.         {
  93.             for (int i = 0; i < AllEmployees.Count; i++)
  94.             {
  95.                 for (int j = i + 1; j < AllEmployees.Count; j++)
  96.                 {
  97.                     if (AllEmployees[j] > AllEmployees[i])
  98.                     {
  99.                         EmployeesChanger(AllEmployees, i, j);
  100.                     }
  101.                 }
  102.             }
  103.         }
  104.  
  105.         private static void EmployeesPrint(List<Employee> AllEmployees)
  106.         {
  107.             for (int i = 0; i < AllEmployees.Count; i++)
  108.             {
  109.                 Console.WriteLine("{0} {1}",AllEmployees[i].FirstName,AllEmployees[i].LastName);
  110.             }
  111.         }
  112.  
  113.         static void Main(string[] args)
  114.         {
  115.             Dictionary<string, int> positions = new Dictionary<string, int>();
  116.             ConsolePositionsReader(positions);
  117.             //FilePositionsReader(positions);
  118.  
  119.             List<Employee> AllEmployees = new List<Employee>();
  120.             ConsoleEmployeesReader(positions, AllEmployees);
  121.             //FileEmployeesReader(positions, AllEmployees);
  122.  
  123.             EmployeesSort(AllEmployees);
  124.             EmployeesPrint(AllEmployees);
  125.         }
  126.     }
  127.  
  128.     class Employee
  129.     {
  130.         private string firstName;
  131.         private string lastName;
  132.         private int positionRank;
  133.         public string FirstName
  134.         {
  135.             get { return firstName; }
  136.             set { firstName = value; }
  137.         }
  138.         public string LastName
  139.         {
  140.             get { return lastName; }
  141.             set { lastName = value; }
  142.         }
  143.         public int PositionRank
  144.         {
  145.             get { return positionRank; }
  146.             set { positionRank = value; }
  147.         }
  148.         public Employee()
  149.         {
  150.             firstName = "";
  151.             lastName = "";
  152.             positionRank = 0;
  153.         }
  154.         public Employee(string firstName, string lastName, int positionRank)
  155.         {
  156.             this.firstName = firstName;
  157.             this.lastName = lastName;
  158.             this.positionRank = positionRank;
  159.         }
  160.         public static bool operator >(Employee e1, Employee e2)
  161.         {
  162.             if (e1.positionRank == e2.positionRank)
  163.             {
  164.                 if (String.Compare(e1.lastName, e2.lastName) == 1)
  165.                 {
  166.                     return false;
  167.                 }
  168.                 else
  169.                 {
  170.                     return true;
  171.                 }
  172.             }
  173.             return e1.positionRank > e2.positionRank;
  174.         }
  175.         public static bool operator <(Employee e1, Employee e2)
  176.         {
  177.             if (e1.positionRank == e2.positionRank)
  178.             {
  179.                 if (String.Compare(e1.lastName, e2.lastName) == 1)
  180.                 {
  181.                     return true;
  182.                 }
  183.                 else
  184.                 {
  185.                     return false;
  186.                 }
  187.             }
  188.             return e1.positionRank < e2.positionRank;
  189.         }
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement