Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace SoftUniCoffeeSupplies2
- {
- class Program
- {
- static void Main()
- {
- string[] delimiters = Console.ReadLine().Split(' ');
- string personDelimiter = delimiters[0];
- string coffeeDelimiter = delimiters[1];
- string input = Console.ReadLine();
- //Lists to hold the objects
- var people = new List<Person>();
- var coffees = new List<Coffee>();
- //First loop for input
- while (input != "end of info")
- {
- int personSplitIndex = input.IndexOf(personDelimiter);
- int coffeSplitIndex = input.IndexOf(coffeeDelimiter);
- //Check whitch type the input is
- if (personSplitIndex != -1)
- {
- //PersonName[firstDelimiter]CoffeeType
- people.Add(Person.ReadPerson(input, personSplitIndex, personDelimiter));
- //Check if his coffee exists
- string coffeeName = input.Remove(0, personSplitIndex + personDelimiter.Length);
- bool exists = false;
- foreach (var coffee in coffees)
- {
- if (coffee.Name == coffeeName)
- {
- //Exists
- exists = true;
- }
- }
- if (!exists)
- {
- //Does not exist. Create it
- coffees.Add(Coffee.CreateCoffee(0,coffeeName));
- }
- }
- else if (coffeSplitIndex != -1)
- {
- //CoffeeType[secondDelimiter]Quantity
- string coffeeName = input.Substring(0, coffeSplitIndex);
- int quantity = int.Parse(input.Remove(0, coffeSplitIndex + delimiters[1].Length));
- //Check if object for this coffee exists
- bool exists = false;
- foreach (var coffee in coffees)
- {
- if (coffee.Name == coffeeName)
- {
- //Exists. Update quantity
- coffee.Quantity += quantity;
- exists = true;
- break;
- }
- }
- if (!exists)
- {
- //It does not exist. Create it
- coffees.Add(Coffee.CreateCoffee(quantity, coffeeName));
- }
- }
- //Keep getting input until "end of info"
- input = Console.ReadLine();
- }
- //Second Loop for input
- input = Console.ReadLine();
- while (input != "end of week")
- {
- string[] elements = input.Split(' ');
- string name = elements[0];
- int amount = int.Parse(elements[1]);
- //Check who this person is
- foreach (var person in people)
- {
- if (person.Name == name)
- {
- //Person found. Add amount
- person.Amount += amount;
- //Get what coffe he drinks and find it
- string coffeeName = person.CoffeeName;
- foreach (var coffee in coffees)
- {
- if (coffee.Name == coffeeName)
- {
- //Coffe found. Add amount drunk
- coffee.Drunk += amount;
- }
- }
- }
- }
- //Keep getting input until "end of week"
- input = Console.ReadLine();
- }
- //Start printing
- foreach (var coffee in coffees)
- {
- if (coffee.IsLeft()<=0)
- {
- //Print each coffee type out of stock
- Console.Out.WriteLine($"Out of {coffee.Name}");
- }
- }
- Console.Out.WriteLine("Coffee Left:");
- foreach (var coffee in coffees.OrderBy(x => -x.IsLeft()))
- {
- if (coffee.IsLeft() > 0)
- {
- //Print each coffee type in stock
- //ordered by quantity
- Console.Out.WriteLine($"{coffee.Name} {coffee.IsLeft()}");
- }
- }
- Console.Out.WriteLine("For:");
- foreach (var coffee in coffees.OrderBy(x => x.Name))
- {
- if (coffee.IsLeft() > 0)
- {
- foreach (var person in people.OrderByDescending(x => x.Name))
- {
- if (coffee.Name == person.CoffeeName)
- {
- //Print each person whose coffe is in stock
- //ordered first by coffe name then by person name
- Console.Out.WriteLine($"{person.Name} {coffee.Name}");
- }
- }
- }
- }
- }
- }
- }
- class Person
- {
- //Parameters
- public int Amount { get; set; }
- public string CoffeeName { get; set; }
- public string Name { get; set; }
- //Actions
- public static Person ReadPerson(string input, int personSplitIndex, string personDelimiter)
- {
- string coffeeName = input.Remove(0, personSplitIndex + personDelimiter.Length);
- var person = new Person
- {
- Name = input.Substring(0, personSplitIndex),
- CoffeeName = coffeeName,
- Amount=0
- };
- return person;
- }
- }
- class Coffee
- {
- //Parameters
- public string Name { get; set; }
- public int Quantity { get; set; }
- public int Drunk { get; set; }
- //Actions
- public static Coffee CreateCoffee(int quantity, string name)
- {
- var coffee = new Coffee
- {
- Name = name,
- Quantity = quantity,
- Drunk=0
- };
- return coffee;
- }
- public int IsLeft()
- {
- int left = Quantity - Drunk;
- return left;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement