Advertisement
centner_dc

Exercise 3 v2

Aug 25th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.77 KB | None | 0 0
  1. ///Exercise 3
  2.  
  3. using System;
  4. using System.Threading;
  5.  
  6. namespace Delegates
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             GoalListsManager goalManager = new GoalListsManager();
  13.             goalManager.Lists = new GoalList[3] { new GoalList("Личный"), new GoalList("Рабочий"), new GoalList("Семейный") };
  14.             var tableHead = string.Empty;
  15.             while (true)
  16.             {
  17.                 Console.Clear();
  18.  
  19.                 goalManager.ViewTable();
  20.  
  21.                 Console.WriteLine("Что вы хотите сделать?\n Добавить лист\n Удалить лист\n Добавить цель");
  22.                 string readCommand = Console.ReadLine().ToLower();
  23.                 switch (readCommand)
  24.                 {
  25.                     case "добавить лист":
  26.                         goalManager.CommandAddList();
  27.                         break;
  28.                     case "удалить лист":
  29.                         goalManager.CommandRemoveList();
  30.                         break;
  31.                     case "добавить цель":
  32.                         goalManager.CommandAddGoal();
  33.                         break;
  34.                     default:
  35.                         break;
  36.                 }
  37.             }
  38.         }
  39.  
  40.         public class GoalList
  41.         {
  42.             public int Length => goals.Length;
  43.  
  44.             public string Name = string.Empty;
  45.  
  46.             public string[] goals = new string[0];
  47.  
  48.             public GoalList(string _name)
  49.             {
  50.                 Name = _name;
  51.             }
  52.  
  53.             public void AddGoal(string newGoal)
  54.             {
  55.                 //Здесь нельзя использовать Array.Resize
  56.                 string[] goalsIndividualNew = new string[Length + 1];
  57.                 for (int j = 0; j < Length; j++)
  58.                 {
  59.                     goalsIndividualNew[j] = goals[j];
  60.                 }
  61.                 goalsIndividualNew[goalsIndividualNew.Length - 1] = newGoal;
  62.                 goals = goalsIndividualNew;
  63.             }
  64.         }
  65.  
  66.         public class GoalListsManager
  67.         {
  68.             public GoalList[] Lists = new GoalList[0];
  69.  
  70.             public void ViewTable()
  71.             {
  72.                 if (Lists.Length == 0)
  73.                 {
  74.                     Console.WriteLine("Листы целей отсутствуют.");
  75.                     return;
  76.                 }
  77.  
  78.                 string tableHead = string.Empty;
  79.                 for (int i = 0; i < Lists.Length; i++)
  80.                     tableHead += (i == 0 ? "" : " | ") + Lists[i].Name;
  81.  
  82.                 string tableBody = string.Empty;
  83.                 for (int i = 0; i < GetMaxLength(Lists); i++)
  84.                 {
  85.                     for (int j = 0; j < Lists.Length; j++)
  86.                         if (i < Lists[j].Length)
  87.                             tableBody += Lists[j].goals[i] + " | ";
  88.                         else
  89.                             tableBody += "Empty | ";
  90.  
  91.                     tableBody += "\n";
  92.                 }
  93.  
  94.                 Console.WriteLine(tableHead + "\n" + tableBody);
  95.  
  96.                 //поиск длинны самого длинного списка на данный момент
  97.                 int GetMaxLength(GoalList[] lists)
  98.                 {
  99.                     int max = 0;
  100.                     for (int i = 0; i < lists.Length; i++)
  101.                         if (max < lists[i].Length)
  102.                             max = lists[i].Length;
  103.  
  104.                     return max;
  105.                 }
  106.             }
  107.  
  108.             public void CommandAddGoal()
  109.             {
  110.                 Console.WriteLine("Куда вы хотите добавить цель?");
  111.                 string listName = Console.ReadLine().ToLower(); //то что введёт пользователь переведённое в нижний регистр
  112.                 Console.WriteLine("Что это за цель?");
  113.                 string goal = Console.ReadLine();
  114.  
  115.                 int i = FindIndexListByName(listName);
  116.  
  117.                 if (i != -1)
  118.                     Lists[i].AddGoal(goal);
  119.             }
  120.  
  121.             public void CommandAddList()
  122.             {
  123.                 Console.WriteLine("Что это за лист?");
  124.                 string listName = Console.ReadLine();
  125.  
  126.                 int i = FindIndexListByName(listName.ToLower());
  127.  
  128.                 if (i == -1)
  129.                 {
  130.                     AddList(new GoalList(listName));
  131.                 }
  132.                 else
  133.                 {
  134.                     Console.WriteLine("Лист с таким именем уже существует.");
  135.                     Console.ReadLine();
  136.                 }
  137.             }
  138.  
  139.             public void CommandRemoveList()
  140.             {
  141.                 Console.WriteLine("Какой лист удалить?");
  142.                 string listName = Console.ReadLine().ToLower();
  143.  
  144.                 int i = FindIndexListByName(listName);
  145.  
  146.                 if (i != -1)
  147.                 {
  148.                     RemoveList(i);
  149.                 }
  150.                 else
  151.                 {
  152.                     Console.WriteLine("Лист с таким именем не найден.");
  153.                     Console.ReadLine();
  154.                 }
  155.             }
  156.  
  157.             private int FindIndexListByName(string listName)
  158.             {
  159.                 int i = 0;
  160.                 for (; i < Lists.Length; i++)
  161.                     if (Lists[i].Name.ToLower() == listName)
  162.                         break;
  163.  
  164.                 if (i >= Lists.Length)
  165.                     i = -1;
  166.  
  167.                 return i;
  168.             }
  169.  
  170.             public void AddList(GoalList newList)
  171.             {
  172.                 GoalList[] GoalListNew = new GoalList[Lists.Length + 1];
  173.  
  174.                 for (int i = 0; i < Lists.Length; i++)
  175.                     GoalListNew[i] = Lists[i];
  176.  
  177.                 GoalListNew[GoalListNew.Length - 1] = newList;
  178.                 Lists = GoalListNew;
  179.             }
  180.  
  181.             public void RemoveList(int index)
  182.             {
  183.                 if (index >= Lists.Length)
  184.                     return;
  185.  
  186.                 if (Lists.Length == 1)
  187.                 {
  188.                     Lists = new GoalList[0];
  189.                     return;
  190.                 }
  191.  
  192.                 GoalList[] GoalListNew = new GoalList[Lists.Length - 1];
  193.  
  194.                 int j = 0;
  195.                 for (int i = 0; i < Lists.Length; i++)
  196.                 {
  197.                     if (i != index)
  198.                     {
  199.                         GoalListNew[j] = Lists[i];
  200.                         j++;
  201.                     }
  202.                 }
  203.  
  204.                 Lists = GoalListNew;
  205.             }
  206.         }
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement