Advertisement
Guest User

Untitled

a guest
Mar 10th, 2017
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 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 _4.Exam_Shopping
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Dictionary<string, int> shop = new Dictionary<string, int>();
  14.  
  15. while (true)
  16. {
  17. string input = Console.ReadLine();
  18. if (input.Equals("exam time")) break;
  19. string[] command = input.Split(' ');
  20. if (input == "shopping time") continue;
  21. string product = command[1];
  22. int quantity = int.Parse(command[2]);
  23. if (command[0].Equals("stock"))
  24. {
  25. if (!shop.ContainsKey(product))
  26. {
  27. shop.Add(product, quantity);
  28. }
  29. else
  30. {
  31. shop[product] += quantity;
  32. }
  33. }
  34. else if (command[0].Equals("buy"))
  35. {
  36. if (!shop.ContainsKey(product))
  37. {
  38. Console.WriteLine($"{product} doesn't exist");
  39. }
  40. else if (shop.ContainsKey(product))
  41. {
  42. int stock = shop[product];
  43. if (stock == 0)
  44. {
  45. Console.WriteLine($"{product} out of stock");
  46. break;
  47. }
  48. else if (quantity > stock)
  49. {
  50. shop[product] = 0;
  51. }
  52. else
  53. {
  54. shop[product] -= quantity;
  55. }
  56. }
  57. }
  58. }
  59. foreach (var item in shop)
  60. {
  61. if (item.Value == 0)
  62. {
  63. continue;
  64. }
  65. Console.WriteLine($"{item.Key} -> {item.Value}");
  66. }
  67.  
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement