Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace App1
  5. {
  6.     class Program
  7.     {
  8.         #region Classes
  9.  
  10.         public struct Department
  11.         {
  12.             public string name;
  13.             public Boss boss;
  14.             public List<Employee> employees;
  15.  
  16.             public Department(string name)
  17.             {
  18.                 this.name = name;
  19.                 boss = new Boss("", 0, 0, -1);
  20.                 employees = new List<Employee>();
  21.             }
  22.         }
  23.  
  24.         public struct Boss
  25.         {
  26.             public string name;
  27.             public int age;
  28.             public long salary;
  29.             public int departmentIndex;
  30.  
  31.             public Boss(string name, int age, long salary, int departIndex)
  32.             {
  33.                 this.name = name;
  34.                 this.age = age;
  35.                 this.salary = salary;
  36.  
  37.                 this.departmentIndex = departIndex;
  38.                 if (departmentIndex >= 0)
  39.                 {
  40.                     Department d = departments[departIndex];
  41.                     d.boss = this;
  42.                     departments[departIndex] = d;
  43.                 }
  44.             }
  45.         }
  46.  
  47.         public struct Employee
  48.         {
  49.             public string name;
  50.             public int age;
  51.             public long salary;
  52.             public int departmentIndex;
  53.             public string bossName;
  54.  
  55.  
  56.             public Employee(string name, int age, long salary, int departIndex)
  57.             {
  58.                 this.name = name;
  59.                 this.age = age;
  60.                 this.salary = salary;
  61.  
  62.                 departmentIndex = departIndex;
  63.                 bossName = departments[departIndex].boss.name;
  64.  
  65.                 Department d = departments[departIndex];
  66.                 d.employees.Add(this);
  67.                 departments[departIndex] = d;
  68.             }
  69.         }
  70.  
  71.  
  72.         #endregion
  73.  
  74.         public static List<Department> departments = new List<Department>();
  75.  
  76.  
  77.         public static string GetLine(string x)
  78.         {
  79.             Console.Write($"{x}: ");
  80.             return Console.ReadLine();
  81.         }
  82.  
  83.         public static void ChooseObjectToCreate()
  84.         {
  85.             int type = 1;
  86.             while (type != 0)
  87.             {
  88.                 Console.WriteLine("Что вы хотите создать? (введите выбранный пункт) \n 1. Отдел \n 2. Начальника \n 3. Работника \n 4. Вывод списка отделов \n Введите 0, если хотите выйти из программы.");
  89.                 Console.Write("Enter>");
  90.                 type = Convert.ToInt32(Console.ReadLine());
  91.  
  92.                 switch (type)
  93.                 {
  94.                     case 1:
  95.                         Console.WriteLine("Создание отдела!");
  96.                         CreateDepartment();
  97.                         break;
  98.  
  99.                     case 2:
  100.                         Console.WriteLine("Создание начальника!");
  101.                         CreateBoss();
  102.                         break;
  103.  
  104.                     case 3:
  105.                         Console.WriteLine("Создание работника!");
  106.                         CreateEmployee();
  107.                         break;
  108.                     case 4:
  109.                         PrintDepartmentsList();
  110.                         break;
  111.                 }
  112.             }
  113.         }
  114.  
  115.         static void Main(string[] args)
  116.         {
  117.             ChooseObjectToCreate();
  118.         }
  119.  
  120.  
  121.         #region Create methods
  122.  
  123.         public static void CreateDepartment()
  124.         {
  125.             string name = GetLine("Название отдела");
  126.             Console.WriteLine(" ");
  127.  
  128.             departments.Add(new Department(name));
  129.         }
  130.  
  131.         public static int GetDepartmentForBoss()
  132.         {
  133.             Console.WriteLine("Перед тем, как создать босса, необходимо выбрать отдел, в котором он будет. Список отделов без босса: ");
  134.  
  135.             for (int i = 0; i < departments.Count; i++)
  136.             {
  137.                 if (departments[i].boss.name == "")
  138.                 {
  139.                     Console.WriteLine($"{i + 1}. {departments[i].name} ");
  140.                 }
  141.             }
  142.             Console.WriteLine("\nВведите номер отдела, в который вы ходите определить босса. Введите 0, если хотите создать новый отдел.");
  143.  
  144.             int departNumber = -1;
  145.             while (true)
  146.             {
  147.                 departNumber = Convert.ToInt32(Console.ReadLine());
  148.  
  149.                 if (departNumber == 0)
  150.                 {
  151.                     //NEED TO CHECK
  152.                     CreateDepartment();
  153.                     departNumber = departments.Count;
  154.                     break;
  155.                 }
  156.                 else if (departNumber > 0 && departNumber <= departments.Count)
  157.                 {
  158.                     if (departments[departNumber - 1].boss.name != "")
  159.                     {
  160.                         Console.WriteLine("Данный отдел уже имеет босса. Пожалуйста, выберите другой отдел.\n");
  161.                     }
  162.                     else break;
  163.                 }
  164.                 else
  165.                 {
  166.                     Console.WriteLine("Пожалуйста, повторите ввод.\n");
  167.                 }
  168.             }
  169.  
  170.             return departNumber - 1;
  171.         }
  172.  
  173.  
  174.  
  175.  
  176.         public static int GetDepartmentForBoss2()
  177.         {
  178.  
  179.  
  180.             int departNumber = -1;
  181.  
  182.             {
  183.                 departNumber = Convert.ToInt32(Console.ReadLine());
  184.  
  185.                 if (departNumber == 0)
  186.                 {
  187.  
  188.                     CreateDepartment();
  189.                     departNumber = departments.Count;
  190.  
  191.                 }
  192.  
  193.             }
  194.  
  195.             return departNumber - 1;
  196.         }
  197.  
  198.  
  199.  
  200.  
  201.         public static Boss CreateBoss()
  202.         {
  203.             int departIndex = GetDepartmentForBoss();
  204.             string name = GetLine("Имя босса");
  205.             int age = Convert.ToInt32(GetLine("Возраст босса"));
  206.             long salary = Convert.ToInt64(GetLine("Зарплата босса"));
  207.  
  208.             Boss boss = new Boss(name, age, salary, departIndex);
  209.  
  210.             Console.WriteLine($"Отдел босса: {departments[departIndex].name}");
  211.             Console.WriteLine(" ");
  212.  
  213.             return boss;
  214.         }
  215.  
  216.  
  217.  
  218.         public static Boss CreateBoss2()
  219.         {
  220.             int departIndex = GetDepartmentForBoss2();
  221.             string name = GetLine("Имя босса");
  222.             int age = Convert.ToInt32(GetLine("Возраст босса"));
  223.             long salary = Convert.ToInt64(GetLine("Зарплата босса"));
  224.  
  225.             Boss boss = new Boss(name, age, salary, departIndex - 1);
  226.  
  227.             Console.WriteLine($"Отдел босса: {departments[departIndex - 1].name}");
  228.             Console.WriteLine(" ");
  229.  
  230.             return boss;
  231.         }
  232.  
  233.  
  234.  
  235.  
  236.         public static int CreateBossForEmployee()
  237.         {
  238.             Console.WriteLine("Прежде чем создать работника, необходимо выбрать отдел с начальником. Список таких отделов: ");
  239.  
  240.             for (int i = 0; i < departments.Count; i++)
  241.             {
  242.                 if (departments[i].boss.name != "")
  243.                 {
  244.                     Console.WriteLine($"{i + 1}. {departments[i].name} ");
  245.                 }
  246.             }
  247.  
  248.             Console.WriteLine("\nВведите номер выбранного отдела. Если хотите создать начальника для отдела - введите 0.");
  249.  
  250.             int departmentIndex = -1;
  251.             while (true)
  252.             {
  253.                 departmentIndex = Convert.ToInt32(Console.ReadLine());
  254.  
  255.                 if (departmentIndex == 0)
  256.                 {
  257.                     Boss boss = CreateBoss();
  258.                     departmentIndex = boss.departmentIndex + 1;
  259.                 }
  260.                 else if (departmentIndex > 0 && departmentIndex <= departments.Count)
  261.                 {
  262.                     if (departments[departmentIndex - 1].boss.name == "")
  263.                     {
  264.                         Console.WriteLine("Данный отдел не имеет босса. Пожалуйста, выберите другой отдел или создайте нового босса.\n");
  265.                     }
  266.                     else break;
  267.                 }
  268.                 else
  269.                 {
  270.                     Console.WriteLine("Пожалуйста, повторите ввод.\n");
  271.                 }
  272.             }
  273.  
  274.             return departmentIndex - 1;
  275.         }
  276.  
  277.         public static void CreateEmployee()
  278.         {
  279.             int departIndex = CreateBossForEmployee();
  280.  
  281.             string name = GetLine("Имя работника");
  282.             int age = Convert.ToInt32(GetLine("Возраст работника"));
  283.             long salary = Convert.ToInt64(GetLine("Зарплата работника"));
  284.  
  285.             Employee employee = new Employee(name, age, salary, departIndex);
  286.  
  287.             Console.WriteLine($"Отдел работника: {departments[departIndex].name}");
  288.             Console.WriteLine($"Босс работника: {employee.bossName}");
  289.             Console.WriteLine(" ");
  290.         }
  291.  
  292.         public static void PrintDepartmentsList()
  293.         {
  294.             foreach (Department d in departments)
  295.             {
  296.                 Console.WriteLine($"name: {d.name}");
  297.  
  298.                 if (d.boss.name != "")
  299.                 {
  300.                     Console.WriteLine("\nboss:");
  301.                     Console.WriteLine($"    name: {d.boss.name}");
  302.                     Console.WriteLine($"    age: {d.boss.age}");
  303.                     Console.WriteLine($"    salary: {d.boss.salary}");
  304.                 }
  305.                 else Console.WriteLine("boss: null");
  306.  
  307.                 Console.WriteLine($"\nemployees: {d.employees.Count}");
  308.  
  309.                 for (int i = 0; i < d.employees.Count; i++)
  310.                 {
  311.                     Employee e = d.employees[i];
  312.                     Console.WriteLine($"    Employee {i + 1}:");
  313.                     Console.WriteLine($"        name: {e.name}");
  314.                     Console.WriteLine($"        age: {e.age}");
  315.                     Console.WriteLine($"        salary: {e.salary}\n");
  316.                 }
  317.  
  318.                 Console.WriteLine("");
  319.             }
  320.         }
  321.  
  322.         #endregion
  323.     }
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement