Advertisement
Guest User

Untitled

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