Menu
CoddyTech
flag Ar iconالعربيةdown icon

تحديث المخزون

جزء من قسم المنطق وتدفّق التنفيذ في رحلة GDScript على Coddy. الدرس 73 من 78.

challenge icon

التحدي

سهل

أضف الأمر update. احتفظ بكل ما كتبته في الخطوات السابقة.

سطر مثل update Apple -20 يضيف -20 إلى مخزون Apple: الكمية الموجبة تعني توريدًا، أما الكمية السالبة فتُخرج عناصر من المخزون.

اكتب الدالة update_stock(inventory, item, quantity) فوق _ready():

  1. إذا لم يكن العنصر موجودًا في inventory، اطبع Error: Item 'Orange' not found. (مع اسم العنصر)
  2. إذا كان المخزون الجديد سيصبح سالبًا، اطبع Error: Insufficient stock for 'Apple'. واحتفظ بالمخزون القديم
  3. وإلا، خزّن المخزون الجديد واطبع Stock for 'Apple' updated successfully.

في الحلقة، أضف فرع elif للأمر update:

  • إذا لم يكن السطر يحتوي على 3 أجزاء بالضبط، اطبع Error: Use update ITEM QUANTITY
  • إذا لم تكن الكمية عددًا صحيحًا، اطبع Error: Quantity must be an integer.
  • وإلا، استدعِ update_stock()

للأوامر add Apple 0.5 100 وadd Banana 0.2 50 وadd Apple 0.6 30 وupdate Apple -20 وupdate Banana 30 وupdate Orange 10 وupdate Apple -90 وshow وexit، يكون الناتج:

Inventory Management System
Item 'Apple' added successfully.
Item 'Banana' added successfully.
Error: Item 'Apple' already exists.
Stock for 'Apple' updated successfully.
Stock for 'Banana' updated successfully.
Error: Item 'Orange' not found.
Error: Insufficient stock for 'Apple'.
{ "Apple": { "price": 0.5, "stock": 80 }, "Banana": { "price": 0.2, "stock": 80 } }
Goodbye!

جرّب بنفسك

extends Node

func add_item(inventory: Dictionary, item: String, price: float, stock: int) -> void:
	if inventory.has(item):
		print("Error: Item '%s' already exists." % item)
		return
	inventory[item] = {"price": price, "stock": stock}
	print("Item '%s' added successfully." % item)

func _ready():
	# المخزون: اسم العنصر -> {"price": float, "stock": int}
	var inventory := {}
	print("Inventory Management System")

	while true:
		var line := OS.read_string_from_stdin()
		var parts := line.split(" ")
		var command := parts[0]

		if command == "exit":
			print("Goodbye!")
			break
		elif command == "show":
			print(inventory)
		elif command == "add":
			if parts.size() != 4:
				print("Error: Use add ITEM PRICE STOCK")
			elif not parts[2].is_valid_float() or not parts[3].is_valid_int():
				print("Error: Price and stock must be numeric.")
			else:
				add_item(inventory, parts[1], float(parts[2]), int(parts[3]))
		else:
			print("Unknown command")

جميع دروس المنطق وتدفّق التنفيذ

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