Final: Account Transfer
Lesson 48 of 48 in Coddy's GDScript for Lua Developers course.
Challenge
EasyImplement FundsAccount(initial:int) with balance; static FundsTransfer.move(source:FundsAccount,target:FundsAccount,n:int)->bool. Reject null accounts, negative amount or insufficient source funds without mutation. Otherwise transfer once. Self-transfer succeeds without net change when affordable. The solve function takes [a,b,n] and returns success:a:b. Initial balances are nonnegative.
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 funds_account.gd, funds_transfer.gd, exercise.gd. The supplied main.gd cannot be edited. Keep each class_name type in its named file.
Port this complete Lua reference to the specified GDScript types. The Lua driver reads three integer lines: initial source balance, initial target balance, then transfer amount. The target driver reads the specified JSON array instead. Rejection leaves both accounts unchanged. Self-transfer succeeds without a net change when affordable.
Lua reference file account.lua:
local Account = {}
Account.__index = Account
function Account.new(initial)
if initial == nil then initial = 0 end
return setmetatable({balance = initial}, Account)
end
return Account
Lua reference file transfer.lua:
local Transfer = {}
function Transfer.move(source, target, amount)
if source == nil or target == nil or amount < 0 or amount > source.balance then
return false
end
source.balance = source.balance - amount
target.balance = target.balance + amount
return true
end
return Transfer
Lua reference file main.lua:
local Account = require("account")
local Transfer = require("transfer")
local source = Account.new(assert(tonumber(io.read("*l"))))
local target = Account.new(assert(tonumber(io.read("*l"))))
local amount = assert(tonumber(io.read("*l")))
local ok = Transfer.move(source, target, amount)
print(string.format("result=%s:%d:%d", tostring(ok), source.balance, target.balance))
Try it yourself
extends Node
func contract_ok(data: Array) -> bool:
var a: FundsAccount = FundsAccount.new(6)
var b: FundsAccount = FundsAccount.new(2)
if FundsTransfer.move(null,b,0) or FundsTransfer.move(a,null,0) or FundsTransfer.move(a,b,7) or FundsTransfer.move(a,b,-1): return false
if a.balance != 6 or b.balance != 2: return false
if not FundsTransfer.move(a,a,4) or a.balance != 6: return false
if not FundsTransfer.move(a,b,3) or a.balance != 3 or b.balance != 5: 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
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