Menu
CoddyTech

Depot: Intake

Lesson 43 of 48 in Coddy's GDScript for Lua Developers course.

challenge icon

Challenge

Easy

Implement add: reject an empty name or negative quantity, otherwise accumulate quantity and return true. Zero is accepted. Inputs are stage, command array, and at stage four a price dictionary. Commands contain an operation (add or take), name, and quantity in that order. The solve function dispatches only operations available at this stage. Preserve all previously implemented operations. Layout and locked main remain unchanged across the project. Complete only the newly required StockDepot method.

Input is one JSON array on stdin. The locked main parses it, calls the public APIs and prints result= followed by the returned text and a newline. No helper should print. Independent fixed probes verify required API behavior before the input case; they do not enforce implementation style.

Your code goes in stock_depot.gd, exercise.gd. The supplied main.gd cannot be edited. Keep each class_name type in its named file.

Try it yourself

extends Node

func contract_ok(data: Array) -> bool:
	var stage: int = int(data[0])
	var depot: StockDepot = StockDepot.new()
	if depot.add("",2) or depot.add("p",-1): return false
	if not depot.add("p",3) or not depot.add("p",2): return false
	if stage >= 2 and depot.report() != "p=5": return false
	if stage >= 3:
		if depot.take("p",6) or depot.take("q",0) or depot.take("p",-1): return false
		if depot.report() != "p=5" or not depot.take("p",2) or depot.report() != "p=3": return false
	if stage >= 4 and depot.worth({"p":4}) != 12: return false
	return true

func _ready() -> void:
	var data: Array = JSON.parse_string(OS.read_string_from_stdin())
	if not contract_ok(data):
		print("API_CONTRACT_FAILED")
		return
	print("result=" + Exercise.solve(data))

All lessons in GDScript for Lua Developers

Practice on your own: Online GDScript compiler