Advertisement
b_gandurov

Untitled

May 23rd, 2024
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. class Program
  8. {
  9.     static void Main()
  10.     {
  11.         var items = new SortedSet<Item>();
  12.         var itemsByName = new Dictionary<string, Item>();
  13.         var itemsByType = new Dictionary<string, SortedSet<Item>>();
  14.         string input = "";
  15.         StringBuilder result = new StringBuilder();
  16.  
  17.         while (input != "end")
  18.         {
  19.             input = Console.ReadLine();
  20.  
  21.             var inputElements = input.Split();
  22.             var command = inputElements[0];
  23.  
  24.             switch (command)
  25.             {
  26.                 case "add":
  27.                     string name = inputElements[1];
  28.                     double price = double.Parse(inputElements[2], CultureInfo.InvariantCulture);
  29.                     string itemType = inputElements[3];
  30.                     if (itemsByName.ContainsKey(name))
  31.                     {
  32.                         result.AppendLine($"Error: Item {name} already exists");
  33.                     }
  34.                     else
  35.                     {
  36.                         var item = new Item(name, price, itemType);
  37.                         items.Add(item);
  38.                         itemsByName[name] = item;
  39.  
  40.                         if (!itemsByType.ContainsKey(itemType))
  41.                         {
  42.                             itemsByType[itemType] = new SortedSet<Item>();
  43.                         }
  44.                         itemsByType[itemType].Add(item);
  45.  
  46.                         result.AppendLine($"Ok: Item {name} added successfully");
  47.                     }
  48.                     break;
  49.  
  50.                 case "filter":
  51.                     var filterType = inputElements[2];
  52.                     if (filterType == "type")
  53.                     {
  54.                         FilterByType(inputElements[3], itemsByType, result);
  55.                     }
  56.                     else if (filterType == "price")
  57.                     {
  58.                         if (inputElements.Length == 7)
  59.                         {
  60.                             double minPrice = double.Parse(inputElements[4], CultureInfo.InvariantCulture);
  61.                             double maxPrice = double.Parse(inputElements[6], CultureInfo.InvariantCulture);
  62.                             FilterByPriceRange(minPrice, maxPrice, items, result);
  63.                         }
  64.                         else if (inputElements[3] == "from")
  65.                         {
  66.                             double minPrice = double.Parse(inputElements[4], CultureInfo.InvariantCulture);
  67.                             FilterByPriceFrom(minPrice, items, result);
  68.                         }
  69.                         else if (inputElements[3] == "to")
  70.                         {
  71.                             double maxPrice = double.Parse(inputElements[4], CultureInfo.InvariantCulture);
  72.                             FilterByPriceTo(maxPrice, items, result);
  73.                         }
  74.                     }
  75.                     break;
  76.             }
  77.         }
  78.         Console.WriteLine(result.ToString());
  79.     }
  80.  
  81.     static void FilterByType(string itemType, Dictionary<string, SortedSet<Item>> itemsByType,StringBuilder result)
  82.     {
  83.         if (!itemsByType.ContainsKey(itemType))
  84.         {
  85.             result.AppendLine($"Error: Type {itemType} does not exist");
  86.             return;
  87.         }
  88.  
  89.         var filteredItems = itemsByType[itemType]
  90.             .Take(10)
  91.             .Select(item => $"{item.Name}({item.Price.ToString("F2", CultureInfo.InvariantCulture)})");
  92.  
  93.         PrintFilteredItems(filteredItems, result);
  94.     }
  95.  
  96.     static void FilterByPriceRange(double minPrice, double maxPrice, SortedSet<Item> items,StringBuilder result)
  97.     {
  98.         var filteredItems = items
  99.             .Where(item => item.Price >= minPrice && item.Price <= maxPrice)
  100.             .Take(10)
  101.             .Select(item => $"{item.Name}({item.Price.ToString("F2", CultureInfo.InvariantCulture)})");
  102.  
  103.         PrintFilteredItems(filteredItems, result);
  104.     }
  105.  
  106.     static void FilterByPriceFrom(double minPrice, SortedSet<Item> items,StringBuilder result)
  107.     {
  108.         var filteredItems = items
  109.             .Where(item => item.Price >= minPrice)
  110.             .Take(10)
  111.             .Select(item => $"{item.Name}({item.Price.ToString("F2", CultureInfo.InvariantCulture)})");
  112.  
  113.         PrintFilteredItems(filteredItems, result);
  114.     }
  115.  
  116.     static void FilterByPriceTo(double maxPrice, SortedSet<Item> items,StringBuilder result)
  117.     {
  118.         var filteredItems = items
  119.             .Where(item => item.Price <= maxPrice)
  120.             .Take(10)
  121.             .Select(item => $"{item.Name}({item.Price.ToString("F2", CultureInfo.InvariantCulture)})");
  122.  
  123.         PrintFilteredItems(filteredItems, result);
  124.     }
  125.  
  126.     static void PrintFilteredItems(IEnumerable<string> filteredItems,StringBuilder result)
  127.     {
  128.         if (filteredItems.Any())
  129.         {
  130.             result.AppendLine($"Ok: {string.Join(", ", filteredItems)}");
  131.         }
  132.         else
  133.         {
  134.             result.AppendLine("Ok: ");
  135.         }
  136.     }
  137.  
  138.     class Item : IComparable<Item>
  139.     {
  140.         public string Name { get; }
  141.         public double Price { get; }
  142.         public string Type { get; }
  143.  
  144.         public Item(string name, double price, string type)
  145.         {
  146.             Name = name;
  147.             Price = price;
  148.             Type = type;
  149.         }
  150.  
  151.         public int CompareTo(Item other)
  152.         {
  153.             int priceComparison = Price.CompareTo(other.Price);
  154.             if (priceComparison != 0) return priceComparison;
  155.  
  156.             if (Name != other.Name) return string.Compare(Name, other.Name, StringComparison.Ordinal);
  157.  
  158.             return string.Compare(Type, other.Type, StringComparison.Ordinal);
  159.         }
  160.  
  161.         public override bool Equals(object obj)
  162.         {
  163.             if (obj is Item other)
  164.             {
  165.                 return Name == other.Name && Price == other.Price && Type == other.Type;
  166.             }
  167.             return false;
  168.         }
  169.  
  170.         public override int GetHashCode()
  171.         {
  172.             return Tuple.Create(Name, Price, Type).GetHashCode();
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement