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

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

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

challenge icon

التحدي

متوسط

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

أكمل الدوال الثلاث التالية:

  1. updateQuantity(quantity *int, change int) (int, bool)
    عدّل القيمة الموجودة في quantity بإضافة change (قيمة سالبة للمبيعات، وقيمة موجبة لإعادة التخزين).
    إذا كانت النتيجة ستصبح أقل من 0، فاضبط *quantity على 0 بدلًا من ذلك.
    أعد الكمية الجديدة وقيمة منطقية تكون 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 يحدّث كمية عنصر ويعيد الكمية الجديدة
// وما إذا كان العنصر نافد المخزون (quantity = 0)
func updateQuantity(quantity *int, change int) (int, bool) {
    // TODO: حدّث مؤشر quantity وأعد القيمة الجديدة وحالة نفاد المخزون
    return 0, false
}

// calculateValue يحسب القيمة الإجمالية لعنصر بناءً على الكمية والسعر
// It also updates the mostValuableItem if this item is more valuable
func calculateValue(itemName string, quantity int, price float64, mostValuableItem *string, highestValue *float64) float64 {
    // TODO: Calculate the total value, update mostValuableItem if needed, and return the total value
    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 لكل واحد
    
    // Track the most valuable item
    var mostValuableItem string
    var highestValue float64
    
    // Display initial inventory
    fmt.Println("Initial Inventory:")
    displayInventory(&apples, &oranges, &bananas, applePrice, orangePrice, bananaPrice)
    
    // Calculate initial values and find most valuable item
    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 موزات (أكثر مما لدينا)
    
    // Check if any items are out of stock
    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!")
    }
    
    // Display updated inventory
    fmt.Println("\nUpdated Inventory:")
    displayInventory(&apples, &oranges, &bananas, applePrice, orangePrice, bananaPrice)
    
    // Reset most valuable tracking for recalculation
    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 موزة
    
    // Display final inventory
    fmt.Println("\nFinal Inventory:")
    displayInventory(&apples, &oranges, &bananas, applePrice, orangePrice, bananaPrice)
    
    // Reset most valuable tracking for final calculation
    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)
}

جميع دروس الأساسيات

تدرّب بنفسك: مترجم Go عبر الإنترنت