Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

التحقق من المخزون

جزء من قسم Logic & Flow في رحلة GO على Coddy — الدرس 46 من 68.

challenge icon

التحدي

سهل

قم بتنفيذ وظيفة فحص المخزون لنظام إدارة المخزون الخاص بك والتي تسترجع كميات المنتجات بأمان وتتعامل مع الحالات التي لا توجد فيها المنتجات. يعتمد هذا التحدي على أساس المخزون من الدرس السابق ويضيف معالجة الأخطاء لاستعلامات المخزون.

ستتلقى مدخلين:

  • سلسلة نصية تحتوي على بيانات المخزون الحالي بالتنسيق "product1:price1:quantity1,product2:price2:quantity2,product3:price3:quantity3" (على سبيل المثال، "Laptop:999.99:5,Mouse:25.50:15,Keyboard:75.00:8")
  • سلسلة نصية تحتوي على طلبات فحص المخزون بالتنسيق "product1,product2,product3" (على سبيل المثال، "Mouse,Tablet,Keyboard")

مهمتك هي:

  1. استخدم نفس الـ Product struct من الدرس السابق مع حقول Price (float64) و Quantity (int)
  2. قم بتحليل المدخل الأول عن طريق التقسيم عند الفواصل للحصول على إدخالات المنتجات الفردية
  3. لكل إدخال منتج، قم بالتقسيم عند النقطتين الرأسيتين للحصول على اسم المنتج، والسعر، والكمية
  4. قم بتحويل سلسلة السعر النصية إلى float64 وسلسلة الكمية النصية إلى int
  5. قم بإنشاء وتعبئة خريطة inventory ببيانات المنتج التي تم تحليلها
  6. قم بإنشاء وظيفة تسمى checkStock تأخذ خريطة المخزون واسم المنتج كمعلمات وترجع (int, error):
    • إذا كان المنتج موجوداً في المخزون، فقم بإرجاع كميته و nil
    • إذا لم يكن المنتج موجوداً، فقم بإرجاع 0 وخطأ مع الرسالة "product not found: [product_name]"
  7. قم بتحليل المدخل الثاني عن طريق التقسيم عند الفواصل للحصول على قائمة المنتجات المراد فحصها
  8. اعرض عنوان فحص المخزون: "Stock Check Results:"
  9. لكل منتج في قائمة الفحص، استدعِ وظيفة checkStock واعرض النتائج:
    • في حالة عدم وجود خطأ: "[product_name]: [quantity] units in stock"
    • في حالة وجود خطأ: "[product_name]: Error - [error_message]"
  10. قم بعدّ وعرض إحصائيات الملخص:
    • "Check Summary:"
    • "Products checked: [total_number_of_products_checked]"
    • "Products found: [number_of_products_that_exist]"
    • "Products not found: [number_of_products_that_dont_exist]"
  11. اعرض إجمالي المخزون للمنتجات التي تم العثور عليها:
    • "Total stock for found products: [sum_of_quantities_for_existing_products] units"
  12. أدرج أي منتجات مفقودة:
    • إذا كانت هناك منتجات مفقودة: "Missing products: [comma_separated_list_of_missing_products]"
    • إذا لم تكن هناك منتجات مفقودة: "All requested products are available"

استخدم حزمة strings لتقسيم السلاسل النصية للمدخلات، وحزمة strconv لتحويل السلاسل النصية إلى أرقام، وحزمة errors لإنشاء رسائل الخطأ، وحزمة fmt للإخراج المنسق. يوضح هذا التحدي كيفية تنفيذ استرجاع البيانات الآمن مع معالجة الأخطاء بشكل صحيح، وهو نمط أساسي في أنظمة إدارة المخزون.

جرّب بنفسك

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

// Define the Product struct
type Product struct {
	Price    float64
	Quantity int
}

func main() {
	// Read input using bufio.Scanner to handle spaces properly
	scanner := bufio.NewScanner(os.Stdin)
	
	scanner.Scan()
	storeInfo := scanner.Text()
	
	scanner.Scan()
	productData := scanner.Text()
	
	// 1. Parse store information (split by comma)
	storeInfoParts := strings.Split(storeInfo, ",")
	storeName := ""
	location := ""
	if len(storeInfoParts) >= 2 {
		storeName = storeInfoParts[0]
		location = storeInfoParts[1]
	}
	
	// 2. Parse product data (split by comma, then by colon for each product)
	productEntries := strings.Split(productData, ",")
	
	// 3. Create inventory map
	inventory := make(map[string]Product)
	
	// 4. Convert strings to appropriate types and populate inventory
	for _, entry := range productEntries {
		parts := strings.Split(entry, ":")
		if len(parts) >= 3 {
			productName := parts[0]
			price, _ := strconv.ParseFloat(parts[1], 64)
			quantity, _ := strconv.Atoi(parts[2])
			
			inventory[productName] = Product{
				Price:    price,
				Quantity: quantity,
			}
		}
	}
	
	// 5. Display store information
	fmt.Printf("=== %s Inventory System ===\n", storeName)
	fmt.Printf("Location: %s\n", location)
	fmt.Printf("Inventory initialized with %d products\n", len(inventory))
	
	// 6. Display current inventory (sorted alphabetically)
	fmt.Println("Current Inventory:")
	var productNames []string
	for name := range inventory {
		productNames = append(productNames, name)
	}
	sort.Strings(productNames)
	
	for _, name := range productNames {
		product := inventory[name]
		fmt.Printf("- %s: $%.2f (Stock: %d)\n", name, product.Price, product.Quantity)
	}
	
	// 7. Calculate and display inventory statistics
	totalProducts := len(inventory)
	totalItems := 0
	totalValue := 0.0
	
	for _, product := range inventory {
		totalItems += product.Quantity
		totalValue += product.Price * float64(product.Quantity)
	}
	
	fmt.Println("Inventory Statistics:")
	fmt.Printf("Total Products: %d\n", totalProducts)
	fmt.Printf("Total Items in Stock: %d\n", totalItems)
	fmt.Printf("Total Inventory Value: $%.2f\n", totalValue)
	
	// 8. Display system status
	fmt.Println("System Status: Ready")
	fmt.Println("Inventory management system initialized successfully")
}

جميع دروس Logic & Flow