Guest User

Untitled

a guest
Apr 22nd, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. use MooseX::Declare;
  2.  
  3. class BankAccount {
  4. has 'balance' => ( isa => 'Num', is => 'rw', default => 0 );
  5.  
  6. method deposit (Num $amount) {
  7. $self->balance( $self->balance + $amount );
  8. }
  9.  
  10. method withdraw (Num $amount) {
  11. my $current_balance = $self->balance();
  12. ( $current_balance >= $amount )
  13. || confess "Account overdrawn";
  14. $self->balance( $current_balance - $amount );
  15. }
  16. }
  17.  
  18. class CheckingAccount extends BankAccount {
  19. has 'overdraft_account' => ( isa => 'BankAccount', is => 'rw' );
  20.  
  21. before withdraw (Num $amount) {
  22. my $overdraft_amount = $amount - $self->balance();
  23. if ( $self->overdraft_account && $overdraft_amount > 0 ) {
  24. $self->overdraft_account->withdraw($overdraft_amount);
  25. $self->deposit($overdraft_amount);
  26. }
  27. }
  28. }
Add Comment
Please, Sign In to add comment