Advertisement
Guest User

C# Advanced (06. Wardrobe)

a guest
Mar 12th, 2022
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Wardrobe
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             int inputCount = int.Parse(Console.ReadLine());
  12.  
  13.             // key - color
  14.             // value - Dictionary<item, itemCount>
  15.             Dictionary<string, Dictionary<string, int>> wardrobe = new Dictionary<string, Dictionary<string, int>>();
  16.  
  17.             for (int i = 0; i < inputCount; i++)
  18.             {
  19.                 string[] colorAndClothes = Console.ReadLine()
  20.                                                   .Split(" -> ", StringSplitOptions.RemoveEmptyEntries)
  21.                                                   .ToArray();
  22.  
  23.                 string[] clothes = colorAndClothes[1].Split(",", StringSplitOptions.RemoveEmptyEntries)
  24.                                                      .ToArray();
  25.  
  26.                 if (wardrobe.ContainsKey(colorAndClothes[0]) == false) // if given color doesn't exist
  27.                 {
  28.                     wardrobe.Add(colorAndClothes[0], new Dictionary<string, int>()); // add color
  29.  
  30.                     for (int j = 0; j < clothes.Length; j++) // add items with this color
  31.                     {
  32.                         wardrobe[colorAndClothes[0]].Add(clothes[j], 1);
  33.                     }
  34.                 }
  35.                 else
  36.                 {
  37.                     for (int j = 0; j < clothes.Length; j++)
  38.                     {
  39.                         if (wardrobe[colorAndClothes[0]].ContainsKey(clothes[j]) == false) // if current item doesn't exist - add item
  40.                         {
  41.                             wardrobe[colorAndClothes[0]].Add(clothes[j], 1);
  42.                         }
  43.                         else
  44.                         {
  45.                             wardrobe[colorAndClothes[0]][clothes[j]] += 1; // else - itemCount++
  46.                         }
  47.                     }
  48.                 }
  49.             }
  50.  
  51.             // Receive from user item to search for in the wardrobe
  52.             string[] searchFor = Console.ReadLine()
  53.                                         .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  54.                                         .ToArray();
  55.  
  56.             // Print result
  57.             foreach (var kvp in wardrobe)
  58.             {
  59.                 Console.WriteLine($"{kvp.Key} clothes:");
  60.                 foreach (var internalKVP in kvp.Value)
  61.                 {
  62.                     if (kvp.Key == searchFor[0] && internalKVP.Key == searchFor[1])
  63.                     {
  64.                         Console.WriteLine($"* {internalKVP.Key} - {internalKVP.Value} (found!)");
  65.                     }
  66.                     else
  67.                     {
  68.                         Console.WriteLine($"* {internalKVP.Key} - {internalKVP.Value}");
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.     }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement