Menu
CoddyTech

Final: Account Transfer

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

challenge icon

Challenge

Easy

Implement 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. Solve takes [a,b,n] and returns success:a:b. Initial balances 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.

Complete Go reference. Save the listed files in one directory and run all .go files together. The main package reads the same JSON array and prints the same result protocol. Only standard-library packages are used.

Go file account.go:

package main
type Account struct { Balance int64 }
func NewAccount(initial int64) *Account { return &Account{Balance:initial} }

Go file transfers.go:

package main
func move(source,target *Account,amount int64) bool {
    if source==nil || target==nil || amount<0 || amount>source.Balance { return false }
    source.Balance-=amount
    target.Balance+=amount
    return true
}

Go file main.go:

package main
import ("encoding/json"; "fmt"; "os")
func main() {
    if len(os.Args)>1 && os.Args[1]=="--probe" {
        a:=NewAccount(6)
        if move(nil,a,0) || move(a,nil,0) || !move(a,a,4) || a.Balance!=6 || move(a,a,7) || a.Balance!=6 { panic("contract failure") }
        fmt.Println("contract-ok")
        return
    }
    var values []int64
    if err:=json.NewDecoder(os.Stdin).Decode(&values); err!=nil { panic(err) }
    source,target:=NewAccount(values[0]),NewAccount(values[1])
    moved:=move(source,target,values[2])
    fmt.Printf("result=%t:%d:%d\n",moved,source.Balance,target.Balance)
}

Your code goes in funds_account.gd, funds_transfer.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 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 Go Developers

Practice on your own: Online GDScript compiler