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

ملخص - فهم المؤشرات

جزء من قسم Fundamentals في رحلة GO على Coddy — الدرس 66 من 109.

challenge icon

التحدي

متوسط

في هذا التحدي، ستقوم ببناء نظام بسيط لإدارة المخزون لمتجر صغير. ستستخدم المؤشرات (pointers) لتحديث كميات المخزون، وحساب القيم، وتتبع التغييرات.

أكمل الوظائف الثلاث أدناه:

  1. updateQuantity(quantity *int, change int) (int, bool)
    قم بتعديل القيمة الموجودة في quantity عن طريق إضافة change (قيمة سالبة للمبيعات، وموجبة لإعادة التعبئة).
    إذا كانت النتيجة ستصبح أقل من 0، فقم بتعيين *quantity إلى 0 بدلاً من ذلك.
    قم بإرجاع الكمية الجديدة وقيمة منطقية (boolean) تكون true إذا أصبح العنصر الآن خارج المخزون (*quantity == 0).
  2. calculateValue(itemName string, quantity int, price float64, mostValuableItem *string, highestValue *float64) float64
    احسب totalValue كـ float64(quantity) * price.
    إذا كانت totalValue أكبر من *highestValue، فقم بتحديث *highestValue وتعيين *mostValuableItem إلى itemName باستخدام المؤشرات.
    أرجع totalValue.
  3. displayInventory(apples, oranges, bananas *int, applePrice, orangePrice, bananaPrice float64)
    اطبع اسم كل عنصر، وكميته (المستخرجة من المؤشر الخاص به)، وقيمته المحسوبة باستخدام fmt.Printf.
    التنسيق: Apples: 10 (Value: $5.00) — استخدم %.2f للأسعار.

المخرجات المتوقعة للمخزون الأولي هي:

Initial Inventory:
Apples: 10 (Value: $5.00)
Oranges: 15 (Value: $10.50)
Bananas: 8 (Value: $2.40)
Total inventory value: $17.90
Most valuable item: Oranges

نصيحة: استخدم *quantity لقراءة أو كتابة القيمة التي يشير إليها المؤشر. على سبيل المثال، *quantity += change يقوم بتعديل المتغير الأصلي مباشرة.

جرّب بنفسك

package main

import "fmt"

// updateQuantity تقوم بتحديث كمية عنصر ما وتعيد الكمية الجديدة
// وما إذا كان العنصر قد نفد من المخزون (الكمية = 0)
func updateQuantity(quantity *int, change int) (int, bool) {
    // TODO: قم بتحديث مؤشر الكمية وإرجاع القيمة الجديدة وحالة نفاد المخزون
    return 0, false
}

// calculateValue تحسب القيمة الإجمالية لعنصر بناءً على الكمية والسعر
// وتقوم أيضاً بتحديث mostValuableItem إذا كان هذا العنصر أكثر قيمة
func calculateValue(itemName string, quantity int, price float64, mostValuableItem *string, highestValue *float64) float64 {
    // TODO: احسب القيمة الإجمالية، وحدث mostValuableItem إذا لزم الأمر، وأرجع القيمة الإجمالية
    return 0.0
}

// displayInventory تطبع تفاصيل المخزون
// تستخدم المؤشرات للسماح للدالة بعرض البيانات في الوقت الفعلي
func displayInventory(apples, oranges, bananas *int, applePrice, orangePrice, bananaPrice float64) {
    fmt.Printf("Apples: %d (Value: $%.2f)\n", *apples, float64(*apples) * applePrice)
    fmt.Printf("Oranges: %d (Value: $%.2f)\n", *oranges, float64(*oranges) * orangePrice)
    fmt.Printf("Bananas: %d (Value: $%.2f)\n", *bananas, float64(*bananas) * bananaPrice)
}

func main() {
    // تهيئة المخزون
    apples := 10
    oranges := 15
    bananas := 8
    
    // الأسعار
    applePrice := 0.5  // $0.50 لكل وحدة
    orangePrice := 0.7 // $0.70 لكل وحدة
    bananaPrice := 0.3 // $0.30 لكل وحدة
    
    // تتبع العنصر الأكثر قيمة
    var mostValuableItem string
    var highestValue float64
    
    // عرض المخزون الأولي
    fmt.Println("Initial Inventory:")
    displayInventory(&apples, &oranges, &bananas, applePrice, orangePrice, bananaPrice)
    
    // حساب القيم الأولية والعثور على العنصر الأكثر قيمة
    appleValue := calculateValue("Apples", apples, applePrice, &mostValuableItem, &highestValue)
    orangeValue := calculateValue("Oranges", oranges, orangePrice, &mostValuableItem, &highestValue)
    bananaValue := calculateValue("Bananas", bananas, bananaPrice, &mostValuableItem, &highestValue)
    
    fmt.Printf("Total inventory value: $%.2f\n", appleValue+orangeValue+bananaValue)
    fmt.Printf("Most valuable item: %s\n\n", mostValuableItem)
    
    // محاكاة بعض المبيعات
    fmt.Println("Processing sales...")
    _, applesOutOfStock := updateQuantity(&apples, -4) // بيع 4 تفاحات
    _, orangesOutOfStock := updateQuantity(&oranges, -8) // بيع 8 برتقالات
    _, bananasOutOfStock := updateQuantity(&bananas, -10) // محاولة بيع 10 موزات (أكثر مما لدينا)
    
    // التحقق مما إذا كان أي من العناصر قد نفد
    if applesOutOfStock {
        fmt.Println("Apples are out of stock!")
    }
    if orangesOutOfStock {
        fmt.Println("Oranges are out of stock!")
    }
    if bananasOutOfStock {
        fmt.Println("Bananas are out of stock!")
    }
    
    // عرض المخزون المحدث
    fmt.Println("\nUpdated Inventory:")
    displayInventory(&apples, &oranges, &bananas, applePrice, orangePrice, bananaPrice)
    
    // إعادة تعيين تتبع العنصر الأكثر قيمة لإعادة الحساب
    mostValuableItem = ""
    highestValue = 0
    
    // إعادة حساب القيم
    appleValue = calculateValue("Apples", apples, applePrice, &mostValuableItem, &highestValue)
    orangeValue = calculateValue("Oranges", oranges, orangePrice, &mostValuableItem, &highestValue)
    bananaValue = calculateValue("Bananas", bananas, bananaPrice, &mostValuableItem, &highestValue)
    
    fmt.Printf("Total inventory value: $%.2f\n", appleValue+orangeValue+bananaValue)
    fmt.Printf("Most valuable item: %s\n\n", mostValuableItem)
    
    // إعادة تزويد المخزون
    fmt.Println("Restocking...")
    updateQuantity(&apples, 5)  // إضافة 5 تفاحات
    updateQuantity(&oranges, 10) // إضافة 10 برتقالات
    updateQuantity(&bananas, 12) // إضافة 12 موزة
    
    // عرض المخزون النهائي
    fmt.Println("\nFinal Inventory:")
    displayInventory(&apples, &oranges, &bananas, applePrice, orangePrice, bananaPrice)
    
    // إعادة تعيين تتبع العنصر الأكثر قيمة للحساب النهائي
    mostValuableItem = ""
    highestValue = 0
    
    // حساب القيمة النهائية
    appleValue = calculateValue("Apples", apples, applePrice, &mostValuableItem, &highestValue)
    orangeValue = calculateValue("Oranges", oranges, orangePrice, &mostValuableItem, &highestValue)
    bananaValue = calculateValue("Bananas", bananas, bananaPrice, &mostValuableItem, &highestValue)
    
    fmt.Printf("Total inventory value: $%.2f\n", appleValue+orangeValue+bananaValue)
    fmt.Printf("Most valuable item: %s\n", mostValuableItem)
}

جميع دروس Fundamentals