package payment; use warnings; use strict; sub new { my $class = shift; my $self = {}; $self->{units} = undef; $self->{bills} = undef; bless $self, $class; } sub change { my $self = shift; @{ $self->{units} } = @_; %{ $self->{bills} } = map { $_ => 0 } @_; } sub add { my $self = shift; my @units = @{ $self->{units} }; my %pieces = %{ $self->{bills} }; my $temp = shift; my $unit; my $i = 0; while ($temp) { $unit = $units[$i]; while ( $temp >= $unit ) { $temp -= $unit; $pieces{$unit} += 1; } last if $i == (@units); $unit = $units[ ++$i ]; } %{ $self->{bills} } = %pieces; } sub get { my $self = shift; return %{ $self->{bills}}; } 1;