Course of Raku / Addendum 🆕 / Types and text machines / Objects and classes / Exercises / A bank account
Solution: A bank account
Here is a possible solution to the task.
Code
class Account {
has $!balance = 0;
method deposit($amount) {
$!balance += $amount;
}
method withdraw($amount) {
if $amount > $!balance {
say 'declined: insufficient funds';
}
else {
$!balance -= $amount;
}
}
method balance { $!balance }
}
my $account = Account.new;
$account.deposit(100);
$account.withdraw(30);
$account.withdraw(200);
say $account.balance;🦋 You can find the source code in the file bank-account.raku.
Output
declined: insufficient funds
70Comments
has $!balance = 0declares the attribute private — with the!twigil it has no generated accessor at all, so the only way to change the balance is through the methods the class provides. Amethod balancegives the outside world a way to read it, but no way to set it.This is deliberate. Had we written
has $.balance, the.twigil would add a public accessor — read-only by default, but one carelessis rwon it would expose direct assignment like$account.balance = 1_000_000, letting anyone move money without going throughdepositorwithdraw. Keeping the attribute private rules that out by construction: the balance can only ever change by the account’s own rules, such as the insufficient-funds check inwithdraw.The second withdrawal asks for more than the balance, so it is declined and the balance stays at
70.