Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.48 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3.  
  4. namespace Lab_3_Var_19
  5. {
  6.     struct Price
  7.     {
  8.         private class sortProductNameDescendingHelper : IComparer
  9.         {
  10.             int IComparer.Compare(object x, object y)
  11.             {
  12.                 Price p1 = (Price) x;
  13.                 Price p2 = (Price) y;
  14.  
  15.                 return String.Compare(p2.productName, p1.productName);
  16.             }
  17.         }
  18.  
  19.         private class sortStoreNameAscendingHelper : IComparer
  20.         {
  21.             int IComparer.Compare(object x, object y)
  22.             {
  23.                 Price p1 = (Price)x;
  24.                 Price p2 = (Price)y;
  25.  
  26.                 return String.Compare(p1.storeName, p2.storeName);
  27.             }
  28.         }
  29.  
  30.         private class sortStoreNameDescendingHelper : IComparer
  31.         {
  32.             int IComparer.Compare(object x, object y)
  33.             {
  34.                 Price p1 = (Price)x;
  35.                 Price p2 = (Price)y;
  36.  
  37.                 return String.Compare(p2.storeName, p1.storeName);
  38.             }
  39.         }
  40.  
  41.         private class sortProductPriceAscendingHelper : IComparer
  42.         {
  43.             int IComparer.Compare(object x, object y)
  44.             {
  45.                 Price p1 = (Price)x;
  46.                 Price p2 = (Price)y;
  47.  
  48.                 if (p1.productPrice > p2.productPrice)
  49.                     return 1;
  50.  
  51.                 if (p1.productPrice < p2.productPrice)
  52.                     return -1;
  53.  
  54.                 else
  55.                     return 0;
  56.             }
  57.         }
  58.  
  59.         private class sortProductPriceDescendingHelper : IComparer
  60.         {
  61.             int IComparer.Compare(object x, object y)
  62.             {
  63.                 Price p1 = (Price)x;
  64.                 Price p2 = (Price)y;
  65.  
  66.                 if (p1.productPrice < p2.productPrice)
  67.                     return 1;
  68.  
  69.                 if (p1.productPrice > p2.productPrice)
  70.                     return -1;
  71.  
  72.                 else
  73.                     return 0;
  74.             }
  75.         }
  76.  
  77.         public string productName;
  78.         public string storeName;
  79.         public decimal productPrice;
  80.  
  81.         public Price((string prodName, string storeName, decimal prodPrice) tuple)
  82.         {
  83.             productName = tuple.prodName;
  84.             storeName = tuple.storeName;
  85.             productPrice = tuple.prodPrice;
  86.         }
  87.  
  88.         public override string ToString()
  89.         {
  90.             return $"Наименование товара: {productName}.\n" +
  91.                    $"Наименование магазина: {storeName}.\n" +
  92.                    $"Стоимость товара: {productPrice}.\n";
  93.         }
  94.  
  95.         int IComparable<Price>.CompareTo(Price other)
  96.         {
  97.             return String.Compare(this.productName, other.productName);
  98.         }
  99.  
  100.         public static IComparer sortProductNameDescending()
  101.         {
  102.             return (IComparer) new sortProductNameDescendingHelper();
  103.         }
  104.  
  105.         public static IComparer sortStoreNameAscending()
  106.         {
  107.             return (IComparer)new sortStoreNameAscendingHelper();
  108.         }
  109.  
  110.         public static IComparer sortStoreNameDescending()
  111.         {
  112.             return (IComparer)new sortStoreNameDescendingHelper();
  113.         }
  114.  
  115.         public static IComparer sortProductPriceAscending()
  116.         {
  117.             return (IComparer)new sortProductPriceAscendingHelper();
  118.         }
  119.  
  120.         public static IComparer sortProductPriceDescending()
  121.         {
  122.             return (IComparer)new sortProductPriceDescendingHelper();
  123.         }
  124.     }
  125.  
  126.     class Program
  127.     {
  128.         static void Main(string[] args)
  129.         {
  130.             Price[] prices = null;
  131.             bool exit = false;
  132.             string choice;
  133.             string temp = "";
  134.             string menu = "\nВыберите один из следующих пунктов меню:\n" +
  135.                           "1. Создать 8 товаров.\n" +
  136.                           "2. Вывести информацию о товарах из указанного магазина.\n" +
  137.                           "3. Вывести информацию о всех товарах.\n" +
  138.                           "4. Отсортировать товары по полю на выбор.\n" +
  139.                           "0. Выход.\n";
  140.  
  141.             string submenu = "\nВыберите один из следующих пунктов меню:\n" +
  142.                              "1. Отсортировать по наименованию товара (возрастание).\n" +
  143.                              "2. Отсортировать по наименованию товара (убывание).\n" +
  144.                              "3. Отсортировать по наименованию магазина (возрастание).\n" +
  145.                              "4. Отсортировать по наименованию магазина (убывание).\n" +
  146.                              "5. Отсортировать по стоимости товара (возрастание).\n" +
  147.                              "6. Отсортировать по стоимости товара (убывание).\n";
  148.  
  149.             while (!exit)
  150.             {
  151.                 Console.WriteLine(menu);
  152.  
  153.                 choice = Console.ReadLine();
  154.  
  155.                 Console.Clear();
  156.  
  157.                 switch (choice)
  158.                 {
  159.                     //Создать 8 товаров
  160.                     case "1":
  161.                         string prodName, storeName;
  162.                         decimal prodPrice;
  163.                         prices = new Price[8];
  164.  
  165.                         for (int i = 0; i < prices.Length; i++)
  166.                         {
  167.                             Console.WriteLine($"Товар №{i + 1}");
  168.  
  169.                             Console.Write("Введите наименование товара: ");
  170.                             prodName = Console.ReadLine();
  171.  
  172.                             Console.Write("Введите наименование магазина: ");
  173.                             storeName = Console.ReadLine();
  174.  
  175.                             while (!false)
  176.                             {
  177.                                 Console.Write("Введите стоимость товара в рублях (целое положительное число): ");
  178.                                 prodPrice = InputChecks.DecimalInput(Console.ReadLine());
  179.  
  180.                                 if (prodPrice < 0)
  181.                                     Console.WriteLine("Некорректное значение! Обратите внимание на ограничения!\n");
  182.                                 else
  183.                                     break;
  184.                             }
  185.  
  186.                             prices[i] = new Price((prodName, storeName, prodPrice));
  187.                         }
  188.  
  189.                         Array.Sort(prices, Price.sortStoreNameAscending());
  190.  
  191.                         break;
  192.  
  193.                     //Вывести информацию о товарах из указанного магазина
  194.                     case "2":
  195.                         if (prices != null)
  196.                         {
  197.                             Console.Write("Введите название магазина: ");
  198.                             choice = Console.ReadLine();
  199.                             temp = "";
  200.  
  201.                             for (int i = 0; i < prices.Length; i++)
  202.                                 if (prices[i].storeName.ToLower() == choice.ToLower())
  203.                                     temp += prices[i].ToString() + "\n";
  204.  
  205.                             if (temp != "")
  206.                                 Console.WriteLine("В этом магазине продаются следующие товары:\n\n" + temp);
  207.                             else
  208.                                 Console.WriteLine("Товаров из указанного магазина в списке нет!\n");
  209.                         }
  210.  
  211.                         else
  212.                             Console.WriteLine("Список товаров пуст!\n");
  213.  
  214.                         break;
  215.  
  216.                     //Вывести информацию о всех товарах
  217.                     case "3":
  218.                         if (prices != null)
  219.                         {
  220.                             Console.WriteLine("Текущий список товаров:\n");
  221.  
  222.                             for (int i = 0; i < prices.Length; i++)
  223.                                 Console.WriteLine("Товар №" + (i + 1) + "\n" + prices[i].ToString());
  224.                         }
  225.  
  226.                         else
  227.                             Console.WriteLine("Список товаров пуст!\n");
  228.  
  229.                         break;
  230.  
  231.                     //Сортировка по полям
  232.                     case "4":
  233.                         if (prices != null)
  234.                         {
  235.                             Console.Clear();
  236.                             Console.WriteLine(submenu);
  237.  
  238.                             choice = Console.ReadLine();
  239.  
  240.                             switch (choice)
  241.                             {
  242.                                 //Наименование товара, возрастание
  243.                                 case "1":
  244.                                     Array.Sort(prices);
  245.                                     break;
  246.  
  247.                                 //Наименование товара, убывание
  248.                                 case "2":
  249.                                     Array.Sort(prices, Price.sortProductNameDescending());
  250.                                     break;
  251.  
  252.                                 //Наименование магазина, возрастание
  253.                                 case "3":
  254.                                     Array.Sort(prices, Price.sortStoreNameAscending());
  255.                                     break;
  256.  
  257.                                 //Наименование магазина, убывание
  258.                                 case "4":
  259.                                     Array.Sort(prices, Price.sortStoreNameDescending());
  260.                                     break;
  261.  
  262.                                 //Стоимость товара, возрастание
  263.                                 case "5":
  264.                                     Array.Sort(prices, Price.sortProductPriceAscending());
  265.                                     break;
  266.  
  267.                                 //Стоимость товара, убывание
  268.                                 case "6":
  269.                                     Array.Sort(prices, Price.sortProductPriceDescending());
  270.                                     break;
  271.  
  272.                                 default:
  273.                                     Console.WriteLine("Такого пункта нет!\n");
  274.                                     break;
  275.                             }
  276.                         }
  277.  
  278.                         else
  279.                             Console.WriteLine("Список товаров пуст!\n");
  280.  
  281.                         break;
  282.  
  283.                     //Выход
  284.                     case "0":
  285.                         exit = true;
  286.                         break;
  287.  
  288.                     default:
  289.                         Console.WriteLine("Такого пункта меню нет!\n");
  290.                         break;
  291.                 }
  292.             }
  293.         }
  294.     }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement