Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///Exercise 3
- using System;
- using System.Threading;
- namespace Delegates
- {
- class Program
- {
- static void Main(string[] args)
- {
- GoalListsManager goalManager = new GoalListsManager();
- goalManager.Lists = new GoalList[3] { new GoalList("Личный"), new GoalList("Рабочий"), new GoalList("Семейный") };
- var tableHead = string.Empty;
- while (true)
- {
- Console.Clear();
- goalManager.ViewTable();
- Console.WriteLine("Что вы хотите сделать?\n Добавить лист\n Удалить лист\n Добавить цель");
- string readCommand = Console.ReadLine().ToLower();
- switch (readCommand)
- {
- case "добавить лист":
- goalManager.CommandAddList();
- break;
- case "удалить лист":
- goalManager.CommandRemoveList();
- break;
- case "добавить цель":
- goalManager.CommandAddGoal();
- break;
- default:
- break;
- }
- }
- }
- public class GoalList
- {
- public int Length => goals.Length;
- public string Name = string.Empty;
- public string[] goals = new string[0];
- public GoalList(string _name)
- {
- Name = _name;
- }
- public void AddGoal(string newGoal)
- {
- //Здесь нельзя использовать Array.Resize
- string[] goalsIndividualNew = new string[Length + 1];
- for (int j = 0; j < Length; j++)
- {
- goalsIndividualNew[j] = goals[j];
- }
- goalsIndividualNew[goalsIndividualNew.Length - 1] = newGoal;
- goals = goalsIndividualNew;
- }
- }
- public class GoalListsManager
- {
- public GoalList[] Lists = new GoalList[0];
- public void ViewTable()
- {
- if (Lists.Length == 0)
- {
- Console.WriteLine("Листы целей отсутствуют.");
- return;
- }
- string tableHead = string.Empty;
- for (int i = 0; i < Lists.Length; i++)
- tableHead += (i == 0 ? "" : " | ") + Lists[i].Name;
- string tableBody = string.Empty;
- for (int i = 0; i < GetMaxLength(Lists); i++)
- {
- for (int j = 0; j < Lists.Length; j++)
- if (i < Lists[j].Length)
- tableBody += Lists[j].goals[i] + " | ";
- else
- tableBody += "Empty | ";
- tableBody += "\n";
- }
- Console.WriteLine(tableHead + "\n" + tableBody);
- //поиск длинны самого длинного списка на данный момент
- int GetMaxLength(GoalList[] lists)
- {
- int max = 0;
- for (int i = 0; i < lists.Length; i++)
- if (max < lists[i].Length)
- max = lists[i].Length;
- return max;
- }
- }
- public void CommandAddGoal()
- {
- Console.WriteLine("Куда вы хотите добавить цель?");
- string listName = Console.ReadLine().ToLower(); //то что введёт пользователь переведённое в нижний регистр
- Console.WriteLine("Что это за цель?");
- string goal = Console.ReadLine();
- int i = FindIndexListByName(listName);
- if (i != -1)
- Lists[i].AddGoal(goal);
- }
- public void CommandAddList()
- {
- Console.WriteLine("Что это за лист?");
- string listName = Console.ReadLine();
- int i = FindIndexListByName(listName.ToLower());
- if (i == -1)
- {
- AddList(new GoalList(listName));
- }
- else
- {
- Console.WriteLine("Лист с таким именем уже существует.");
- Console.ReadLine();
- }
- }
- public void CommandRemoveList()
- {
- Console.WriteLine("Какой лист удалить?");
- string listName = Console.ReadLine().ToLower();
- int i = FindIndexListByName(listName);
- if (i != -1)
- {
- RemoveList(i);
- }
- else
- {
- Console.WriteLine("Лист с таким именем не найден.");
- Console.ReadLine();
- }
- }
- private int FindIndexListByName(string listName)
- {
- int i = 0;
- for (; i < Lists.Length; i++)
- if (Lists[i].Name.ToLower() == listName)
- break;
- if (i >= Lists.Length)
- i = -1;
- return i;
- }
- public void AddList(GoalList newList)
- {
- GoalList[] GoalListNew = new GoalList[Lists.Length + 1];
- for (int i = 0; i < Lists.Length; i++)
- GoalListNew[i] = Lists[i];
- GoalListNew[GoalListNew.Length - 1] = newList;
- Lists = GoalListNew;
- }
- public void RemoveList(int index)
- {
- if (index >= Lists.Length)
- return;
- if (Lists.Length == 1)
- {
- Lists = new GoalList[0];
- return;
- }
- GoalList[] GoalListNew = new GoalList[Lists.Length - 1];
- int j = 0;
- for (int i = 0; i < Lists.Length; i++)
- {
- if (i != index)
- {
- GoalListNew[j] = Lists[i];
- j++;
- }
- }
- Lists = GoalListNew;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement