Check Availability
Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 74 of 78.
Challenge
EasyAdd the command check. Keep everything you wrote in the previous steps.
Write the function check_availability(inventory, item) above _ready(). It prints nothing and returns an int:
- the stock of the item, when the item is in
inventory -1when the item is missing. A stock is never negative, so-1can only mean "not found".
In the loop, a line like check Apple:
- prints
Error: Use check ITEMwhen it does not have exactly2parts - otherwise calls
check_availability()and printsItem not foundfor-1, or the item name,:, the stock andin stock
For the commands add Apple 0.5 100, add Banana 0.2 50, update Apple -20, update Banana 30, check Apple, check Banana, check Orange and exit, the output is:
Inventory Management System
Item 'Apple' added successfully.
Item 'Banana' added successfully.
Stock for 'Apple' updated successfully.
Stock for 'Banana' updated successfully.
Apple: 80 in stock
Banana: 80 in stock
Item not found
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 _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]))
else:
print("Unknown command")
All lessons in Logic & Flow
1Strings In Depth
String MethodsSearching in StringsReplace and TrimSplit and JoinRecap - Username Check4Dictionaries Part 2
Nested DictionariesArrays of DictionariesMerging DictionariesCopying DictionariesRecap - Frequency Counter2Advanced Decision Making
Match With VariablesMatch GuardsArray PatternsDictionary PatternsMembership ChecksRecap - Command Parser5Contact Book Application
Display MenuAdd Contact8Student Records Manager
Project OverviewAdd Student3Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysJagged ArraysCommon 2D Array PatternsRecap - Game Board6Sets with Dictionaries
What is a Set?Dictionary as a SetAdding and RemovingUnion and IntersectionSet DifferenceRecap - Remove Duplicates9Advanced Data Aggregation
Summing ValuesFinding Minimum and MaximumCustom SortingRecap - Dictionary SorterPractice on your own: Online GDScript compiler