Menu
Coddy logo textTech

Recap - Manage Warehouse

Coddy C# 여정의 논리 및 흐름 섹션에 포함된 레슨 — 66개 중 54번째.

challenge icon

챌린지

중급

딕셔너리를 사용하여 창고 재고 관리 시스템을 만드세요. 귀하의 작업은 다음 작업을 수행하는 ManageWarehouse 메서드를 구현하는 것입니다:

  1. 먼저 모든 항목과 그 수량을 형식: "Item: [item], Quantity: [quantity]"로 출력하세요
  2. 인벤토리에 "apples"가 존재하는지 확인하고 "Apples in stock: [True/False]"를 출력하세요
  3. "bananas"가 존재하면 그 수량에 10을 더하세요 (직접 값 수정 사용)
  4. 수량이 0인 항목을 제거하세요
  5. 창고에 있는 고유 항목의 총 수를 출력하세요
  6. 마지막으로 업데이트된 인벤토리 딕셔너리를 반환하세요

직접 해보기

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class Program
{
    public static Dictionary<string, int> ManageWarehouse(Dictionary<string, int> inventory)
    {
        // 여기에 코드를 작성하세요
        
        return inventory;
    }
    
    static void Main(string[] args)
    {
        Dictionary<string, int> inventory = new Dictionary<string, int>();
        
        // JSON 형식인지 확인하기 위해 첫 번째 줄을 읽습니다
        string firstLine = Console.ReadLine();
        
        // 입력이 JSON 형식인지 확인합니다
        if (firstLine != null && firstLine.StartsWith("{") && firstLine.EndsWith("}"))
        {
            try
            {
                // JSON 입력을 처리합니다
                string jsonContent = firstLine.Substring(1, firstLine.Length - 2);
                
                // 따옴표 안에 있지 않은 쉼표를 기준으로 분리합니다
                string pattern = @",(?=(?:[^""]*""[^""]*"")*[^""]*$)";
                string[] entries = Regex.Split(jsonContent, pattern);
                
                foreach (string entry in entries)
                {
                    // 정규표현식을 사용하여 키와 값을 추출합니다
                    Match match = Regex.Match(entry, @"""([^""]+)""\s*:\s*(\d+)");
                    if (match.Success)
                    {
                        string keyMatch = match.Groups[1].Value;
                        int valueMatch = int.Parse(match.Groups[2].Value);
                        inventory.Add(keyMatch, valueMatch);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error parsing JSON input: {ex.Message}");
                return;
            }
        }
        else
        {
            // Process traditional input format
            int n = int.Parse(firstLine);
            for (int i = 0; i < n; i++)
            {
                string[] parts = Console.ReadLine().Split(':');
                inventory.Add(parts[0], int.Parse(parts[1]));
            }
        }
        
        Dictionary<string, int> result = ManageWarehouse(inventory);
        
        // Print updated inventory
        Console.WriteLine("Updated Inventory:");
        foreach (var item in result)
        {
            Console.WriteLine($"{item.Key}: {item.Value}");
        }
    }
}

논리 및 흐름의 모든 레슨