Advertisement
Guest User

04. SoftUni Coffee Supplies [with classes]

a guest
Jun 10th, 2016
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace SoftUniCoffeeSupplies2
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             string[] delimiters = Console.ReadLine().Split(' ');
  12.             string personDelimiter = delimiters[0];
  13.             string coffeeDelimiter = delimiters[1];
  14.             string input = Console.ReadLine();
  15.  
  16.             //Lists to hold the objects
  17.             var people = new List<Person>();
  18.             var coffees = new List<Coffee>();
  19.  
  20.             //First loop for input
  21.             while (input != "end of info")
  22.             {
  23.                 int personSplitIndex = input.IndexOf(personDelimiter);
  24.                 int coffeSplitIndex = input.IndexOf(coffeeDelimiter);
  25.  
  26.                 //Check whitch type the input is
  27.                 if (personSplitIndex != -1)
  28.                 {
  29.                     //PersonName[firstDelimiter]CoffeeType
  30.                     people.Add(Person.ReadPerson(input, personSplitIndex, personDelimiter));
  31.  
  32.                     //Check if his coffee exists
  33.                     string coffeeName = input.Remove(0, personSplitIndex + personDelimiter.Length);
  34.                     bool exists = false;
  35.                     foreach (var coffee in coffees)
  36.                     {
  37.                         if (coffee.Name == coffeeName)
  38.                         {
  39.                             //Exists
  40.                             exists = true;
  41.                         }
  42.                     }
  43.                     if (!exists)
  44.                     {
  45.                         //Does not exist. Create it
  46.                         coffees.Add(Coffee.CreateCoffee(0,coffeeName));
  47.                     }
  48.                 }
  49.                 else if (coffeSplitIndex != -1)
  50.                 {
  51.                     //CoffeeType[secondDelimiter]Quantity
  52.                     string coffeeName = input.Substring(0, coffeSplitIndex);
  53.                     int quantity = int.Parse(input.Remove(0, coffeSplitIndex + delimiters[1].Length));
  54.  
  55.                     //Check if object for this coffee exists
  56.                     bool exists = false;
  57.                     foreach (var coffee in coffees)
  58.                     {
  59.                         if (coffee.Name == coffeeName)
  60.                         {
  61.                             //Exists. Update quantity
  62.                             coffee.Quantity += quantity;
  63.                             exists = true;
  64.                             break;
  65.                         }
  66.                     }
  67.                     if (!exists)
  68.                     {
  69.                         //It does not exist. Create it
  70.                         coffees.Add(Coffee.CreateCoffee(quantity, coffeeName));
  71.                     }
  72.                 }
  73.  
  74.                 //Keep getting input until "end of info"
  75.                 input = Console.ReadLine();
  76.             }
  77.  
  78.             //Second Loop for input
  79.             input = Console.ReadLine();
  80.             while (input != "end of week")
  81.             {
  82.                 string[] elements = input.Split(' ');
  83.                 string name = elements[0];
  84.                 int amount = int.Parse(elements[1]);
  85.  
  86.                 //Check who this person is
  87.                 foreach (var person in people)
  88.                 {
  89.                     if (person.Name == name)
  90.                     {
  91.                         //Person found. Add amount
  92.                         person.Amount += amount;
  93.  
  94.                         //Get what coffe he drinks and find it
  95.                         string coffeeName = person.CoffeeName;
  96.                         foreach (var coffee in coffees)
  97.                         {
  98.                             if (coffee.Name == coffeeName)
  99.                             {
  100.                                 //Coffe found. Add amount drunk
  101.                                 coffee.Drunk += amount;
  102.                             }
  103.                         }
  104.                     }
  105.                 }
  106.  
  107.                 //Keep getting input until "end of week"
  108.                 input = Console.ReadLine();
  109.             }
  110.            
  111.             //Start printing
  112.             foreach (var coffee in coffees)
  113.             {
  114.                 if (coffee.IsLeft()<=0)
  115.                 {
  116.                     //Print each coffee type out of stock
  117.                     Console.Out.WriteLine($"Out of {coffee.Name}");
  118.                 }
  119.             }
  120.             Console.Out.WriteLine("Coffee Left:");
  121.             foreach (var coffee in coffees.OrderBy(x => -x.IsLeft()))
  122.             {
  123.                 if (coffee.IsLeft() > 0)
  124.                 {
  125.                     //Print each coffee type in stock
  126.                     //ordered by quantity
  127.                     Console.Out.WriteLine($"{coffee.Name} {coffee.IsLeft()}");
  128.                 }
  129.             }
  130.             Console.Out.WriteLine("For:");
  131.             foreach (var coffee in coffees.OrderBy(x => x.Name))
  132.             {
  133.                 if (coffee.IsLeft() > 0)
  134.                 {
  135.                     foreach (var person in people.OrderByDescending(x => x.Name))
  136.                     {
  137.                         if (coffee.Name == person.CoffeeName)
  138.                         {
  139.                             //Print each person whose coffe is in stock
  140.                             //ordered first by coffe name then by person name
  141.                             Console.Out.WriteLine($"{person.Name} {coffee.Name}");
  142.                         }
  143.                     }
  144.                 }
  145.             }
  146.         }
  147.     }
  148. }
  149.  
  150. class Person
  151. {
  152.     //Parameters
  153.     public int Amount { get; set; }
  154.     public string CoffeeName { get; set; }
  155.     public string Name { get; set; }
  156.  
  157.     //Actions
  158.     public static Person ReadPerson(string input, int personSplitIndex, string personDelimiter)
  159.     {
  160.         string coffeeName = input.Remove(0, personSplitIndex + personDelimiter.Length);
  161.         var person = new Person
  162.         {
  163.             Name = input.Substring(0, personSplitIndex),
  164.             CoffeeName = coffeeName,
  165.             Amount=0
  166.         };
  167.         return person;
  168.     }
  169. }
  170.  
  171. class Coffee
  172. {
  173.     //Parameters
  174.     public string Name { get; set; }
  175.     public int Quantity { get; set; }
  176.     public int Drunk { get; set; }
  177.  
  178.     //Actions
  179.     public static Coffee CreateCoffee(int quantity, string name)
  180.     {
  181.         var coffee = new Coffee
  182.         {
  183.             Name = name,
  184.             Quantity = quantity,
  185.             Drunk=0
  186.         };
  187.         return coffee;
  188.     }
  189.  
  190.     public int IsLeft()
  191.     {
  192.         int left = Quantity - Drunk;
  193.         return left;
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement