Advertisement
gosharski

Inventory Manager

Mar 8th, 2021
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Inventory_Manager
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string input = Console.ReadLine();
  13.  
  14. Dictionary<string, List<Item>> items = new Dictionary<string, List<Item>>();
  15.  
  16. var set = new HashSet<Item>();
  17.  
  18. StringBuilder str = new StringBuilder();
  19.  
  20. while (input != "end")
  21. {
  22. string[] parameters = input.Split(' ');
  23. if (parameters[0] == "add")
  24. {
  25. var item = new Item(parameters[1], double.Parse(parameters[2]), parameters[3]);
  26. if (!set.Add(item))
  27. {
  28. str.AppendLine($"Error: Item {parameters[1]} already exists");
  29.  
  30. input = Console.ReadLine();
  31. continue;
  32. }
  33. else
  34. {
  35. // add jeans 10.99 clothes
  36. // add pants 10.99 clothes
  37. if (items.ContainsKey(parameters[3]))
  38. {
  39. items[parameters[3]].Add(item);
  40. }
  41. else
  42. {
  43. items.Add(parameters[3], new List<Item> { item });
  44. }
  45.  
  46. str.AppendLine($"Ok: Item {parameters[1]} added successfully");
  47.  
  48. input = Console.ReadLine();
  49. continue;
  50. }
  51. }
  52.  
  53. if (parameters[0] == "filter")
  54. {
  55. var criteria = parameters[2];
  56.  
  57. if (criteria == "type")
  58. {
  59. //filter by type clothes
  60. if (!items.ContainsKey(parameters[3]))
  61. {
  62. str.AppendLine($"Error: Type {parameters[3]} does not exists");
  63.  
  64. input = Console.ReadLine();
  65. continue;
  66. }
  67.  
  68. var msg = $"Ok: {string.Join(", ", items[parameters[3]].OrderBy(p => p.Price).ThenBy(p => p.Name)/*.ThenBy(p => p.Type)*/.Take(10))}";
  69. str.AppendLine(msg);
  70.  
  71. input = Console.ReadLine();
  72. continue;
  73. }
  74. else if (criteria == "price")
  75. {
  76. if (parameters[3] == "from")
  77. {
  78. if (parameters.Length > 5)
  79. {
  80. str.AppendLine($"Ok: {string.Join(", ", set.Where(p => p.Price > double.Parse(parameters[4]) && p.Price < double.Parse(parameters[6])).OrderBy(p => p.Price).ThenBy(p => p.Name).ThenBy(p => p.Type).Take(10)/*.Select(p => p.ToString())*/)}");
  81.  
  82. input = Console.ReadLine();
  83. continue;
  84.  
  85. }
  86.  
  87. str.AppendLine($"Ok: {string.Join(", ", set.Where(p => p.Price > double.Parse(parameters[4])).OrderBy(p => p.Price).ThenBy(p => p.Name).ThenBy(p => p.Type).Take(10)/*.Select(p => p.ToString())*/)}");
  88.  
  89. input = Console.ReadLine();
  90. continue;
  91. }
  92. else if (parameters[3] == "to")
  93. {
  94. str.AppendLine($"Ok: {string.Join(", ", set.Where(p => p.Price < double.Parse(parameters[4])).OrderBy(p => p.Price).ThenBy(p => p.Name).ThenBy(p => p.Type).Take(10)/*.Select(p => p.ToString())*/)}");
  95.  
  96. input = Console.ReadLine();
  97. continue;
  98. }
  99. }
  100. }
  101. }
  102.  
  103. Console.WriteLine(str);
  104. }
  105. }
  106.  
  107. public class Item
  108. {
  109. double price;
  110. string name;
  111. string type;
  112. public Item(string name, double price, string type)
  113. {
  114. this.Name = name;
  115. this.Price = price;
  116. this.Type = type;
  117. }
  118.  
  119. public double Price { get => this.price; set => this.price = value; }
  120. public string Name { get => this.name; set => this.name = value; }
  121. public string Type { get => this.type; set => this.type = value; }
  122. public override string ToString()
  123. {
  124. return $"{this.Name}({this.price})";
  125. }
  126. public override bool Equals(object obj)
  127. {
  128. if (obj == null || !this.GetType().Equals(obj.GetType()))
  129. {
  130. return false;
  131. }
  132.  
  133. var comp = obj as Item;
  134.  
  135. return this.Name.Equals(comp.Name);
  136. }
  137. public override int GetHashCode()
  138. {
  139. return Name.GetHashCode();
  140. }
  141. }
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement