Advertisement
kurec

Untitled

May 12th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace OnlineStore
  8. {
  9. class Program
  10. {
  11. static Dictionary<string, Product> products = new Dictionary<string, Product>();
  12. static Dictionary<int, Order> orders = new Dictionary<int, Order>();
  13. static void Main(string[] args)
  14. {
  15. string command;
  16.  
  17. while ((command = Console.ReadLine()) != "End")
  18. {
  19. var commandArgs = command.Split(' ').ToArray();
  20.  
  21. switch (commandArgs[0])
  22. {
  23. case "CreateProduct":
  24. AddProducts(commandArgs.Skip(1).ToArray());
  25. break;
  26. case "CreateOrder":
  27. CreateOrder(commandArgs.Skip(1).ToArray());
  28. break;
  29. case "AddToOrder":
  30. AddToOrder(commandArgs.Skip(1).ToArray());
  31. break;
  32. case "GetOrderTotalPrice":
  33. // GetOrderTotalPrice(commandArgs.Skip(1).ToArray());
  34. break;
  35. case "GetOrderDiscountedProductsPrice":
  36. // GetOrderDiscountedProductsPrice(commandArgs.Skip(1).ToArray());
  37. break;
  38. case "GetOrderDiscountedProductsCount":
  39. // GetDiscountedProductsCount(commandArgs.Skip(1).ToArray());
  40. break;
  41. case "GetTotalOrdersCount":
  42. GetTotalOrdersCount();
  43. break;
  44. case "PrintProductInfoByName":
  45. PrintProductInfoByName(commandArgs.Skip(1).ToArray());
  46.  
  47. break;
  48. case "PrintOrderByNumber":
  49. PrintOrderByNumber(commandArgs.Skip(1).ToArray());
  50. break;
  51. default:
  52. Console.WriteLine("Invalid command!");
  53. break;
  54. }
  55.  
  56. }
  57. }
  58. private static void PrintProductInfoByName(string[] name)
  59. {
  60. string productName = name[0];
  61.  
  62. if (!products.ContainsKey(productName))
  63. {
  64. Console.WriteLine($"Invalid product {productName}");
  65. return;
  66. }
  67.  
  68. Console.WriteLine(products[productName].ToString());
  69. }
  70. private static void AddToOrder(string[] orderInfo)
  71. {
  72. int orderNumber = int.Parse(orderInfo[0]);
  73. if (!orders.ContainsKey(orderNumber))
  74. {
  75. Console.WriteLine("Non existing order!");
  76.  
  77. return;
  78. }
  79. Order order = orders[orderNumber];
  80.  
  81. orderInfo.Skip(1).ToList().ForEach(p =>
  82. {
  83. if (!products.ContainsKey(p))
  84. {
  85. Console.WriteLine($"Invalid product {p}");
  86. }
  87. else
  88. {
  89. order.AddProduct(products[p]);
  90. }
  91. });
  92. }
  93. private static void AddProducts(string[] productInfo)
  94. {
  95. string name = productInfo[0];
  96. double price = double.Parse(productInfo[1]);
  97.  
  98. if (products.ContainsKey(name))
  99. {
  100. Console.WriteLine("Cannot add product! Product already exists!");
  101.  
  102. return;
  103. }
  104.  
  105. try
  106. {
  107. Product product;
  108.  
  109. if (productInfo.Length > 2)
  110. {
  111. bool isOnPromotion = bool.Parse(productInfo[2]);
  112. product = new Product(name, price, isOnPromotion);
  113. }
  114. else
  115. {
  116. product = new Product(name, price);
  117. }
  118.  
  119. products.Add(name, product);
  120. }
  121. catch (ArgumentException ex)
  122. {
  123. Console.WriteLine(ex.Message);
  124. }
  125. }
  126. private static void PrintOrderByNumber(string[] number)
  127. {
  128. int orderNumber = int.Parse(number[0]);
  129.  
  130. if (!orders.ContainsKey(orderNumber))
  131. {
  132. Console.WriteLine("Non existing order!");
  133. return;
  134. }
  135.  
  136. Console.WriteLine(orders[orderNumber].ToString());
  137. }
  138.  
  139. private static void GetTotalOrdersCount()
  140. {
  141. Console.WriteLine(Order.OrdersCount);
  142. }
  143.  
  144.  
  145. private static void CreateOrder(string[] orderInfo)
  146. {
  147. int orderNumber = int.Parse(orderInfo[0]);
  148.  
  149. if (orders.ContainsKey(orderNumber))
  150. {
  151. Console.WriteLine("Cannot create order! Order already exists!");
  152.  
  153. return;
  154. }
  155.  
  156. List<Product> productsList = orderInfo
  157. .Skip(1)
  158. .Where(p => products.ContainsKey(p))
  159. .Select(p => products[p])
  160. .ToList();
  161.  
  162. Order order = new Order(orderNumber, productsList);
  163. orders.Add(order.OrderNumber, order);
  164. }
  165.  
  166.  
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement