Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- namespace Lab_3_Var_19
- {
- struct Price
- {
- private class sortProductNameDescendingHelper : IComparer
- {
- int IComparer.Compare(object x, object y)
- {
- Price p1 = (Price) x;
- Price p2 = (Price) y;
- return String.Compare(p2.productName, p1.productName);
- }
- }
- private class sortStoreNameAscendingHelper : IComparer
- {
- int IComparer.Compare(object x, object y)
- {
- Price p1 = (Price)x;
- Price p2 = (Price)y;
- return String.Compare(p1.storeName, p2.storeName);
- }
- }
- private class sortStoreNameDescendingHelper : IComparer
- {
- int IComparer.Compare(object x, object y)
- {
- Price p1 = (Price)x;
- Price p2 = (Price)y;
- return String.Compare(p2.storeName, p1.storeName);
- }
- }
- private class sortProductPriceAscendingHelper : IComparer
- {
- int IComparer.Compare(object x, object y)
- {
- Price p1 = (Price)x;
- Price p2 = (Price)y;
- if (p1.productPrice > p2.productPrice)
- return 1;
- if (p1.productPrice < p2.productPrice)
- return -1;
- else
- return 0;
- }
- }
- private class sortProductPriceDescendingHelper : IComparer
- {
- int IComparer.Compare(object x, object y)
- {
- Price p1 = (Price)x;
- Price p2 = (Price)y;
- if (p1.productPrice < p2.productPrice)
- return 1;
- if (p1.productPrice > p2.productPrice)
- return -1;
- else
- return 0;
- }
- }
- public string productName;
- public string storeName;
- public decimal productPrice;
- public Price((string prodName, string storeName, decimal prodPrice) tuple)
- {
- productName = tuple.prodName;
- storeName = tuple.storeName;
- productPrice = tuple.prodPrice;
- }
- public override string ToString()
- {
- return $"Наименование товара: {productName}.\n" +
- $"Наименование магазина: {storeName}.\n" +
- $"Стоимость товара: {productPrice}.\n";
- }
- int IComparable<Price>.CompareTo(Price other)
- {
- return String.Compare(this.productName, other.productName);
- }
- public static IComparer sortProductNameDescending()
- {
- return (IComparer) new sortProductNameDescendingHelper();
- }
- public static IComparer sortStoreNameAscending()
- {
- return (IComparer)new sortStoreNameAscendingHelper();
- }
- public static IComparer sortStoreNameDescending()
- {
- return (IComparer)new sortStoreNameDescendingHelper();
- }
- public static IComparer sortProductPriceAscending()
- {
- return (IComparer)new sortProductPriceAscendingHelper();
- }
- public static IComparer sortProductPriceDescending()
- {
- return (IComparer)new sortProductPriceDescendingHelper();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Price[] prices = null;
- bool exit = false;
- string choice;
- string temp = "";
- string menu = "\nВыберите один из следующих пунктов меню:\n" +
- "1. Создать 8 товаров.\n" +
- "2. Вывести информацию о товарах из указанного магазина.\n" +
- "3. Вывести информацию о всех товарах.\n" +
- "4. Отсортировать товары по полю на выбор.\n" +
- "0. Выход.\n";
- string submenu = "\nВыберите один из следующих пунктов меню:\n" +
- "1. Отсортировать по наименованию товара (возрастание).\n" +
- "2. Отсортировать по наименованию товара (убывание).\n" +
- "3. Отсортировать по наименованию магазина (возрастание).\n" +
- "4. Отсортировать по наименованию магазина (убывание).\n" +
- "5. Отсортировать по стоимости товара (возрастание).\n" +
- "6. Отсортировать по стоимости товара (убывание).\n";
- while (!exit)
- {
- Console.WriteLine(menu);
- choice = Console.ReadLine();
- Console.Clear();
- switch (choice)
- {
- //Создать 8 товаров
- case "1":
- string prodName, storeName;
- decimal prodPrice;
- prices = new Price[8];
- for (int i = 0; i < prices.Length; i++)
- {
- Console.WriteLine($"Товар №{i + 1}");
- Console.Write("Введите наименование товара: ");
- prodName = Console.ReadLine();
- Console.Write("Введите наименование магазина: ");
- storeName = Console.ReadLine();
- while (!false)
- {
- Console.Write("Введите стоимость товара в рублях (целое положительное число): ");
- prodPrice = InputChecks.DecimalInput(Console.ReadLine());
- if (prodPrice < 0)
- Console.WriteLine("Некорректное значение! Обратите внимание на ограничения!\n");
- else
- break;
- }
- prices[i] = new Price((prodName, storeName, prodPrice));
- }
- Array.Sort(prices, Price.sortStoreNameAscending());
- break;
- //Вывести информацию о товарах из указанного магазина
- case "2":
- if (prices != null)
- {
- Console.Write("Введите название магазина: ");
- choice = Console.ReadLine();
- temp = "";
- for (int i = 0; i < prices.Length; i++)
- if (prices[i].storeName.ToLower() == choice.ToLower())
- temp += prices[i].ToString() + "\n";
- if (temp != "")
- Console.WriteLine("В этом магазине продаются следующие товары:\n\n" + temp);
- else
- Console.WriteLine("Товаров из указанного магазина в списке нет!\n");
- }
- else
- Console.WriteLine("Список товаров пуст!\n");
- break;
- //Вывести информацию о всех товарах
- case "3":
- if (prices != null)
- {
- Console.WriteLine("Текущий список товаров:\n");
- for (int i = 0; i < prices.Length; i++)
- Console.WriteLine("Товар №" + (i + 1) + "\n" + prices[i].ToString());
- }
- else
- Console.WriteLine("Список товаров пуст!\n");
- break;
- //Сортировка по полям
- case "4":
- if (prices != null)
- {
- Console.Clear();
- Console.WriteLine(submenu);
- choice = Console.ReadLine();
- switch (choice)
- {
- //Наименование товара, возрастание
- case "1":
- Array.Sort(prices);
- break;
- //Наименование товара, убывание
- case "2":
- Array.Sort(prices, Price.sortProductNameDescending());
- break;
- //Наименование магазина, возрастание
- case "3":
- Array.Sort(prices, Price.sortStoreNameAscending());
- break;
- //Наименование магазина, убывание
- case "4":
- Array.Sort(prices, Price.sortStoreNameDescending());
- break;
- //Стоимость товара, возрастание
- case "5":
- Array.Sort(prices, Price.sortProductPriceAscending());
- break;
- //Стоимость товара, убывание
- case "6":
- Array.Sort(prices, Price.sortProductPriceDescending());
- break;
- default:
- Console.WriteLine("Такого пункта нет!\n");
- break;
- }
- }
- else
- Console.WriteLine("Список товаров пуст!\n");
- break;
- //Выход
- case "0":
- exit = true;
- break;
- default:
- Console.WriteLine("Такого пункта меню нет!\n");
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement