Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class CoffeeSupplies
- {
- static void Main()
- {
- var personCoffe = new Dictionary<string, string>();
- var coffeeQuantity = new Dictionary<string, uint>();
- readInfo(personCoffe, coffeeQuantity);
- firstCheckForNonLeftCoffee(personCoffe, coffeeQuantity);
- makeSomeCoffee(personCoffe, coffeeQuantity);
- reportCoffeeLeft(coffeeQuantity);
- reportWichMemberHasCoffeeLeft(personCoffe, coffeeQuantity);
- }
- private static void reportWichMemberHasCoffeeLeft(Dictionary<string, string> personCoffe, Dictionary<string, uint> coffeeQuantity)
- {
- var sorted = personCoffe
- .OrderBy(c => c.Value)
- .ThenByDescending(p => p.Key);
- Console.WriteLine("For:");
- foreach (var person in sorted)
- {
- var coffee = person.Value;
- if (coffeeQuantity.ContainsKey(coffee))
- {
- var name = person.Key;
- Console.WriteLine($"{name} {coffee}");
- }
- }
- }
- private static void reportCoffeeLeft(Dictionary<string, uint> coffeeQuantity)
- {
- var sorted = coffeeQuantity
- .OrderByDescending(coffee => coffee.Value);
- Console.WriteLine("Coffee Left:");
- foreach (var coffee in coffeeQuantity)
- {
- var type = coffee.Key;
- var quantity = coffee.Value;
- Console.WriteLine($"{type} {quantity}");
- }
- }
- private static void makeSomeCoffee(Dictionary<string, string> personCoffe, Dictionary<string, uint> coffeeQuantity)
- {
- while (true)
- {
- var order = Console.ReadLine().Split();
- if (string.Join(" ", order).Contains("end of week")) break;
- var person = order[0];
- var coffee = personCoffe[person];
- var quantity = uint.Parse(order[1]);
- if (coffeeQuantity.ContainsKey(coffee))
- {
- var coffeeLeft = coffeeQuantity[coffee];
- coffeeLeft -= quantity;
- coffeeQuantity[coffee] = coffeeLeft;
- if (coffeeLeft <= 0)
- {
- coffeeQuantity.Remove(coffee);
- Console.WriteLine($"Out of {coffee}");
- }
- }
- }
- }
- private static void firstCheckForNonLeftCoffee(Dictionary<string, string> personCoffe, Dictionary<string, uint> coffeeQuantity)
- {
- foreach (var person in personCoffe)
- {
- var coffee = person.Value;
- if (!coffeeQuantity.ContainsKey(coffee))
- {
- Console.WriteLine($"Out of {coffee}");
- }
- }
- }
- private static void readInfo(Dictionary<string, string> personCoffe, Dictionary<string, uint> coffeeQuantity)
- {
- var delimiters = Console.ReadLine().Split();
- var firstDel = delimiters[0];
- var secondDel = delimiters[1];
- while (true)
- {
- var command = Console.ReadLine();
- if (command == "end of info") break;
- if (command.Contains(firstDel))
- {
- string[] info = readData(command, firstDel);
- string name = info[0];
- string coffee = info[1];
- personCoffe[name] = coffee;
- }
- else if (command.Contains(secondDel))
- {
- string[] info = readData(command, secondDel);
- string coffee = info[0];
- uint quantity = uint.Parse(info[1]);
- if (coffeeQuantity.ContainsKey(coffee))
- {
- coffeeQuantity[coffee] += quantity;
- }
- else
- {
- coffeeQuantity[coffee] = quantity;
- }
- }
- }
- }
- private static string[] readData(string command, string delimiter)
- {
- var info = new string[2];
- info[0] = command.Substring(0, command.IndexOf(delimiter));
- info[1] = command.Remove(0, info[0].Length + delimiter.Length);
- return info;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement