Advertisement
desislava_topuzakova

Program.cs

May 15th, 2022
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace RegularExam
  5. {
  6. internal class Program
  7. {
  8.  
  9. static Dictionary<string, FlowerStore> stores = new Dictionary<string, FlowerStore>();
  10. static void Main(string[] args)
  11. {
  12. string input;
  13.  
  14. while ((input = Console.ReadLine()) != "STOP")
  15. {
  16. string[] splittedInput = input.Split(' ');
  17. string command = splittedInput[0];
  18.  
  19. switch (command)
  20. {
  21. case "AddFlower":
  22. AddFlower(splittedInput[1], splittedInput[2], double.Parse(splittedInput[3]), splittedInput[4]);
  23. break;
  24. case "SellFlower":
  25. SellFlower(splittedInput[1], splittedInput[2], double.Parse(splittedInput[3]), splittedInput[4]);
  26. break;
  27. case "CalculateTotalPrice":
  28. CalculateTotalPrice(splittedInput[1]);
  29. break;
  30. case "GetFlowerWithHighestPrice":
  31. GetFlowerWithHighestPrice(splittedInput[1]);
  32. break;
  33. case "GetFlowerWithLowestPrice":
  34. GetFlowerWithLowestPrice(splittedInput[1]);
  35. break;
  36. case "RenameFlowerStore":
  37. RenameFlowerStore(splittedInput[1], splittedInput[2]);
  38. break;
  39. case "SellAllFlowers":
  40. SellAllFlowers(splittedInput[1]);
  41. break;
  42. case "FlowerStoreInfo":
  43. FlowerStoreInfo(splittedInput[1]);
  44. break;
  45. case "CreateFlowerStore":
  46. CreateFlowerStore(splittedInput[1]);
  47. break;
  48. default:
  49. Console.WriteLine("Invalid command!");
  50. break;
  51. }
  52. }
  53.  
  54. }
  55.  
  56. private static void AddFlower(string type, string color, double price, string name)
  57. {
  58. try
  59. {
  60. Flower flower = new Flower(type, color, price);
  61. if (!stores.ContainsKey(name))
  62. {
  63. Console.WriteLine("Could not add this flower to your store.");
  64. return;
  65. }
  66. FlowerStore store = stores[name];
  67. store.AddFlower(flower);
  68. Console.WriteLine($"You added flower {type} with color {color} to store {store.Name}.");
  69.  
  70. }
  71. catch (ArgumentException ex)
  72. {
  73. Console.WriteLine(ex.Message);
  74. }
  75. }
  76.  
  77. private static void SellFlower(string type, string color, double price, string name)
  78. {
  79. try
  80. {
  81. if (!stores.ContainsKey(name))
  82. {
  83. Console.WriteLine("Could not sell this flower from your store.");
  84. return;
  85. }
  86.  
  87. Flower flower = new Flower(type, color, price);
  88. FlowerStore store = stores[name];
  89. if (store.SellFlower(flower))
  90. {
  91. Console.WriteLine($"You sold flower {type} with color {color} from flower store {name}.");
  92. }
  93. else
  94. {
  95. Console.WriteLine($"Did not sell flower {type} with color {color} from flower store {name}.");
  96. }
  97. }
  98. catch (ArgumentException ex)
  99. {
  100. Console.WriteLine(ex.Message);
  101. }
  102. }
  103.  
  104. private static void CalculateTotalPrice(string name)
  105. {
  106. try
  107. {
  108. if (!stores.ContainsKey(name))
  109. {
  110. Console.WriteLine("Could not calculate total price.");
  111. return;
  112. }
  113. FlowerStore store = stores[name];
  114.  
  115. Console.WriteLine($"Total price: {store.CalculateTotalPrice():F2}");
  116. }
  117. catch (ArgumentException ex)
  118. {
  119. Console.WriteLine(ex.Message);
  120. }
  121. }
  122.  
  123. private static void RenameFlowerStore(string name, string newName)
  124. {
  125.  
  126. if (!stores.ContainsKey(name))
  127. {
  128. Console.WriteLine($"Could not rename the store {name}.");
  129. return;
  130. }
  131. FlowerStore store = stores[name];
  132.  
  133. try
  134. {
  135. store.RenameFlowerStore(newName);
  136. stores.Remove(name);
  137. stores.Add(newName, store);
  138. Console.WriteLine($"You renamed your store from {name} to {newName}.");
  139. }
  140. catch (ArgumentException ex)
  141. {
  142. Console.WriteLine(ex.Message);
  143. }
  144. }
  145.  
  146. private static void SellAllFlowers(string name)
  147. {
  148.  
  149. if (!stores.ContainsKey(name))
  150. {
  151. Console.WriteLine($"Could not sell all flowers from store {name}.");
  152. return;
  153. }
  154. FlowerStore store = stores[name];
  155.  
  156. store.SellAllFlowers();
  157. Console.WriteLine($"You sold all flowers from store {name}.");
  158. }
  159.  
  160. private static void FlowerStoreInfo(string name)
  161. {
  162. if (!stores.ContainsKey(name))
  163. {
  164. Console.WriteLine($"Could not get store {name}.");
  165. return;
  166. }
  167. FlowerStore store = stores[name];
  168. Console.WriteLine(store.ToString());
  169. }
  170.  
  171. private static void GetFlowerWithLowestPrice(string name)
  172. {
  173.  
  174. if (!stores.ContainsKey(name))
  175. {
  176. Console.WriteLine($"Could not get flower with lowest price from store {name}.");
  177. return;
  178. }
  179. FlowerStore store = stores[name];
  180.  
  181. Console.WriteLine($"Flower from store {name} has lowest price: {store.GetFlowerWithLowestPrice().Price:F2}");
  182. }
  183.  
  184. private static void GetFlowerWithHighestPrice(string name)
  185. {
  186.  
  187. if (!stores.ContainsKey(name))
  188. {
  189. Console.WriteLine($"Could not get flower with highest price from store {name}.");
  190. return;
  191. }
  192. FlowerStore store = stores[name];
  193.  
  194. Console.WriteLine($"Flower from store {name} has highest price: {store.GetFlowerWithHighestPrice().Price:F2}");
  195. }
  196.  
  197.  
  198. private static void CreateFlowerStore(string name)
  199. {
  200.  
  201. try
  202. {
  203. FlowerStore store = new FlowerStore(name);
  204. stores.Add(name, store);
  205. Console.WriteLine($"You created flower store {name}.");
  206. }
  207. catch (ArgumentException ex)
  208. {
  209. Console.WriteLine(ex.Message);
  210.  
  211. }
  212. }
  213. }
  214. }
  215.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement