Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace App1
- {
- class Program
- {
- #region Classes
- public struct Department
- {
- public string name;
- public Boss boss;
- public List<Employee> employees;
- public Department(string name)
- {
- this.name = name;
- boss = new Boss("", 0, 0, -1);
- employees = new List<Employee>();
- }
- }
- public struct Boss
- {
- public string name;
- public int age;
- public long salary;
- public int departmentIndex;
- public Boss(string name, int age, long salary, int departIndex)
- {
- this.name = name;
- this.age = age;
- this.salary = salary;
- this.departmentIndex = departIndex;
- if (departmentIndex >= 0)
- {
- Department d = departments[departIndex];
- d.boss = this;
- departments[departIndex] = d;
- }
- }
- }
- public struct Employee
- {
- public string name;
- public int age;
- public long salary;
- public int departmentIndex;
- public string bossName;
- public Employee(string name, int age, long salary, int departIndex)
- {
- this.name = name;
- this.age = age;
- this.salary = salary;
- departmentIndex = departIndex;
- bossName = departments[departIndex].boss.name;
- Department d = departments[departIndex];
- d.employees.Add(this);
- departments[departIndex] = d;
- }
- }
- #endregion
- public static List<Department> departments = new List<Department>();
- public static string GetLine(string x)
- {
- Console.Write($"{x}: ");
- return Console.ReadLine();
- }
- public static void ChooseObjectToCreate()
- {
- int type = 1;
- while (type != 0)
- {
- Console.WriteLine("Что вы хотите создать? (введите выбранный пункт) \n 1. Отдел \n 2. Начальника \n 3. Работника \n 4. Вывод списка отделов \n Введите 0, если хотите выйти из программы.");
- Console.Write("Enter>");
- type = Convert.ToInt32(Console.ReadLine());
- switch (type)
- {
- case 1:
- Console.WriteLine("Создание отдела!");
- CreateDepartment();
- break;
- case 2:
- Console.WriteLine("Создание начальника!");
- CreateBoss();
- break;
- case 3:
- Console.WriteLine("Создание работника!");
- CreateEmployee();
- break;
- case 4:
- PrintDepartmentsList();
- break;
- }
- }
- }
- static void Main(string[] args)
- {
- ChooseObjectToCreate();
- }
- #region Create methods
- public static void CreateDepartment()
- {
- string name = GetLine("Название отдела");
- Console.WriteLine(" ");
- departments.Add(new Department(name));
- }
- public static int GetDepartmentForBoss()
- {
- Console.WriteLine("Перед тем, как создать босса, необходимо выбрать отдел, в котором он будет. Список отделов без босса: ");
- for (int i = 0; i < departments.Count; i++)
- {
- if (departments[i].boss.name == "")
- {
- Console.WriteLine($"{i + 1}. {departments[i].name} ");
- }
- }
- Console.WriteLine("\nВведите номер отдела, в который вы ходите определить босса. Введите 0, если хотите создать новый отдел.");
- int departNumber = -1;
- while (true)
- {
- departNumber = Convert.ToInt32(Console.ReadLine());
- if (departNumber == 0)
- {
- //NEED TO CHECK
- CreateDepartment();
- departNumber = departments.Count;
- break;
- }
- else if (departNumber > 0 && departNumber <= departments.Count)
- {
- if (departments[departNumber - 1].boss.name != "")
- {
- Console.WriteLine("Данный отдел уже имеет босса. Пожалуйста, выберите другой отдел.\n");
- }
- else break;
- }
- else
- {
- Console.WriteLine("Пожалуйста, повторите ввод.\n");
- }
- }
- return departNumber - 1;
- }
- public static int GetDepartmentForBoss2()
- {
- int departNumber = -1;
- {
- departNumber = Convert.ToInt32(Console.ReadLine());
- if (departNumber == 0)
- {
- CreateDepartment();
- departNumber = departments.Count;
- }
- }
- return departNumber - 1;
- }
- public static Boss CreateBoss()
- {
- int departIndex = GetDepartmentForBoss();
- string name = GetLine("Имя босса");
- int age = Convert.ToInt32(GetLine("Возраст босса"));
- long salary = Convert.ToInt64(GetLine("Зарплата босса"));
- Boss boss = new Boss(name, age, salary, departIndex);
- Console.WriteLine($"Отдел босса: {departments[departIndex].name}");
- Console.WriteLine(" ");
- return boss;
- }
- public static Boss CreateBoss2()
- {
- int departIndex = GetDepartmentForBoss2();
- string name = GetLine("Имя босса");
- int age = Convert.ToInt32(GetLine("Возраст босса"));
- long salary = Convert.ToInt64(GetLine("Зарплата босса"));
- Boss boss = new Boss(name, age, salary, departIndex-1);
- Console.WriteLine($"Отдел босса: {departments[departIndex-1].name}");
- Console.WriteLine(" ");
- return boss;
- }
- public static int CreateBossForEmployee()
- {
- Console.WriteLine("Прежде чем создать работника, необходимо выбрать отдел с начальником. Список таких отделов: ");
- for (int i = 0; i < departments.Count; i++)
- {
- if (departments[i].boss.name != "")
- {
- Console.WriteLine($"{i + 1}. {departments[i].name} ");
- }
- }
- Console.WriteLine("\nВведите номер выбранного отдела. Если хотите создать начальника для отдела - введите 0.");
- int departmentIndex = -1;
- while (true)
- {
- departmentIndex = Convert.ToInt32(Console.ReadLine());
- if (departmentIndex == 0)
- {
- Boss boss = CreateBoss();
- departmentIndex = boss.departmentIndex + 1;
- }
- else if (departmentIndex > 0 && departmentIndex <= departments.Count)
- {
- if (departments[departmentIndex - 1].boss.name == "")
- {
- Console.WriteLine("Данный отдел не имеет босса. Пожалуйста, выберите другой отдел или создайте нового босса.\n");
- }
- else break;
- }
- else
- {
- Console.WriteLine("Пожалуйста, повторите ввод.\n");
- }
- }
- return departmentIndex - 1;
- }
- public static void CreateEmployee()
- {
- int departIndex = CreateBossForEmployee();
- string name = GetLine("Имя работника");
- int age = Convert.ToInt32(GetLine("Возраст работника"));
- long salary = Convert.ToInt64(GetLine("Зарплата работника"));
- Employee employee = new Employee(name, age, salary, departIndex);
- Console.WriteLine($"Отдел работника: {departments[departIndex].name}");
- Console.WriteLine($"Босс работника: {employee.bossName}");
- Console.WriteLine(" ");
- }
- public static void PrintDepartmentsList()
- {
- foreach (Department d in departments)
- {
- Console.WriteLine($"name: {d.name}");
- if (d.boss.name != "")
- {
- Console.WriteLine("\nboss:");
- Console.WriteLine($" name: {d.boss.name}");
- Console.WriteLine($" age: {d.boss.age}");
- Console.WriteLine($" salary: {d.boss.salary}");
- }
- else Console.WriteLine("boss: null");
- Console.WriteLine($"\nemployees: {d.employees.Count}");
- for (int i = 0; i < d.employees.Count; i++)
- {
- Employee e = d.employees[i];
- Console.WriteLine($" Employee {i + 1}:");
- Console.WriteLine($" name: {e.name}");
- Console.WriteLine($" age: {e.age}");
- Console.WriteLine($" salary: {e.salary}\n");
- }
- Console.WriteLine("");
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement