Advertisement
KirillMysnik

Задача с сотрудниками

Jun 18th, 2015
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.92 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         Staff staff = new Staff();
  9.  
  10.         // Инициализация массива первой частью сотрудников
  11.         byte amount = ReadTools.ReadByte("Сколькими сотрудниками вы хотите инициализировать массив?");
  12.         staff.AddEmpolyees(amount);
  13.  
  14.         // Вывод первой части сотрудников
  15.         Console.Clear();
  16.         staff.PrintAll();
  17.         Console.ReadLine();
  18.  
  19.         // Добавление второй части сотрудников
  20.         amount = ReadTools.ReadByte("Сколько новых сотрудников добавить?");
  21.         staff.AddEmpolyees(amount);
  22.  
  23.         // Вывод всех сотрудников
  24.         Console.Clear();
  25.         staff.PrintAll();
  26.  
  27.         // Вывод данных о зарплате
  28.         Console.WriteLine("Ср. з/п: {0}, мин. з/п: {1}, макс. з/п: {2}", staff.AverageSalary(), staff.MinSalary(), staff.MaxSalary());
  29.         Console.ReadLine();
  30.     }
  31. }
  32.  
  33.  
  34. class ReadTools
  35. {
  36.     /// <summary>
  37.     /// Безопасно считывает значение типа byte с приглашением
  38.     /// </summary>
  39.     /// <param name="prompt">приглашение</param>
  40.     /// <returns>считываемое значение</returns>
  41.     public static byte ReadByte(string prompt)
  42.     {
  43.         byte rs;
  44.         do
  45.             Console.Write("{0}: ", prompt);
  46.  
  47.         while (!byte.TryParse(Console.ReadLine(), out rs));
  48.         return rs;
  49.     }
  50.  
  51.     /// <summary>
  52.     /// Безопасно считывает значение типа ushort с приглашением
  53.     /// </summary>
  54.     /// <param name="prompt">приглашение</param>
  55.     /// <returns>считываемое значение</returns>
  56.     public static ushort ReadUShort(string prompt)
  57.     {
  58.         ushort rs;
  59.         do
  60.             Console.Write("{0}: ", prompt);
  61.  
  62.         while (!ushort.TryParse(Console.ReadLine(), out rs));
  63.         return rs;
  64.     }
  65.  
  66.     /// <summary>
  67.     /// Считывает значение типа string с приглашением
  68.     /// </summary>
  69.     /// <param name="prompt">приглашение</param>
  70.     /// <returns>считываемое значение</returns>
  71.     public static string ReadString(string prompt)
  72.     {
  73.         Console.Write("{0}: ", prompt);
  74.         return Console.ReadLine();
  75.     }
  76. }
  77.  
  78.  
  79. class Employee
  80. {
  81.     public string name { get; set; }
  82.     public byte age { get; set; }
  83.     public ushort salary { get; set; }
  84.  
  85.     public Employee(string name, byte age, ushort salary)
  86.     {
  87.         this.name = name;
  88.         this.age = age;
  89.         this.salary = salary;
  90.     }
  91.  
  92.     public override string ToString()
  93.     {
  94.         return String.Format("СОТРУДНИК(имя: \"{0}\", возраст: {1}, зарплата: {2})", name, age, salary);
  95.     }
  96. }
  97.  
  98.  
  99. class Staff
  100. {
  101.     Employee[] employees = new Employee[0];
  102.  
  103.     /// <summary>
  104.     /// Ищет максимальную зарплату сотрудников
  105.     /// </summary>
  106.     /// <returns>максимальная зарплата</returns>
  107.     public ushort MaxSalary()
  108.     {
  109.         ushort max = ushort.MinValue;
  110.         foreach (Employee employee in employees)
  111.             if (employee.salary > max)
  112.                 max = employee.salary;
  113.  
  114.         return max;
  115.     }
  116.  
  117.     /// <summary>
  118.     /// Ищет минимальную зарплату сотрудников
  119.     /// </summary>
  120.     /// <returns>минимальная зарплата</returns>
  121.     public ushort MinSalary()
  122.     {
  123.         ushort min = ushort.MaxValue;
  124.         foreach (Employee employee in employees)
  125.             if (employee.salary < min)
  126.                 min = employee.salary;
  127.  
  128.         return min;
  129.     }
  130.  
  131.     /// <summary>
  132.     /// Ищет среднюю зарплату сотрудников
  133.     /// </summary>
  134.     /// <returns>средняя зарплата (или -1 при отсутствии сотрудников)</returns>
  135.     public double AverageSalary()
  136.     {
  137.         double av = 0.0;
  138.         foreach (Employee employee in employees)
  139.             av += employee.salary;
  140.  
  141.         return employees.Length == 0 ? -1 : av / employees.Length;
  142.     }
  143.  
  144.     /// <summary>
  145.     /// Запрашивает у пользователя новых сотрудников и расширяет массив сотрудников
  146.     /// </summary>
  147.     /// <param name="amount">количество запрашиваемых сотрудников</param>
  148.     public void AddEmpolyees(byte amount)
  149.     {
  150.         byte i;
  151.         Employee[] newEmployees = new Employee[employees.Length + amount];
  152.  
  153.         // Копирование уже имеющихся элементов
  154.         for (i = 0; i < employees.Length; i++)
  155.             newEmployees[i] = employees[i];
  156.  
  157.         string name;
  158.         byte age;
  159.         ushort salary;
  160.  
  161.         // Считывание новых сотрудников
  162.         for (i = (byte)employees.Length; i < employees.Length + amount; i++)
  163.         {
  164.             Console.Clear();
  165.             Console.WriteLine("ЗАПОЛНЕНИЕ СОТРУДНИКА №{0}", i + 1);
  166.             name = ReadTools.ReadString("Имя");
  167.             age = ReadTools.ReadByte("Возраст");
  168.             salary = ReadTools.ReadUShort("Зарплата");
  169.  
  170.             newEmployees[i] = new Employee(name, age, salary);
  171.         }
  172.  
  173.         // Замена старого массива на расширенный
  174.         employees = newEmployees;
  175.     }
  176.  
  177.     /// <summary>
  178.     /// Выводит на экран всех сотрудников
  179.     /// </summary>
  180.     public void PrintAll()
  181.     {
  182.         for (byte i = 0; i < employees.Length; i++)
  183.             Console.WriteLine("[{0}] {1}", i + 1, employees[i]);
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement