Depot: Intake
Lesson 43 of 48 in Coddy's GDScript for C# Developers course.
Challenge
EasyImplement add: reject 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 are [add or take,name,quantity]. Solve dispatches only operations available at this stage. 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. main.gd holds the supplied input/output code and 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 C# Developers
1Scripts and Scalar Values
Run Script FilesText and CommentsBindings and TypesInteger ArithmeticBoolean ResultsRecap: Stock Label2Flow and Functions
Branches and ValuesPattern MatchingLoops and RangesTyped FunctionsDefault ArgumentsRecap: Command CountPractice on your own: Online GDScript compiler