Created
November 4, 2009 19:00
-
-
Save deppp/226292 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use MooseX::Declare; | |
class BankAccount { | |
has 'balance' => ( isa => 'Num', is => 'rw', default => 0 ); | |
method deposit (Num $amount) { | |
$self->balance( $self->balance + $amount ); | |
} | |
method withdraw (Num $amount) { | |
my $current_balance = $self->balance(); | |
( $current_balance >= $amount ) | |
|| confess "Account overdrawn"; | |
$self->balance( $current_balance - $amount ); | |
} | |
} | |
class CheckingAccount extends BankAccount { | |
has 'overdraft_account' => ( isa => 'BankAccount', is => 'rw' ); | |
before withdraw (Num $amount) { | |
my $overdraft_amount = $amount - $self->balance(); | |
if ( $self->overdraft_account && $overdraft_amount > 0 ) { | |
$self->overdraft_account->withdraw($overdraft_amount); | |
$self->deposit($overdraft_amount); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment