Advertisement
Guest User

SoftUni Coffee Supply

a guest
Jun 11th, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace SoftUniCoffeSupply
  6. {
  7.     class SoftUniCoffeSupply
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string[] delimeters = Console.ReadLine().Split();
  12.             string firstDelimeter = delimeters[0];
  13.             string secondDelimeter = delimeters[1];
  14.             Dictionary<string, ulong> CoffeTypeAndQuantity = new Dictionary<string, ulong>();
  15.             Dictionary<string, string> PersonAndCoffe = new Dictionary<string, string>();
  16.             // Reading information about coffees, types and people who want them
  17.             while (true)
  18.             {
  19.                 string input = Console.ReadLine();
  20.                 if (input == "end of info")
  21.                 {
  22.                     break;
  23.                 }
  24.                 if (input.Contains(firstDelimeter))
  25.                 {
  26.                     string[] personinfo = input.Split(new[] { firstDelimeter }, StringSplitOptions.None);
  27.                     if (!PersonAndCoffe.ContainsKey(personinfo[0]))
  28.                     {
  29.                         PersonAndCoffe.Add(personinfo[0], personinfo[1]);
  30.                     }
  31.                     else
  32.                     {
  33.                         PersonAndCoffe[personinfo[0]] = personinfo[1];
  34.                     }
  35.                     foreach (var pair in PersonAndCoffe)
  36.                     {
  37.                         if (!CoffeTypeAndQuantity.ContainsKey(pair.Value))
  38.                         {
  39.                             CoffeTypeAndQuantity.Add(pair.Value, 0);
  40.                         }
  41.                     }
  42.                 }
  43.                 else
  44.                 {
  45.                     string[] coffeeinfo = input.Split(new[] { secondDelimeter }, StringSplitOptions.None);                    
  46.                     if (!CoffeTypeAndQuantity.ContainsKey(coffeeinfo[0]))
  47.                     {
  48.                         CoffeTypeAndQuantity.Add(coffeeinfo[0], ulong.Parse(coffeeinfo[1]));
  49.                     }
  50.                     else
  51.                     {
  52.                         CoffeTypeAndQuantity[coffeeinfo[0]] += ulong.Parse(coffeeinfo[1]);
  53.                     }
  54.                 }
  55.             }
  56.             // Reading information about quantity of coffee to be drinked
  57.             while (true)
  58.             {
  59.                 string command = Console.ReadLine();
  60.                 string[] QuantityWanted = command.Split();
  61.                 if (command == "end of week")
  62.                 {
  63.                     break;
  64.                 }
  65.                 string name = QuantityWanted[0];
  66.                 ulong quantity = ulong.Parse(QuantityWanted[1]);
  67.                 string coffee = PersonAndCoffe[name];
  68.                 CoffeTypeAndQuantity[coffee] -= quantity;
  69.             }
  70.             // Sorting the dictionaries
  71.             var SortedCoffees = CoffeTypeAndQuantity.OrderByDescending(x => x.Value).Where(x => x.Value > 0).ToDictionary(x => x.Key.ToString(), x => ulong.Parse(x.Value.ToString()), StringComparer.OrdinalIgnoreCase);
  72.             var SortedLeft = PersonAndCoffe.OrderBy(x => x.Value).ThenByDescending(x => x.Key).ToDictionary(x => x.Key.ToString(), x => x.Value.ToString(), StringComparer.OrdinalIgnoreCase);
  73.             // Printing the results
  74.             foreach (var coffee in CoffeTypeAndQuantity)
  75.             {
  76.                 if (coffee.Value == 0)
  77.                 {
  78.                     Console.WriteLine($"Out of {coffee.Key}");
  79.                 }
  80.             }
  81.             Console.WriteLine("Coffee Left:");
  82.             foreach (var coffee in SortedCoffees)
  83.             {
  84.                     Console.WriteLine($"{coffee.Key} {coffee.Value}");            
  85.             }
  86.             Console.WriteLine("For:");
  87.             foreach (var pair in SortedLeft)
  88.             {
  89.                 if (SortedCoffees.ContainsKey(pair.Value))
  90.                 {
  91.                     Console.WriteLine($"{pair.Key} {pair.Value}");
  92.                 }
  93.             }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement