// Stateful Actor Example - Bank Account
// Demonstrates complex state management in actors
actor BankAccount {
account_number: String,
balance: i32,
transaction_count: i32,
receive deposit(amount: i32) {
if amount > 0 {
self.balance = self.balance + amount;
self.transaction_count = self.transaction_count + 1;
println("Deposited " + amount.to_string() + ". New balance: " + self.balance.to_string())
} else {
println("Invalid deposit amount: " + amount.to_string())
}
}
receive withdraw(amount: i32) {
if amount > 0 && amount <= self.balance {
self.balance = self.balance - amount;
self.transaction_count = self.transaction_count + 1;
println("Withdrew " + amount.to_string() + ". New balance: " + self.balance.to_string())
} else {
println("Invalid withdrawal: insufficient funds or invalid amount")
}
}
receive get_balance() -> i32 {
self.balance
}
receive get_statement() -> String {
"Account: " + self.account_number +
", Balance: " + self.balance.to_string() +
", Transactions: " + self.transaction_count.to_string()
}
}