my $grocerylist = { Eggs => '1.99', Milk => '1.75', Cookies => '2.99', Carrots => '1.25', Eggplant => '1.89', Cereal => '3.25', Gum => '0.75', Juice => '1.99' }; my $cart = { items => {}, total => 0, cash => 0, }; print "Welcome to the Acme(tm) grocery store.\n"; print "How much do you have to spend today? "; chomp ($cart->{cash} = ); printf "You have: \$%.2f\n", $cart->{cash}; while ( ( $cart->{cash} - $cart->{total} ) > 0) { my $action = menu(); if ( $action eq 'Q') { quit($cart); } elsif ($action eq 'Showall' ) { showall($grocerylist); } elsif ($action eq 'Cart'){ cart($cart); } elsif (exists $grocerylist->{$action}){ add_cart( $cart, $grocerylist, $action ); } else { print "\n*****Don't know how to do $action\n\n"; } } sub add_cart { my ( $cart, $grocerylist, $item ) = @_; if ( ($cart->{total} + $grocerylist->{$item}) > $cart->{cash} ) { printf "\nSorry you can't afford '%s' @ \$%.2f as you only have \$%.2f\n", $item, $grocerylist->{$item}, $cart->{cash}-$cart->{total}; } else { $cart->{items}->{$item}++; $cart->{total} += $grocerylist->{$item}; print "\nAdded $item to cart OK\n"; } } sub menu { print qq! Enter: item name, 'cart' to view your current purchases, 'showall' to show all available items. 'q' to quit and display purchases. Action: !; chomp (my $action = ); $action = ucfirst(lc $action); return $action; } sub quit { my $cart = shift; checkout($cart); } sub cart { my $cart = shift; print "This is Cart so far:\n\n"; printf("%-15s %s\n" .('-'x25) ."\n", "Item", "Quantity"); printf ("%-15s %s\n", $_, $cart->{items}->{$_}) for keys %{$cart->{items}}; printf " Spent \$%.2f Budget: \$%.2f Remaining: \$%.2f\n", $cart->{total}, $cart->{cash}, $cart->{cash} - $cart->{total}; } sub showall { my $grocerylist = shift; printf("%-15s %s\n" . ('-'x25) ."\n", 'Item', 'Price'); printf("%-15s %s\n", $_, $grocerylist->{$_}) for keys %$grocerylist; } sub checkout { my $cart = shift; require Data::Dumper; print Data::Dumper::Dumper($cart); exit 0; }