File: Money.pm

package info (click to toggle)
libur-perl 0.470%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,192 kB
  • sloc: perl: 61,814; javascript: 255; xml: 108; sh: 13; makefile: 9
file content (43 lines) | stat: -rw-r--r-- 970 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package Vending::Command::Service::Show::Money;
use strict;
use warnings;

class Vending::Command::Service::Show::Money {
    is_abstract => 1,
    is => 'Vending::Command::Service',
    doc => 'parent class for show change and show bank',
    has => [
        location_name => { is => 'String', is_abstract => 1 },
    ],
};

sub execute {
    my $self = shift;

    my $machine = $self->machine();

    my $loc = $machine->machine_locations(name => $self->location_name);
    unless ($loc) {
        $self->error_message("There is no slot named ".$self->location_name);
        return;
    }

    my @coins = $loc->items;

    my %coins_by_type;
    my $total_value = 0;

    foreach my $coin ( @coins ) {
        $coins_by_type{$coin->name}++;
        $total_value += $coin->value_cents;
    }

    while(my($type,$count) = each %coins_by_type) {
        printf("%-7s:%6d\n", $type,$count);
    }
    printf("Total:\t\$%.2f\n",$total_value/100);
    return 1;

}
1;