Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- static void Main()
- {
- Staff staff = new Staff();
- // Инициализация массива первой частью сотрудников
- byte amount = ReadTools.ReadByte("Сколькими сотрудниками вы хотите инициализировать массив?");
- staff.AddEmpolyees(amount);
- // Вывод первой части сотрудников
- Console.Clear();
- staff.PrintAll();
- Console.ReadLine();
- // Добавление второй части сотрудников
- amount = ReadTools.ReadByte("Сколько новых сотрудников добавить?");
- staff.AddEmpolyees(amount);
- // Вывод всех сотрудников
- Console.Clear();
- staff.PrintAll();
- // Вывод данных о зарплате
- Console.WriteLine("Ср. з/п: {0}, мин. з/п: {1}, макс. з/п: {2}", staff.AverageSalary(), staff.MinSalary(), staff.MaxSalary());
- Console.ReadLine();
- }
- }
- class ReadTools
- {
- /// <summary>
- /// Безопасно считывает значение типа byte с приглашением
- /// </summary>
- /// <param name="prompt">приглашение</param>
- /// <returns>считываемое значение</returns>
- public static byte ReadByte(string prompt)
- {
- byte rs;
- do
- Console.Write("{0}: ", prompt);
- while (!byte.TryParse(Console.ReadLine(), out rs));
- return rs;
- }
- /// <summary>
- /// Безопасно считывает значение типа ushort с приглашением
- /// </summary>
- /// <param name="prompt">приглашение</param>
- /// <returns>считываемое значение</returns>
- public static ushort ReadUShort(string prompt)
- {
- ushort rs;
- do
- Console.Write("{0}: ", prompt);
- while (!ushort.TryParse(Console.ReadLine(), out rs));
- return rs;
- }
- /// <summary>
- /// Считывает значение типа string с приглашением
- /// </summary>
- /// <param name="prompt">приглашение</param>
- /// <returns>считываемое значение</returns>
- public static string ReadString(string prompt)
- {
- Console.Write("{0}: ", prompt);
- return Console.ReadLine();
- }
- }
- class Employee
- {
- public string name { get; set; }
- public byte age { get; set; }
- public ushort salary { get; set; }
- public Employee(string name, byte age, ushort salary)
- {
- this.name = name;
- this.age = age;
- this.salary = salary;
- }
- public override string ToString()
- {
- return String.Format("СОТРУДНИК(имя: \"{0}\", возраст: {1}, зарплата: {2})", name, age, salary);
- }
- }
- class Staff
- {
- Employee[] employees = new Employee[0];
- /// <summary>
- /// Ищет максимальную зарплату сотрудников
- /// </summary>
- /// <returns>максимальная зарплата</returns>
- public ushort MaxSalary()
- {
- ushort max = ushort.MinValue;
- foreach (Employee employee in employees)
- if (employee.salary > max)
- max = employee.salary;
- return max;
- }
- /// <summary>
- /// Ищет минимальную зарплату сотрудников
- /// </summary>
- /// <returns>минимальная зарплата</returns>
- public ushort MinSalary()
- {
- ushort min = ushort.MaxValue;
- foreach (Employee employee in employees)
- if (employee.salary < min)
- min = employee.salary;
- return min;
- }
- /// <summary>
- /// Ищет среднюю зарплату сотрудников
- /// </summary>
- /// <returns>средняя зарплата (или -1 при отсутствии сотрудников)</returns>
- public double AverageSalary()
- {
- double av = 0.0;
- foreach (Employee employee in employees)
- av += employee.salary;
- return employees.Length == 0 ? -1 : av / employees.Length;
- }
- /// <summary>
- /// Запрашивает у пользователя новых сотрудников и расширяет массив сотрудников
- /// </summary>
- /// <param name="amount">количество запрашиваемых сотрудников</param>
- public void AddEmpolyees(byte amount)
- {
- byte i;
- Employee[] newEmployees = new Employee[employees.Length + amount];
- // Копирование уже имеющихся элементов
- for (i = 0; i < employees.Length; i++)
- newEmployees[i] = employees[i];
- string name;
- byte age;
- ushort salary;
- // Считывание новых сотрудников
- for (i = (byte)employees.Length; i < employees.Length + amount; i++)
- {
- Console.Clear();
- Console.WriteLine("ЗАПОЛНЕНИЕ СОТРУДНИКА №{0}", i + 1);
- name = ReadTools.ReadString("Имя");
- age = ReadTools.ReadByte("Возраст");
- salary = ReadTools.ReadUShort("Зарплата");
- newEmployees[i] = new Employee(name, age, salary);
- }
- // Замена старого массива на расширенный
- employees = newEmployees;
- }
- /// <summary>
- /// Выводит на экран всех сотрудников
- /// </summary>
- public void PrintAll()
- {
- for (byte i = 0; i < employees.Length; i++)
- Console.WriteLine("[{0}] {1}", i + 1, employees[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement