Advertisement
ElviraPetkova

Santa new List

Mar 6th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _2._Santa_s_New_List
  6. {
  7.     public class Program
  8.     {
  9.         public static void Main()
  10.         {
  11.             Dictionary<string, int> children = new Dictionary<string, int>(); //key = children, value-total toys;
  12.             Dictionary<string, int> toys = new Dictionary<string, int>(); //key = toy, value = amount;
  13.  
  14.             while (true)
  15.             {
  16.                 string input = Console.ReadLine();
  17.                 if(input == "END")
  18.                 {
  19.                     break;
  20.                 }
  21.  
  22.                 string[] information = input.Split("->");
  23.                 string actionOrName = information[0];
  24.  
  25.                 if(actionOrName == "Remove")
  26.                 {
  27.                     string childName = information[1];
  28.  
  29.                     if (children.ContainsKey(childName))
  30.                     {
  31.                         children.Remove(childName);
  32.                     }
  33.                 }
  34.                 else
  35.                 {
  36.                     string childName = actionOrName;
  37.                     string typeOfToy = information[1];
  38.                     int amount = int.Parse(information[2]);
  39.  
  40.                     if(children.ContainsKey(childName) == false)
  41.                     {
  42.                         children.Add(childName, 0);
  43.                     }
  44.  
  45.                     children[childName] += amount;
  46.  
  47.                     if(toys.ContainsKey(typeOfToy) == false)
  48.                     {
  49.                         toys.Add(typeOfToy, 0);
  50.                     }
  51.  
  52.                     toys[typeOfToy] += amount;
  53.  
  54.                 }
  55.             }
  56.  
  57.             Console.WriteLine("Children:");
  58.             foreach (var child in children.OrderByDescending(x=>x.Value).ThenBy(x=>x.Key))
  59.             {
  60.                 Console.WriteLine($"{child.Key} -> {child.Value}");
  61.             }
  62.  
  63.             Console.WriteLine("Presents:");
  64.             foreach (var kvp in toys)
  65.             {
  66.                 Console.WriteLine($"{kvp.Key} -> {kvp.Value}");
  67.             }
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement