6 use Test::More tests => 16;
17 has '$.balance' => (accessor => 'balance', default => 0);
20 my ($self, $amount) = @_;
21 $self->balance($self->balance + $amount);
25 my ($self, $amount) = @_;
26 my $current_balance = $self->balance();
27 ($current_balance >= $amount)
28 || confess "Account overdrawn";
29 $self->balance($current_balance - $amount);
32 package CheckingAccount;
35 use base 'BankAccount';
37 has '$.overdraft_account' => (accessor => 'overdraft_account');
39 before 'withdraw' => sub {
40 my ($self, $amount) = @_;
41 my $overdraft_amount = $amount - $self->balance();
42 if ($overdraft_amount > 0) {
43 $self->overdraft_account->withdraw($overdraft_amount);
44 $self->deposit($overdraft_amount);
50 my $savings_account = BankAccount->new(balance => 250);
51 isa_ok($savings_account, 'BankAccount');
53 is($savings_account->balance, 250, '... got the right savings balance');
55 $savings_account->withdraw(50);
56 } '... withdrew from savings successfully';
57 is($savings_account->balance, 200, '... got the right savings balance after withdrawl');
59 $savings_account->deposit(150);
60 is($savings_account->balance, 350, '... got the right savings balance after deposit');
62 my $checking_account = CheckingAccount->new(
64 overdraft_account => $savings_account
66 isa_ok($checking_account, 'CheckingAccount');
67 isa_ok($checking_account, 'BankAccount');
69 is($checking_account->overdraft_account, $savings_account, '... got the right overdraft account');
71 is($checking_account->balance, 100, '... got the right checkings balance');
74 $checking_account->withdraw(50);
75 } '... withdrew from checking successfully';
76 is($checking_account->balance, 50, '... got the right checkings balance after withdrawl');
77 is($savings_account->balance, 350, '... got the right savings balance after checking withdrawl (no overdraft)');
80 $checking_account->withdraw(200);
81 } '... withdrew from checking successfully';
82 is($checking_account->balance, 0, '... got the right checkings balance after withdrawl');
83 is($savings_account->balance, 200, '... got the right savings balance after overdraft withdrawl');