Menu
CoddyTech

Tạo báo cáo

Thuộc phần Logic và luồng chương trình trong hành trình GDScript của Coddy. Bài học 75 trên 78.

challenge icon

Thử thách

Khó

Cuối cùng, hãy thêm lệnh sell, lệnh này bán nhiều mặt hàng cùng lúc và báo cáo doanh thu. Giữ lại mọi thứ bạn đã viết trong các bước trước.

Một dòng như sell Apple:30 Banana:20 sẽ bán 30 quả táo và 20 quả chuối. Mỗi phần sau sell gồm tên mặt hàng, dấu hai chấm và số lượng; mỗi mặt hàng chỉ xuất hiện tối đa một lần trong mỗi dòng.

Viết hàm sales_report(inventory, sales) phía trên _ready(). sales là một từ điển ánh xạ tên mỗi mặt hàng với số lượng đã bán. Với từng mặt hàng, theo thứ tự:

  1. Nếu mặt hàng không có trong inventory, in ra Error: Item 'Orange' not found. rồi chuyển sang mặt hàng tiếp theo
  2. Nếu lượng hàng tồn kho ít hơn số lượng cần bán, in ra Error: Insufficient stock for 'Apple'. rồi chuyển sang mặt hàng tiếp theo
  3. Nếu không, trừ số lượng đó khỏi lượng hàng tồn kho và cộng số lượng nhân với giá vào doanh thu

Hàm trả về văn bản Total revenue: $, theo sau là doanh thu với hai chữ số thập phân.

Trong vòng lặp, nhánh sell tạo từ điển sales từ các phần sau lệnh. Nếu một phần không đúng định dạng gồm tên và số nguyên, được phân tách bằng :, hãy in ra Error: Invalid sale 'Apple-3'. (kèm phần đó) rồi bỏ qua phần này. Sau đó, in ra giá trị mà sales_report() trả về.

Với các lệnh add Apple 0.5 50, add Banana 0.2 60, sell Apple:30 Banana:20 Orange:10, show và exit, đầu ra là:

Inventory Management System
Item 'Apple' added successfully.
Item 'Banana' added successfully.
Error: Item 'Orange' not found.
Total revenue: $19.00
{ "Apple": { "price": 0.5, "stock": 20 }, "Banana": { "price": 0.2, "stock": 40 } }
Goodbye!

Tự làm thử

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 update_stock(inventory: Dictionary, item: String, quantity: int) -> void:
	if not inventory.has(item):
		print("Error: Item '%s' not found." % item)
		return
	var new_stock: int = inventory[item]["stock"] + quantity
	if new_stock < 0:
		print("Error: Insufficient stock for '%s'." % item)
		return
	inventory[item]["stock"] = new_stock
	print("Stock for '%s' updated successfully." % item)

func check_availability(inventory: Dictionary, item: String) -> int:
	if not inventory.has(item):
		return -1
	return inventory[item]["stock"]

func _ready():
	# inventory: tên mặt hàng -> {"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]))
		elif command == "update":
			if parts.size() != 3:
				print("Error: Use update ITEM QUANTITY")
			elif not parts[2].is_valid_int():
				print("Error: Quantity must be an integer.")
			else:
				update_stock(inventory, parts[1], int(parts[2]))
		elif command == "check":
			if parts.size() != 2:
				print("Error: Use check ITEM")
			else:
				var stock := check_availability(inventory, parts[1])
				if stock == -1:
					print("Item not found")
				else:
					print(parts[1], ": ", stock, " in stock")
		else:
			print("Unknown command")

Tất cả bài học trong Logic và luồng chương trình

Tự luyện tập: Trình biên dịch GDScript online