Advertisement
Guest User

Nikulden Meals

a guest
Dec 11th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace nikuldenMeals
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Dictionary<string, List<string>> likedMeals = new Dictionary<string, List<string>>();
  12.             int unlikedMeals = 0;
  13.             string input = Console.ReadLine();
  14.             while (input != "Stop")
  15.             {
  16.                 string[] cmdArg = input.Split("-");
  17.                 string command = cmdArg[0];
  18.                 string guestName = cmdArg[1];
  19.                 string meal = cmdArg[2];
  20.                 bool existingGuest = CheckForGuest(likedMeals, guestName);
  21.                 bool existingMeal = false;
  22.                 switch (command)
  23.                 {
  24.                     case "Like":
  25.                         if (!existingGuest)
  26.                         {
  27.                             likedMeals.Add(guestName, new List<string>());
  28.                             likedMeals[guestName].Add(meal);
  29.                         }
  30.                         else
  31.                         {
  32.                             existingMeal = CheckForMeal(likedMeals, guestName, meal);
  33.                             if (!existingMeal)
  34.                             {
  35.                                 likedMeals[guestName].Add(meal);
  36.                             }
  37.                         }
  38.                         break;
  39.                     case "Unlike":
  40.                         if (!existingGuest)
  41.                         {
  42.                             Console.WriteLine($"{guestName} is not at the party.");
  43.                         }
  44.                         else
  45.                         {
  46.                             existingMeal = CheckForMeal(likedMeals, guestName, meal);
  47.                             if (existingMeal)
  48.                             {
  49.                                 unlikedMeals++;
  50.                                 likedMeals[guestName].Remove(meal);
  51.                                 Console.WriteLine($"{guestName} doesn't like the {meal}.");
  52.                             }
  53.                             else
  54.                             {
  55.                                 Console.WriteLine($"{guestName} doesn't have the {meal} in his/her collection.");
  56.                             }
  57.                         }
  58.                         break;
  59.                 }
  60.                 input = Console.ReadLine();
  61.             }
  62.             likedMeals = likedMeals
  63.                 .OrderByDescending(x => x.Value.Count())
  64.                 .ThenBy(x => x.Key)
  65.                 .ToDictionary(a => a.Key, b => b.Value);
  66.             foreach (var guest in likedMeals)
  67.             {
  68.                 Console.WriteLine($"{guest.Key}: {string.Join(", ", guest.Value)}");
  69.             }
  70.             Console.WriteLine($"Unliked meals: {unlikedMeals}");
  71.         }
  72.  
  73.         static bool CheckForMeal(Dictionary<string, List<string>> likedMeals, string name, string meal)
  74.         {
  75.             if (likedMeals[name].Contains(meal))
  76.             {
  77.                 return true;
  78.             }
  79.             return false;
  80.         }
  81.  
  82.         static bool CheckForGuest(Dictionary<string, List<string>> likedMeals, string guestName)
  83.         {
  84.             if (likedMeals.ContainsKey(guestName))
  85.             {
  86.                 return true;
  87.             }
  88.             return false;
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement