yanchevilian

06. Wardrobe / C# Advanced/ Dictionary

Jul 25th, 2021 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Problem_6._Wardrobe
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int numberOfClothes = int.Parse(Console.ReadLine());
  12.            
  13.             Dictionary<string, Dictionary<string, int>> allClothes = new Dictionary<string, Dictionary<string, int>>();
  14.             FillWardrobe(allClothes, numberOfClothes);
  15.  
  16.             string[] searchingCloth = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
  17.             string color = searchingCloth[0];
  18.             string clothing = searchingCloth[1];
  19.             PrintWardrobe(allClothes, color, clothing);
  20.         }
  21.  
  22.         static void PrintWardrobe(Dictionary<string, Dictionary<string, int>> allClothes, string color, string clothing)
  23.         {
  24.             foreach (var currentCloth in allClothes)
  25.             {
  26.                 Console.WriteLine($"{currentCloth.Key} clothes:");
  27.  
  28.                 foreach (var cloth in currentCloth.Value)
  29.                 {
  30.                     if (color == currentCloth.Key && clothing == cloth.Key)
  31.                     {
  32.                         Console.WriteLine($"* {cloth.Key} - {cloth.Value} (found!)");
  33.                     }
  34.                     else
  35.                     {
  36.                         Console.WriteLine($"* {cloth.Key} - {cloth.Value}");
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.  
  42.         static Dictionary<string, Dictionary<string,int>> FillWardrobe(Dictionary<string, Dictionary<string, int>> allClothes, int numberOfClothes)
  43.         {
  44.             for (int i = 0; i < numberOfClothes; i++)
  45.             {
  46.                 string[] input = Console.ReadLine().Split(" -> ", StringSplitOptions.RemoveEmptyEntries);
  47.                 string currentColor = input[0]; // blue
  48.                 string[] items = input[1].Split(",", StringSplitOptions.RemoveEmptyEntries); //dress,pants,jeans
  49.  
  50.                 if (allClothes.ContainsKey(currentColor) == false)
  51.                 {
  52.                     allClothes.Add(currentColor, new Dictionary<string, int>());
  53.                 }
  54.  
  55.                 for (int cloth = 0; cloth < items.Length; cloth++)
  56.                 {
  57.                     string currentCloth = items[cloth];
  58.                     if (allClothes[currentColor].ContainsKey(currentCloth) == false)
  59.                     {
  60.                         allClothes[currentColor].Add(currentCloth, 0);
  61.                     }
  62.  
  63.                     allClothes[currentColor][currentCloth]++;
  64.                 }
  65.             }
  66.  
  67.             return allClothes;
  68.         }
  69.     }
  70. }
  71.  
Add Comment
Please, Sign In to add comment