Files
zc/bank.zc

94 lines
2.4 KiB
Plaintext
Raw Normal View History

2026-02-07 14:04:32 +11:00
#include <string.h>
class(BankAccount, {
Integer money;
String name;
Method(BankAccount, deposit, Integer, Integer amount);
Method(BankAccount, withdraw, Integer, Integer amount);
Method(BankAccount, getAmount, Integer);
Destructor(BankAccount);
});
method(BankAccount, deposit, Integer, Integer amount) {
self->money += amount;
return self->money;
}
method(BankAccount, withdraw, Integer, Integer amount) {
self->money -= amount;
return self->money;
}
method(BankAccount, getAmount, Integer) {
return self->money;
}
destructor(BankAccount) {}
constructor(BankAccount, String name) {
return (BankAccount) {
.money = 10,
.name = name,
.deposit = &__BankAccount_deposit,
.withdraw = &__BankAccount_withdraw,
.getAmount = &__BankAccount_getAmount,
.destructor_fn = &destroyBankAccount
};
}
class(Bank, {
Integer size;
Integer capacity;
BankAccount list accounts;
Method(Bank, addAccount, BankAccount*, BankAccount account);
Method(Bank, getAccount, BankAccount*, String name);
Destructor(Bank);
});
method(Bank, addAccount, BankAccount*, BankAccount account) {
if (self->size plus 1 greaterorequalto self->capacity) {
resize(self->accounts, self->capacity * 2);
}
self->accounts[self->size] = account;
self->size increment;
return &self->accounts[self->size - 1];
}
method(Bank, getAccount, BankAccount*, String name) {
for (Integer i = 0; i lessthan self->size; i increment) {
if (strcmp(name, self->accounts[i].name) equals 0) {
return &self->accounts[i];
}
}
return NULL;
}
destructor(Bank) {
for (Integer i = 0; i lessthan self->size; i increment) {
forget(self->accounts[i]);
}
free(self->accounts);
}
constructor(Bank) {
return (Bank) {
.size = 0,
.capacity = 32,
.accounts = many(BankAccount, 32),
.addAccount = &__Bank_addAccount,
.getAccount = &__Bank_getAccount,
.destructor_fn = &destroyBank
};
}
entry {
Bank myBank = newBank();
myBank.addAccount(&myBank, newBankAccount("Maxwell"));
myBank.addAccount(&myBank, newBankAccount("John Citizen"));
myBank.addAccount(&myBank, newBankAccount("Jane Citizen"));
BankAccount* max = myBank.getAccount(&myBank, "Maxwell");
max->deposit(max, 100);
print(max->getAmount(max));
}