6 use Test::More tests => 16;
19 has '$.balance' => (accessor => 'balance', default => 0);
22 my ($self, $amount) = @_;
23 $self->balance($self->balance + $amount);
27 my ($self, $amount) = @_;
28 my $current_balance = $self->balance();
29 ($current_balance >= $amount)
30 || confess "Account overdrawn";
31 $self->balance($current_balance - $amount);
34 package CheckingAccount;
39 extends 'BankAccount';
41 has '$.overdraft_account' => (accessor => 'overdraft_account');
43 before 'withdraw' => sub {
44 my ($self, $amount) = @_;
45 my $overdraft_amount = $amount - $self->balance();
46 if ($overdraft_amount > 0) {
47 $self->overdraft_account->withdraw($overdraft_amount);
48 $self->deposit($overdraft_amount);
54 my $savings_account = BankAccount->new(balance => 250);
55 isa_ok($savings_account, 'BankAccount');
57 is($savings_account->balance, 250, '... got the right savings balance');
59 $savings_account->withdraw(50);
60 } '... withdrew from savings successfully';
61 is($savings_account->balance, 200, '... got the right savings balance after withdrawl');
63 $savings_account->deposit(150);
64 is($savings_account->balance, 350, '... got the right savings balance after deposit');
66 my $checking_account = CheckingAccount->new(
68 overdraft_account => $savings_account
70 isa_ok($checking_account, 'CheckingAccount');
71 isa_ok($checking_account, 'BankAccount');
73 is($checking_account->overdraft_account, $savings_account, '... got the right overdraft account');
75 is($checking_account->balance, 100, '... got the right checkings balance');
78 $checking_account->withdraw(50);
79 } '... withdrew from checking successfully';
80 is($checking_account->balance, 50, '... got the right checkings balance after withdrawl');
81 is($savings_account->balance, 350, '... got the right savings balance after checking withdrawl (no overdraft)');
84 $checking_account->withdraw(200);
85 } '... withdrew from checking successfully';
86 is($checking_account->balance, 0, '... got the right checkings balance after withdrawl');
87 is($savings_account->balance, 200, '... got the right savings balance after overdraft withdrawl');