Advertisement
HTsekin

Untitled

Mar 7th, 2021
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.48 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text;
  5.  
  6. namespace DSA_InventoryManager
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             HashSet<string> itemsByType = new HashSet<string>();
  13.             Dictionary<string, Item> items = new Dictionary<string, Item>();
  14.             Dictionary<string, List<string>> typeItemNames = new Dictionary<string, List<string>>();
  15.  
  16.             StringBuilder outputMessages = new StringBuilder();
  17.             string[] commandParameters;
  18.             bool alive = true;
  19.             string type;
  20.  
  21.  
  22.  
  23.             do
  24.             {
  25.  
  26.                 commandParameters = Console.ReadLine().Split(" ");
  27.                
  28.                 switch (commandParameters[0])
  29.                 {
  30.                     case "add":
  31.                         string name = commandParameters[1];
  32.                         decimal price = decimal.Parse(commandParameters[2]);
  33.                         type = commandParameters[3];
  34.  
  35.                         if (!items.ContainsKey(name))
  36.                         {
  37.  
  38.                            
  39.                             if (typeItemNames.ContainsKey(type))
  40.                             {
  41.                                 typeItemNames[type].Add(name);
  42.                             }
  43.                             else
  44.                             {
  45.                                 typeItemNames.Add(type, new List<string>() { name });
  46.                             }
  47.  
  48.                             items.Add(name, new Item(name, price, type));
  49.                             outputMessages.AppendLine($"Ok: Item {name} added successfully");
  50.                         }
  51.                         else
  52.                         {
  53.                             outputMessages.AppendLine($"Error: Item {name} already exists");
  54.                         }
  55.                           break;
  56.                     case "filter":
  57.                         string sortBy = commandParameters[2];
  58.  
  59.  
  60.                         if (sortBy == "type")
  61.                         {
  62.                             type = commandParameters[3];
  63.                            
  64.                             if (typeItemNames.ContainsKey(type))
  65.                             {
  66.                                
  67.  
  68.                                 outputMessages.Append("Ok: ");
  69.                            
  70.                                 foreach (string item in typeItemNames[type])
  71.                                 {
  72.                                     outputMessages.Append(ItemToString(items[item]));
  73.                                 }
  74.                                 outputMessages.AppendLine();
  75.                             }
  76.                             else
  77.                             {
  78.                                 outputMessages.AppendLine($"Error: Type {type} does not exists");
  79.                             }
  80.                         }
  81.                         else if(sortBy == "price")
  82.                         {
  83.                             if(commandParameters.Length == 5)
  84.                             {
  85.                                
  86.                                 decimal pricee = decimal.Parse(commandParameters[4]);
  87.                                 string range = commandParameters[3];
  88.                                
  89.  
  90.                                 if (range == "from")
  91.                                 {
  92.                                     List<string> tmpItems = items.Where(item => item.Value.Price > pricee)
  93.                                         .OrderBy(item => item.Value.Price)
  94.                                         .Take(10)
  95.                                         .Select(item => ItemToString(item.Value)).ToList();
  96.  
  97.                                     outputMessages.Append("OK: ");
  98.                                     outputMessages.AppendLine(string.Join("", tmpItems));
  99.                                 }
  100.                                 else
  101.                                 {
  102.                                     List<string> tmpItems = items.Where(item => item.Value.Price <= pricee)
  103.                                         .OrderBy(item => item.Value.Price)
  104.                                         .Take(10)
  105.                                         .Select(item => ItemToString(item.Value)).ToList();
  106.  
  107.                                     outputMessages.Append("OK: ");
  108.                                     outputMessages.AppendLine(string.Join("", tmpItems));
  109.                                 }
  110.                                
  111.                             }
  112.                             else
  113.                             {
  114.                                 decimal fromPrice = decimal.Parse(commandParameters[4]);
  115.                                 decimal toPrice = decimal.Parse(commandParameters[6]);
  116.  
  117.                                 List<string> tmpItems = items.Where(item => item.Value.Price > fromPrice &&
  118.                                 item.Value.Price < toPrice)
  119.                                         .OrderBy(item => item.Value.Price)
  120.                                         .Take(10)
  121.                                         .Select(item => ItemToString(item.Value)).ToList();
  122.  
  123.  
  124.                                 outputMessages.Append("OK: ");
  125.                                 outputMessages.AppendLine(string.Join("", tmpItems));
  126.                             }
  127.                         }
  128.                        
  129.                         break;
  130.                     case "end":
  131.                         alive = false;
  132.                         break;
  133.                 }
  134.  
  135.             } while (alive);
  136.  
  137.  
  138.  
  139.             Console.WriteLine(outputMessages.ToString().Trim(' ').Trim(','));
  140.         }
  141.  
  142.  
  143.         public static string ItemToString(Item item)
  144.         {
  145.             return $"{item.Name}({item.Price}), ";
  146.         }
  147.     }
  148.  
  149.     public class Item
  150.     {
  151.         private string name;
  152.         private decimal price;
  153.         private string type;
  154.  
  155.         public Item(string name, decimal price, string type)
  156.         {
  157.             this.Name = name;
  158.             this.Price = price;
  159.             this.Type = type;
  160.         }
  161.  
  162.         public string Name
  163.         {
  164.             get => this.name;
  165.             set => this.name = value;
  166.         }
  167.  
  168.         public decimal Price
  169.         {
  170.             get => this.price;
  171.             set => this.price = value;
  172.         }
  173.  
  174.         public string Type
  175.         {
  176.             get => this.type;
  177.             set => this.type = value;
  178.         }
  179.  
  180.     }
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement