Menu
CoddyTech

Generate Report

Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 75 of 78.

challenge icon

Challenge

Hard

Last, add the command sell, which sells several items at once and reports the revenue. Keep everything you wrote in the previous steps.

A line like sell Apple:30 Banana:20 sells 30 apples and 20 bananas. Each part after sell is an item name, a colon and a quantity, and an item appears at most once per line.

Write the function sales_report(inventory, sales) above _ready(). sales is a dictionary that maps each item name to the quantity sold. For each item, in order:

  1. If the item is not in inventory, print Error: Item 'Orange' not found. and go on with the next item
  2. If its stock is smaller than the quantity, print Error: Insufficient stock for 'Apple'. and go on with the next item
  3. Otherwise, take the quantity out of the stock and add the quantity times the price to the revenue

The function returns the text Total revenue: $ followed by the revenue with two decimals.

In the loop, the sell branch builds the sales dictionary from the parts after the command. A part that is not exactly a name and a whole number separated by : prints Error: Invalid sale 'Apple-3'. (with that part) and is skipped. Then print what sales_report() returns.

For the commands add Apple 0.5 50, add Banana 0.2 60, sell Apple:30 Banana:20 Orange:10, show and exit, the output is:

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!

Try it yourself

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():
	# The inventory: item name -> {"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")

All lessons in Logic & Flow

Practice on your own: Online GDScript compiler