using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace Prac8 { internal class Program { private static void Main() { Console.OutputEncoding = Encoding.UTF8; MyList equipmentList = new MyList(); bool isRunning = true; while (isRunning) { PrintMenu(); Console.Write("Выберите действие: "); string choice = Console.ReadLine(); Console.WriteLine(); try { switch (choice) { case "1": equipmentList.Add(ReadEquipment()); Console.WriteLine("Тренажёр добавлен в конец списка."); break; case "2": { int index = ReadInt("Введите индекс для вставки: "); equipmentList.Insert(index, ReadEquipment()); Console.WriteLine("Тренажёр вставлен по указанному индексу."); break; } case "3": { int index = ReadInt("Введите индекс изменяемого элемента: "); equipmentList[index] = ReadEquipment(); Console.WriteLine("Элемент успешно изменён."); break; } case "4": equipmentList.RemoveLast(); Console.WriteLine("Последний элемент удалён."); break; case "5": { int index = ReadInt("Введите индекс удаляемого элемента: "); equipmentList.RemoveAt(index); Console.WriteLine("Элемент удалён."); break; } case "6": Console.WriteLine($"Количество элементов: {equipmentList.Count}"); break; case "7": equipmentList.Print(); break; case "8": { int index = ReadInt("Введите индекс элемента: "); Console.WriteLine(equipmentList.Get(index)); break; } case "9": AddRangeInteractive(equipmentList); break; case "10": InsertRangeInteractive(equipmentList); break; case "11": { int firstIndex = ReadInt("Введите первый индекс: "); int secondIndex = ReadInt("Введите второй индекс: "); equipmentList.RemoveRange(firstIndex, secondIndex); Console.WriteLine("Диапазон элементов удалён."); break; } case "12": FindEquipment(equipmentList); break; case "13": { int sortMode = ReadInt("Введите 1 для сортировки по возрастанию или 0 для сортировки по убыванию: "); equipmentList.Sort(sortMode); Console.WriteLine("Список отсортирован."); break; } case "14": equipmentList.Reverse(); Console.WriteLine("Список развёрнут."); break; case "15": equipmentList.Distinct(); Console.WriteLine("Дубликаты удалены."); break; case "16": equipmentList.Clear(); Console.WriteLine("Список очищен."); break; case "17": PrintWithForeach(equipmentList); break; case "0": isRunning = false; Console.WriteLine("Программа завершена."); break; default: Console.WriteLine("Такого пункта меню нет. Люди, как обычно, выбирают что попало."); break; } } catch (Exception ex) { Console.WriteLine($"Ошибка: {ex.Message}"); } if (isRunning) { Pause(); Console.Clear(); } } } private static void PrintMenu() { Console.WriteLine("Меню работы с MyList<Тренажёр>:"); Console.WriteLine("1 - Добавить элемент в конец"); Console.WriteLine("2 - Добавить элемент по индексу"); Console.WriteLine("3 - Изменить элемент по индексу"); Console.WriteLine("4 - Удалить последний элемент"); Console.WriteLine("5 - Удалить элемент по индексу"); Console.WriteLine("6 - Получить количество элементов"); Console.WriteLine("7 - Вывести список"); Console.WriteLine("8 - Вывести элемент по индексу"); Console.WriteLine("9 - Добавить диапазон элементов в конец"); Console.WriteLine("10 - Вставить диапазон элементов по индексу"); Console.WriteLine("11 - Удалить диапазон элементов"); Console.WriteLine("12 - Найти элемент по условию"); Console.WriteLine("13 - Сортировать список"); Console.WriteLine("14 - Развернуть список"); Console.WriteLine("15 - Удалить дубликаты"); Console.WriteLine("16 - Очистить список"); Console.WriteLine("17 - Вывести список через foreach"); Console.WriteLine("0 - Выход"); Console.WriteLine(); } private static GymEquipment ReadEquipment() { Console.WriteLine("Введите данные тренажёра:"); int number = ReadInt("№: "); string name = ReadRequiredString("Название: "); string type = ReadRequiredString("Тип: "); string condition = ReadRequiredString("Состояние: "); string place = ReadRequiredString("Место: "); return new GymEquipment(number, name, type, condition, place); } private static void AddRangeInteractive(MyList equipmentList) { int count = ReadInt("Сколько элементов нужно добавить в конец: "); if (count <= 0) { Console.WriteLine("Количество должно быть больше нуля."); return; } MyList tempList = new MyList(count); for (int i = 0; i < count; i++) { Console.WriteLine($"\nЭлемент {i + 1} из {count}:"); tempList.Add(ReadEquipment()); } equipmentList.AddRange(tempList); Console.WriteLine("Диапазон элементов добавлен в конец списка."); } private static void InsertRangeInteractive(MyList equipmentList) { int index = ReadInt("Введите индекс для вставки диапазона: "); int count = ReadInt("Сколько элементов нужно вставить: "); if (count <= 0) { Console.WriteLine("Количество должно быть больше нуля."); return; } MyList tempList = new MyList(count); for (int i = 0; i < count; i++) { Console.WriteLine($"\nЭлемент {i + 1} из {count}:"); tempList.Add(ReadEquipment()); } equipmentList.InsertRange(index, tempList); Console.WriteLine("Диапазон элементов вставлен."); } private static void FindEquipment(MyList equipmentList) { if (equipmentList.Count == 0) { Console.WriteLine("Список пуст."); return; } string fragment = ReadRequiredString("Введите текст для поиска (по названию, типу, состоянию или месту): "); GymEquipment found = equipmentList.Find(item => item.Name.IndexOf(fragment, StringComparison.OrdinalIgnoreCase) >= 0 || item.Type.IndexOf(fragment, StringComparison.OrdinalIgnoreCase) >= 0 || item.Condition.IndexOf(fragment, StringComparison.OrdinalIgnoreCase) >= 0 || item.Place.IndexOf(fragment, StringComparison.OrdinalIgnoreCase) >= 0); if (found == null) { Console.WriteLine("Подходящий элемент не найден."); } else { Console.WriteLine("Найденный элемент:"); Console.WriteLine(found); } } private static void PrintWithForeach(MyList equipmentList) { if (equipmentList.Count == 0) { Console.WriteLine("Список пуст."); return; } int index = 0; foreach (GymEquipment equipment in equipmentList) { Console.WriteLine($"[{index}] {equipment}"); index++; } } private static int ReadInt(string message) { while (true) { Console.Write(message); string input = Console.ReadLine(); if (int.TryParse(input, out int value)) { return value; } Console.WriteLine("Введите целое число."); } } private static string ReadRequiredString(string message) { while (true) { Console.Write(message); string input = Console.ReadLine(); if (!string.IsNullOrWhiteSpace(input)) { return input.Trim(); } Console.WriteLine("Строка не должна быть пустой."); } } private static void Pause() { Console.WriteLine(); Console.WriteLine("Нажмите Enter, чтобы продолжить..."); Console.ReadLine(); } } }