Menu
Coddy logo textTech

Recap - Manage Warehouse

Part of the Logic & Flow section of Coddy's C# journey — lesson 54 of 66.

challenge icon

Challenge

Medium

Create a warehouse inventory management system using dictionaries. Your task is to implement the ManageWarehouse method that performs the following operations:

  1. First, print all items and their quantities in format: "Item: [item], Quantity: [quantity]"
  2. Check if "apples" exist in inventory and print: "Apples in stock: [True/False]"
  3. Add 10 to the quantity of "bananas" if they exist (use direct value modification)
  4. Remove any item that has 0 quantity
  5. Print the total number of distinct items in the warehouse
  6. Finally, return the updated inventory dictionary

Try it yourself

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

class Program
{
    public static Dictionary<string, int> ManageWarehouse(Dictionary<string, int> inventory)
    {
        // Your code here
        
        return inventory;
    }
    
    static void Main(string[] args)
    {
        Dictionary<string, int> inventory = new Dictionary<string, int>();
        
        // Read first line to check if it's JSON format
        string firstLine = Console.ReadLine();
        
        // Check if input is in JSON format
        if (firstLine != null && firstLine.StartsWith("{") && firstLine.EndsWith("}"))
        {
            try
            {
                // Process JSON input
                string jsonContent = firstLine.Substring(1, firstLine.Length - 2);
                
                // Split by commas that are not inside quotes
                string pattern = @",(?=(?:[^""]*""[^""]*"")*[^""]*$)";
                string[] entries = Regex.Split(jsonContent, pattern);
                
                foreach (string entry in entries)
                {
                    // Extract key and value using regex
                    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}");
        }
    }
}

All lessons in Logic & Flow