6 use Test::More tests => 16;
20 accessor => 'balance',
22 type_constraint => Int(),
26 my ($self, $amount) = @_;
27 $self->balance($self->balance + $amount);
31 my ($self, $amount) = @_;
32 my $current_balance = $self->balance();
33 ($current_balance >= $amount)
34 || confess "Account overdrawn";
35 $self->balance($current_balance - $amount);
38 package CheckingAccount;
43 extends 'BankAccount';
45 has 'overdraft_account' => (
46 accessor => 'overdraft_account',
47 type_constraint => subtype Object => where { $_->isa('BankAccount') },
50 before 'withdraw' => sub {
51 my ($self, $amount) = @_;
52 my $overdraft_amount = $amount - $self->balance();
53 if ($overdraft_amount > 0) {
54 $self->overdraft_account->withdraw($overdraft_amount);
55 $self->deposit($overdraft_amount);
61 my $savings_account = BankAccount->new(balance => 250);
62 isa_ok($savings_account, 'BankAccount');
64 is($savings_account->balance, 250, '... got the right savings balance');
66 $savings_account->withdraw(50);
67 } '... withdrew from savings successfully';
68 is($savings_account->balance, 200, '... got the right savings balance after withdrawl');
70 $savings_account->deposit(150);
71 is($savings_account->balance, 350, '... got the right savings balance after deposit');
73 my $checking_account = CheckingAccount->new(
75 overdraft_account => $savings_account
77 isa_ok($checking_account, 'CheckingAccount');
78 isa_ok($checking_account, 'BankAccount');
80 is($checking_account->overdraft_account, $savings_account, '... got the right overdraft account');
82 is($checking_account->balance, 100, '... got the right checkings balance');
85 $checking_account->withdraw(50);
86 } '... withdrew from checking successfully';
87 is($checking_account->balance, 50, '... got the right checkings balance after withdrawl');
88 is($savings_account->balance, 350, '... got the right savings balance after checking withdrawl (no overdraft)');
91 $checking_account->withdraw(200);
92 } '... withdrew from checking successfully';
93 is($checking_account->balance, 0, '... got the right checkings balance after withdrawl');
94 is($savings_account->balance, 200, '... got the right savings balance after overdraft withdrawl');