sivancheva

CoffeeSupplies100/100

Oct 11th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 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. using System.Text.RegularExpressions;
  7.  
  8. namespace CoffeeSupplies
  9. {
  10. class CoffeeSupplies
  11. {
  12. static void Main(string[] args)
  13. {
  14. string[] delimiters = Console.ReadLine().Split();
  15.  
  16.  
  17. string delimiterOne = delimiters[0];
  18. string delimiterTwo = delimiters[1];
  19.  
  20. string command = Console.ReadLine();
  21.  
  22. List<CoffeeDrinker> coffeeDrinkers = new List<CoffeeDrinker>();
  23. List<Coffee> coffeeTypes = new List<Coffee>();
  24.  
  25. while (command != "end of info")
  26. {
  27. if (Regex.IsMatch(command, $"(\\w+)(?:{Regex.Escape(delimiterOne)})(\\w+)"))
  28. {
  29. string[] personAndCoffee = Regex.Split(command, Regex.Escape(delimiterOne));
  30.  
  31. CoffeeDrinker drinker = new CoffeeDrinker
  32. {
  33. Name = personAndCoffee[0],
  34. CoffeeType = new Coffee { Name = personAndCoffee[1] }
  35. };
  36. if (!coffeeTypes.Any(x => x.Name == drinker.CoffeeType.Name))
  37. {
  38. coffeeTypes.Add(new Coffee { Name = personAndCoffee[1] });
  39. }
  40.  
  41. coffeeDrinkers.Add(drinker);
  42. }
  43. else
  44. {
  45. string[] coffeeAndQuantity = Regex.Split(command, Regex.Escape(delimiterTwo));
  46.  
  47. Coffee coffee = new Coffee
  48. {
  49. Name = coffeeAndQuantity[0],
  50. Quantity = int.Parse(coffeeAndQuantity[1])
  51. };
  52.  
  53. if (coffeeTypes.Any(x => x.Name == coffee.Name))
  54. {
  55. var indx = coffeeTypes.FindIndex(x => x.Name == coffee.Name);
  56.  
  57. coffeeTypes[indx].Quantity += coffee.Quantity;
  58. }
  59. else
  60. {
  61. coffeeTypes.Add(coffee);
  62. }
  63. }
  64.  
  65. command = Console.ReadLine();
  66. }
  67.  
  68. command = Console.ReadLine();
  69.  
  70. foreach (var cof in coffeeTypes.Where(x => x.Quantity <= 0))
  71. {
  72. Console.WriteLine($"Out of {cof.Name}");
  73. }
  74.  
  75. while (command != "end of week")
  76. {
  77. string[] personAndCoffeCups = command.Split();
  78. string person = personAndCoffeCups[0];
  79. int cups = int.Parse(personAndCoffeCups[1]);
  80.  
  81. int indxP = coffeeDrinkers.FindIndex(x => x.Name == person);
  82.  
  83. string coffeeName = coffeeDrinkers[indxP].CoffeeType.Name;
  84.  
  85. int indxC = coffeeTypes.FindIndex(x => x.Name == coffeeName);
  86.  
  87.  
  88. coffeeTypes[indxC].Quantity -= cups;
  89. if (coffeeTypes[indxC].Quantity <= 0)
  90. {
  91. Console.WriteLine($"Out of {coffeeName}");
  92. }
  93.  
  94.  
  95.  
  96. command = Console.ReadLine();
  97. }
  98. List<string> coffeeNames = new List<string>();
  99.  
  100. Console.WriteLine("Coffee Left:");
  101. foreach (var c in coffeeTypes.Where(x => x.Quantity > 0).OrderByDescending(x => x.Quantity))
  102. {
  103. Console.WriteLine($"{c.Name} {c.Quantity}");
  104. coffeeNames.Add(c.Name);
  105. }
  106.  
  107. var endResults = new List<CoffeeDrinker>();
  108.  
  109. foreach (var cName in coffeeNames)
  110. {
  111. foreach (var drinker in coffeeDrinkers)
  112. {
  113. if (drinker.CoffeeType.Name == cName)
  114. {
  115. endResults.Add(drinker);
  116. }
  117. }
  118. }
  119.  
  120. Console.WriteLine("For:");
  121.  
  122. foreach (var drinker in endResults.OrderBy(x => x.CoffeeType.Name).ThenByDescending(p => p.Name))
  123. {
  124. //{personName} {coffeeType}
  125. Console.WriteLine($"{drinker.Name} {drinker.CoffeeType.Name}");
  126. }
  127. }
  128. }
  129.  
  130. class CoffeeDrinker
  131. {
  132. public string Name { get; set; }
  133. public Coffee CoffeeType { get; set; }
  134. }
  135.  
  136. class Coffee
  137. {
  138. public string Name { get; set; }
  139. public int Quantity { get; set; } = 0;
  140. }
  141. }
Add Comment
Please, Sign In to add comment