Menu
CoddyTech

Final: Account Transfer

Lesson 48 of 48 in Coddy's GDScript for C++ 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 C++ reference. Save these files together and compile main.cpp with C++20. The headers provide the helper APIs; main reads the same JSON array and prints the same result protocol. Only the standard library is used.

C++ file account.hpp:

#pragma once
#include <cstdint>
struct Account {
    std::int64_t balance;
    explicit Account(std::int64_t initial=0):balance(initial) {}
};

C++ file transfers.hpp:

#pragma once
#include <limits>
#include "account.hpp"
inline bool move_funds(Account *source,Account *target,std::int64_t amount) {
    if(!source || !target || amount<0 || amount>source->balance) return false;
    if(source==target) return true;
    // Preserve the reference's unchecked 64-bit sum without signed overflow.
    auto sum=static_cast<std::uint64_t>(target->balance)+static_cast<std::uint64_t>(amount);
    source->balance-=amount;
    const auto max=std::numeric_limits<std::int64_t>::max();
    target->balance=sum<=static_cast<std::uint64_t>(max)?static_cast<std::int64_t>(sum)
        :-1-static_cast<std::int64_t>(std::numeric_limits<std::uint64_t>::max()-sum);
    return true;
}

C++ file main.cpp:

#include <iostream>
#include <string>
#include <stdexcept>
#include "transfers.hpp"
int main(int argc,char **argv) {
    if(argc>1 && std::string(argv[1])=="--probe") {
        Account a(6);
        if(move_funds(nullptr,&a,0) || move_funds(&a,nullptr,0) ||
           !move_funds(&a,&a,4) || a.balance!=6 ||
           move_funds(&a,&a,7) || a.balance!=6) throw std::runtime_error("contract failure");
        std::cout << "contract-ok\n"; return 0;
    }
    char open,comma1,comma2,close;
    std::int64_t a,b,n;
    if(!(std::cin>>open>>a>>comma1>>b>>comma2>>n>>close) ||
       open!='[' || comma1!=',' || comma2!=',' || close!=']') return 1;
    Account source(a),target(b);
    bool moved=move_funds(&source,&target,n);
    std::cout << "result=" << std::boolalpha << moved << ':' << source.balance << ':' << target.balance << '\n';
}

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 C++ Developers

Practice on your own: Online GDScript compiler