Final: Account Transfer
Lesson 48 of 48 in Coddy's GDScript for Java 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. 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 Java reference. Compile the listed .java files together with Java 17, then run Main. The program reads the same JSON array and prints the same result protocol using only standard-library classes.
Java file Account.java:
public class Account {
public long balance;
public Account(long initial) { balance=initial; }
public Account() { this(0); }
}
Java file Transfers.java:
public class Transfers {
public static boolean move(Account source,Account target,long amount) {
if(source==null || target==null || amount<0 || amount>source.balance) return false;
source.balance-=amount;
target.balance+=amount;
return true;
}
}
Java file Main.java:
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) throws IOException {
if(args.length>0 && args[0].equals("--probe")) {
Account a=new Account(6);
if(Transfers.move(null,a,0) || Transfers.move(a,null,0) ||
!Transfers.move(a,a,4) || a.balance!=6 ||
Transfers.move(a,a,7) || a.balance!=6) throw new IllegalStateException("contract failure");
System.out.println("contract-ok"); return;
}
String text=new String(System.in.readAllBytes(),StandardCharsets.UTF_8).strip();
if(!text.startsWith("[") || !text.endsWith("]")) throw new IllegalArgumentException("expected array");
String[] values=text.substring(1,text.length()-1).split(",",-1);
if(values.length!=3) throw new IllegalArgumentException("expected three integers");
Account source=new Account(Long.parseLong(values[0].strip()));
Account target=new Account(Long.parseLong(values[1].strip()));
long amount=Long.parseLong(values[2].strip());
boolean moved=Transfers.move(source,target,amount);
System.out.println("result="+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 Java 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