Menu
Coddy logo textTech

סיכום – ניהול מחסן

חלק מהיחידה לוגיקה וזרימת תוכנית במסלול ה-C# של Coddy. שיעור 54 מתוך 66.

challenge icon

אתגר

בינוני

צרו מערכת לניהול מלאי של מחסן באמצעות מילונים. המשימה שלכם היא לממש את המתודה ManageWarehouse, שמבצעת את הפעולות הבאות:

  1. תחילה, הדפיסו את כל הפריטים ואת הכמויות שלהם בפורמט: "Item: [item], Quantity: [quantity]"
  2. בדקו אם "apples" נמצא במלאי והדפיסו: "Apples in stock: [True/False]"
  3. הוסיפו 10 לכמות של "bananas" אם הם נמצאים במלאי (השתמשו בשינוי ישיר של הערך)
  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)
                {
                    // חלצו מפתח וערך באמצעות 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}");
        }
    }
}

כל השיעורים ביחידה לוגיקה וזרימת תוכנית

תרגלו בעצמכם: קומפיילר C# אונליין