alexandrheathen

Untitled

Apr 2nd, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.58 KB | None | 0 0
  1. using System;
  2.  
  3. namespace StructConsole
  4. {
  5.     class Worker
  6.     {
  7.         private string Name;
  8.         private int Year;
  9.         private byte Month;
  10.  
  11.        
  12.         public Worker()
  13.         {
  14.             Name = "Trener V. J.";
  15.             Year = DateTime.Now.Year;
  16.             Month = (byte)DateTime.Now.Month;
  17.         }
  18.  
  19.         public Worker(string name, int year, byte month)
  20.         {
  21.             SetName(name);
  22.             SetYear(year);
  23.             SetMonth(month);
  24.         }
  25.  
  26.         public Worker(string name, int year)
  27.         {
  28.             SetName(name);
  29.             SetYear(year);
  30.             SetMonth((byte)DateTime.Now.Month);
  31.         }
  32.  
  33.         public Worker(Worker copy)
  34.         {
  35.             Name = copy.Name;
  36.             Year = copy.Year;
  37.             Month = copy.Month;
  38.         }
  39.  
  40.         public void SetName(string name)
  41.         {
  42.             if(name.Length > 0)
  43.                 Name = name;
  44.         }
  45.  
  46.         public void SetYear(int year)
  47.         {
  48.             if (year > 2000 && year < 2019)
  49.                 Year = year;
  50.         }
  51.  
  52.         public void SetMonth(byte month)
  53.         {
  54.             if (month > 0 && month < 13)
  55.                 Month = month;
  56.         }
  57.  
  58.  
  59.         public string GetName()
  60.         {
  61.             return Name;
  62.         }
  63.  
  64.         public int GetYear()
  65.         {
  66.             return Year;
  67.         }
  68.  
  69.         public byte GetMonth()
  70.         {
  71.             return Month;
  72.         }
  73.     }
  74.  
  75.     class Company
  76.     {
  77.         private string Name;
  78.         private string Position;
  79.         private int Salary;
  80.         public Worker NewWorker;
  81.         public Company()
  82.         {
  83.             Name = "Valve";
  84.             Position = "Cashhunter";
  85.             Salary = 1234567;
  86.         }
  87.  
  88.         public Company(string position, int salary)
  89.         {
  90.             Name = "Valve";
  91.             Position = position;
  92.             Salary = salary;
  93.         }
  94.  
  95.         public Company(string name, string position, int salary)
  96.         {
  97.             Name = name;
  98.             Position = position;
  99.             Salary = salary;
  100.         }
  101.  
  102.         public Company(Company copy)
  103.         {
  104.             Name = copy.Name;
  105.             Position = copy.Position;
  106.             Salary = copy.Salary;
  107.         }
  108.  
  109.         public void SetName(string name)
  110.         {
  111.             if(name.Length > 0)
  112.                 Name = name;
  113.         }
  114.  
  115.         public void SetPosition(string position)
  116.         {
  117.             if(position.Length > 0)
  118.                 Position = position;
  119.         }
  120.  
  121.         public void SetSalary(int salary)
  122.         {
  123.             if (salary > 0 && int.TryParse(Console.ReadLine(), out salary))
  124.                 Salary = salary;
  125.             else
  126.                 Console.WriteLine("Enter another value!");
  127.         }
  128.  
  129.         public string GetName()
  130.         {
  131.             return Name;
  132.         }
  133.  
  134.         public string GetPosition()
  135.         {
  136.             return Position;
  137.         }
  138.  
  139.         public int GetSalary()
  140.         {
  141.             return Salary;
  142.         }
  143.  
  144.         public void SetWorker(string name, int year, byte month)
  145.         {
  146.             NewWorker.SetName(name);
  147.             NewWorker.SetYear(year);
  148.             NewWorker.SetYear(month);
  149.         }
  150.  
  151.         public int GetWorkExperience(Company copy)
  152.         {
  153.             DateTime YourYear = new DateTime(copy.NewWorker.GetYear());
  154.             DateTime Today = new DateTime(DateTime.Now.Year);
  155.             int res = Convert.ToInt32((Today - YourYear));
  156.             return res / 12;
  157.         }
  158.  
  159.         public int GetTotalMoney(Company copy)
  160.         {
  161.             return copy.GetSalary() * NewWorker.GetMonth();
  162.         }
  163.     }
  164.  
  165.     class Program
  166.     {
  167.         static void StopMech()
  168.         {
  169.             Console.ForegroundColor = ConsoleColor.White;
  170.             Console.WriteLine("Press enter to continue...");
  171.             Console.ReadKey();
  172.         }
  173.         static void CheckInputData(out int n, string phrase)
  174.         {
  175.             bool flag = true;
  176.             do
  177.             {
  178.                 Console.Write(phrase);
  179.                 if(!Int32.TryParse(Console.ReadLine(), out n))
  180.                 {
  181.                     flag = false;
  182.                     Console.ForegroundColor = ConsoleColor.DarkRed;
  183.                     Console.WriteLine("Error404. Try to enter again!");
  184.                 }
  185.             } while (!flag);
  186.         }
  187.         public static Worker[] ReadAllWorkers()
  188.         {
  189.             int len;
  190.             string lenPhrase = "Enter worker's amount: ";
  191.             CheckInputData(out len, lenPhrase);
  192.             Worker[] workers = new Worker[len];
  193.             for (int i = 0; i < workers.Length; i++)
  194.             {
  195.                 Console.ForegroundColor = ConsoleColor.DarkMagenta;
  196.                 Console.WriteLine("~ ~ ~ ~ WORKER #{0} ~ ~ ~ ~", i + 1);
  197.                 workers[i] = ReadWorker();
  198.             }
  199.             return workers;
  200.         }
  201.  
  202.         public static Worker ReadWorker()
  203.         {
  204.             Worker worker = new Worker();
  205.             Console.Write("Enter worker's surname and initials: ");
  206.             worker.SetName(Console.ReadLine());
  207.             int year;
  208.             string yearPhrase = "Enter worker's experience (years): ";
  209.             CheckInputData(out year, yearPhrase);
  210.             worker.SetYear(year);
  211.             //worker.SetYear(Convert.ToInt32(Console.ReadLine()));
  212.             int month;
  213.             string monthPhrase = "Enter the month worker started working: ";
  214.             CheckInputData(out month, monthPhrase);
  215.             worker.SetMonth((byte)month);
  216.             ReadCompany();
  217.             return worker;
  218.         }
  219.  
  220.         public static Company ReadCompany()
  221.         {
  222.             Company company = new Company();
  223.             Console.ForegroundColor = ConsoleColor.DarkBlue;
  224.             Console.Write("Enter company name: ");
  225.             company.SetName(Console.ReadLine());
  226.             Console.Write("Enter worker's position in company: ");
  227.             company.SetPosition(Console.ReadLine());
  228.             int salary;
  229.             string salaryPhrase = "Enter worker's salary: ";
  230.             CheckInputData(out salary, salaryPhrase);
  231.             return company;
  232.         }
  233.  
  234.         public static void PrintCompany(Company company)
  235.         {
  236.             Console.ForegroundColor = ConsoleColor.White;
  237.             Console.Write("Company: ");
  238.             Console.ForegroundColor = ConsoleColor.DarkGreen;
  239.             company.GetName();
  240.             Console.ForegroundColor = ConsoleColor.White;
  241.             Console.Write("Position: ");
  242.             Console.ForegroundColor = ConsoleColor.DarkGreen;
  243.             company.GetPosition();
  244.             Console.Write("Salary: ");
  245.             Console.ForegroundColor = ConsoleColor.DarkGreen;
  246.             company.GetSalary();
  247.         }
  248.  
  249.         public static void PrintWorker(Worker worker)
  250.         {
  251.             Company company = new Company();
  252.             Console.ForegroundColor = ConsoleColor.DarkYellow;
  253.             Console.Write("Worker's name: ");
  254.             Console.ForegroundColor = ConsoleColor.Blue;
  255.             worker.GetName();
  256.             Console.ForegroundColor = ConsoleColor.DarkYellow;
  257.             Console.Write("Worker's experience: ");
  258.             Console.ForegroundColor = ConsoleColor.Blue;
  259.             worker.GetYear();
  260.             Console.ForegroundColor = ConsoleColor.DarkYellow;
  261.             Console.Write("Worker works since: ");
  262.             Console.ForegroundColor = ConsoleColor.Blue;
  263.             worker.GetMonth();
  264.             PrintCompany(company);
  265.         }
  266.  
  267.         public static void PrintWorkers(Worker[] workers)
  268.         {
  269.             for (int i = 0; i < workers.Length; i++)
  270.             {
  271.                 Console.WriteLine("- - - - WORKER #{0} - - - -", i + 1);
  272.                 PrintWorker(workers[i]);
  273.             }
  274.             StopMech();
  275.         }
  276.  
  277.         public void GetWorkersInfo(Worker[] workers, out int highest, out int lowest)
  278.         {
  279.             Console.Clear();
  280.             int highest = int.MinValue, lowest = int.MaxValue;
  281.             for (int i = 0; i < workers.Length; i++)
  282.             {
  283.                 if()
  284.             }
  285.         }
  286.  
  287.         public int SortWorkersBySalary(Worker a, Worker b)
  288.         {
  289.             Company company = new Company();
  290.             double avgB = company.GetWorkExperience(company);
  291.             double avgA = company.GetWorkExperience(company);
  292.             if (avgA > avgB)
  293.                 return 1;
  294.             if (avgA < avgB)
  295.                 return -1;
  296.             return 0;
  297.         }
  298.  
  299.         public int SortWorkersByExperience(Worker a, Worker b)
  300.         {
  301.             DateTime fstDateA = new DateTime(a.GetYear());
  302.             DateTime sndDateB = new DateTime(b.GetYear());
  303.             return DateTime.Compare(fstDateA, sndDateB);
  304.         }
  305.  
  306.         static void Menu()
  307.         {
  308.             Console.Title = "Лабораторна робота №5";
  309.             Console.ForegroundColor = ConsoleColor.White;
  310.             Console.WriteLine("~ ~ ~ ~ MENU ~ ~ ~ ~");
  311.             Console.ForegroundColor = ConsoleColor.Cyan;
  312.             Console.WriteLine("1. Add workers;\n2. Show all workers;\n3. Show the HIGHEST and LOWEST salaries;\n4. Sort workers by SALARY;\n5. Sort workers by EXPERIENCE");
  313.         }
  314.         static void Main(string[] args)
  315.         {
  316.             ConsoleKeyInfo hotKey;
  317.             int len = 0; //highest, lowest;
  318.             Worker[] workers = new Worker[len];
  319.             do
  320.             {
  321.                 Menu();
  322.                 hotKey = Console.ReadKey();
  323.                 switch(hotKey.Key)
  324.                 {
  325.                     case ConsoleKey.D1: // Add worker
  326.                         workers = ReadAllWorkers();
  327.                         len = workers.Length;
  328.                         break;
  329.                     case ConsoleKey.D2: // Show all workers
  330.                         PrintWorkers(workers);
  331.                         break;
  332.                     case ConsoleKey.D3: // Show the highest and lowest salary
  333.                        
  334.                         break;
  335.                     case ConsoleKey.D4: // Sort workers by salary
  336.                         break;
  337.                     case ConsoleKey.D5: // Sort workers by experience
  338.                         break;
  339.                     case ConsoleKey.D0:
  340.                         Console.Clear();
  341.                         Console.ForegroundColor = ConsoleColor.Green;
  342.                         Console.WriteLine("See you later!");
  343.                         Environment.Exit(0);
  344.                         break;
  345.                 }
  346.             } while (true);
  347.         }
  348.     }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment