Advertisement
Guest User

04. SoftUni Coffee Supplies

a guest
Jun 11th, 2016
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class CoffeeSupplies
  6. {
  7.     static void Main()
  8.     {
  9.         var personCoffe = new Dictionary<string, string>();
  10.         var coffeeQuantity = new Dictionary<string, uint>();
  11.  
  12.         readInfo(personCoffe, coffeeQuantity);
  13.  
  14.         firstCheckForNonLeftCoffee(personCoffe, coffeeQuantity);
  15.  
  16.         makeSomeCoffee(personCoffe, coffeeQuantity);
  17.  
  18.         reportCoffeeLeft(coffeeQuantity);
  19.  
  20.         reportWichMemberHasCoffeeLeft(personCoffe, coffeeQuantity);
  21.     }
  22.  
  23.     private static void reportWichMemberHasCoffeeLeft(Dictionary<string, string> personCoffe, Dictionary<string, uint> coffeeQuantity)
  24.     {
  25.         var sorted = personCoffe
  26.             .OrderBy(c => c.Value)
  27.             .ThenByDescending(p => p.Key);
  28.  
  29.         Console.WriteLine("For:");
  30.         foreach (var person in sorted)
  31.         {
  32.             var coffee = person.Value;
  33.  
  34.             if (coffeeQuantity.ContainsKey(coffee))
  35.             {
  36.                 var name = person.Key;
  37.  
  38.                 Console.WriteLine($"{name} {coffee}");
  39.             }
  40.         }
  41.     }
  42.  
  43.     private static void reportCoffeeLeft(Dictionary<string, uint> coffeeQuantity)
  44.     {
  45.         var sorted = coffeeQuantity
  46.             .OrderByDescending(coffee => coffee.Value);
  47.  
  48.         Console.WriteLine("Coffee Left:");
  49.         foreach (var coffee in coffeeQuantity)
  50.         {
  51.             var type = coffee.Key;
  52.             var quantity = coffee.Value;
  53.             Console.WriteLine($"{type} {quantity}");
  54.         }
  55.     }
  56.  
  57.     private static void makeSomeCoffee(Dictionary<string, string> personCoffe, Dictionary<string, uint> coffeeQuantity)
  58.     {
  59.         while (true)
  60.         {
  61.             var order = Console.ReadLine().Split();
  62.             if (string.Join(" ", order).Contains("end of week")) break;
  63.  
  64.             var person = order[0];
  65.             var coffee = personCoffe[person];
  66.             var quantity = uint.Parse(order[1]);
  67.  
  68.             if (coffeeQuantity.ContainsKey(coffee))
  69.             {
  70.                 var coffeeLeft = coffeeQuantity[coffee];
  71.                 coffeeLeft -= quantity;
  72.                 coffeeQuantity[coffee] = coffeeLeft;
  73.  
  74.                 if (coffeeLeft <= 0)
  75.                 {
  76.                     coffeeQuantity.Remove(coffee);
  77.                     Console.WriteLine($"Out of {coffee}");
  78.                 }
  79.             }
  80.         }        
  81.     }
  82.  
  83.     private static void firstCheckForNonLeftCoffee(Dictionary<string, string> personCoffe, Dictionary<string, uint> coffeeQuantity)
  84.     {
  85.         foreach (var person in personCoffe)
  86.         {
  87.             var coffee = person.Value;
  88.             if (!coffeeQuantity.ContainsKey(coffee))
  89.             {
  90.                 Console.WriteLine($"Out of {coffee}");
  91.             }
  92.         }
  93.     }
  94.  
  95.     private static void readInfo(Dictionary<string, string> personCoffe, Dictionary<string, uint> coffeeQuantity)
  96.     {
  97.         var delimiters = Console.ReadLine().Split();
  98.  
  99.         var firstDel = delimiters[0];
  100.         var secondDel = delimiters[1];
  101.  
  102.         while (true)
  103.         {
  104.             var command = Console.ReadLine();
  105.             if (command == "end of info") break;
  106.  
  107.             if (command.Contains(firstDel))
  108.             {
  109.                 string[] info = readData(command, firstDel);
  110.  
  111.                 string name = info[0];
  112.                 string coffee = info[1];
  113.                
  114.                 personCoffe[name] = coffee;                
  115.             }
  116.             else if (command.Contains(secondDel))
  117.             {
  118.                 string[] info = readData(command, secondDel);
  119.  
  120.                 string coffee = info[0];
  121.                 uint quantity = uint.Parse(info[1]);
  122.  
  123.                 if (coffeeQuantity.ContainsKey(coffee))
  124.                 {
  125.                     coffeeQuantity[coffee] += quantity;
  126.                 }
  127.                 else
  128.                 {
  129.                     coffeeQuantity[coffee] = quantity;
  130.                 }
  131.                
  132.             }
  133.         }
  134.     }
  135.  
  136.     private static string[] readData(string command, string delimiter)
  137.     {
  138.         var info = new string[2];
  139.        
  140.         info[0] = command.Substring(0, command.IndexOf(delimiter));
  141.         info[1] = command.Remove(0, info[0].Length + delimiter.Length);
  142.  
  143.         return info;
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement