Advertisement
Guest User

04. SoftUni Coffee Supplies

a guest
Jun 10th, 2016
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.19 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 SoftUniCoffeeSupplies
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[] delimiters = Console.ReadLine().Split(' ');
  14.             string input = Console.ReadLine();
  15.             var personCoffee=new Dictionary<string,string>();
  16.             var coffeeQuantity=new Dictionary<string,int>();
  17.             var personQuantity = new Dictionary<string, int>();
  18.             var coffeDrank = new Dictionary<string, int>();
  19.             var coffeeLeft = new Dictionary<string, int>();
  20.  
  21.             //First loop for input
  22.             while (input != "end of info")
  23.             {
  24.                 int personSplitIndex=input.IndexOf(delimiters[0]);
  25.                 if (personSplitIndex != -1)
  26.                 {
  27.                     //PersonName[firstDelimiter]CoffeeType
  28.                     string person = input.Substring(0, personSplitIndex);
  29.                     string coffee = input.Remove(0, personSplitIndex + delimiters[0].Length);
  30.                     personCoffee[person] = coffee;
  31.                 }
  32.                 else
  33.                 {
  34.                     //CoffeeType[secondDelimiter]Quantity
  35.                     int coffeSplitIndex = input.IndexOf(delimiters[1]);
  36.                     string coffee= input.Substring(0, coffeSplitIndex);
  37.                     int quantity = int.Parse(input.Remove(0, coffeSplitIndex + delimiters[1].Length));
  38.                     if (coffeeQuantity.Keys.Contains(coffee))
  39.                     {
  40.                         coffeeQuantity[coffee] += quantity;
  41.                     }
  42.                     else
  43.                     {
  44.                         coffeeQuantity[coffee] = quantity;
  45.                     }
  46.                 }
  47.                 input = Console.ReadLine();
  48.             }
  49.            
  50.             //Second loop for input
  51.             input = Console.ReadLine();
  52.             while (input != "end of week")
  53.             {
  54.                 string[] elements = input.Split(' ');
  55.                 string name = elements[0];
  56.                 int quantity = int.Parse(elements[1]);
  57.  
  58.                 if (personQuantity.ContainsKey(name))
  59.                 {
  60.                     personQuantity[name] += quantity;
  61.                 }
  62.                 else
  63.                 {
  64.                     personQuantity[name] = quantity;
  65.                 }
  66.                 input = Console.ReadLine();
  67.             }
  68.  
  69.            
  70.             //Calculate how much coffee is drank from each type
  71.             foreach (var pair in personCoffee)
  72.             {
  73.                 string coffee = pair.Value;
  74.                 string name = pair.Key;
  75.                 if (personQuantity.Keys.Contains(name))
  76.                 {
  77.                     int drank = personQuantity[name];
  78.                     if (coffeDrank.ContainsKey(coffee))
  79.                     {
  80.                         coffeDrank[coffee] += drank;
  81.                     }
  82.                     else
  83.                     {
  84.                         coffeDrank[coffee] = drank;
  85.                     }
  86.  
  87.                 }
  88.             }
  89.  
  90.            
  91.             //Check if out of [x type of cofee] or add to dictionary with left types
  92.             foreach (var i in personCoffee)
  93.             {
  94.                 string coffee = i.Value;
  95.                 int quantity;
  96.                 int drank;
  97.  
  98.                 if (coffeeQuantity.Keys.Contains(coffee))
  99.                 {
  100.                     quantity = coffeeQuantity[coffee];
  101.                 }
  102.                 else
  103.                 {
  104.                     quantity = 0;
  105.                 }
  106.  
  107.                 if (coffeDrank.Keys.Contains(coffee))
  108.                 {
  109.                     drank = coffeDrank[coffee];
  110.                 }
  111.                 else
  112.                 {
  113.                     drank = 0;
  114.                 }
  115.  
  116.                 if (drank < quantity)
  117.                 {
  118.                     int left = quantity - drank;
  119.                     coffeeLeft[coffee] = left;
  120.                 }
  121.                 else
  122.                 {
  123.                     Console.Out.WriteLine($"Out of {coffee}");
  124.                 }
  125.             }
  126.  
  127.  
  128.             //Print coffee types that are left sorted by amount
  129.             Console.Out.WriteLine("Coffee Left:");
  130.             foreach (var i in coffeeLeft.OrderBy(x => -x.Value))
  131.             {
  132.                 string coffee = i.Key;
  133.                 int left = i.Value;
  134.                 Console.Out.WriteLine($"{coffee} {left}");
  135.             }
  136.  
  137.             //Print people and the type of coffee they drink(if it is left). Ordered by coffee name, then by person name
  138.             Console.Out.WriteLine("For:");
  139.             foreach (var i in coffeeLeft.OrderBy(x => x.Key))
  140.             {
  141.                 string coffee = i.Key;
  142.                 foreach (var person in personCoffee.OrderByDescending(x => x.Key))
  143.                 {
  144.                     string coffee2 = person.Value;
  145.                     if (coffee == coffee2)
  146.                     {
  147.                         Console.Out.WriteLine($"{person.Key} {coffee}");
  148.                     }
  149.                 }
  150.             }
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement