#!/usr/local/bin/perl use strict; 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 %grocerytotals = (Eggs => '0', Milk => '0', Cookies => '0', Carrots => '0', Eggplant => '0', Cereal => '0', Gum => '0', Juice => '0'); my %cart = (); my $action; my $cash; my $count; my $quantity = 0; my $total; my $temp; my $newcount; print "Welcome to the Acme(tm) grocery store.\n"; print "How much do you have to spend today?\n"; chomp ($cash = ); printf("%-1s \$%.2f\n", "You have:", "$cash\n"); print "\nEnter an item name, 'cart' to view your current purchases,\n" . "or 'showall' to show all available items.\n"; chomp ($action = ucfirst); while ($action){ if ($action eq 'Showall'){ #no code written here yet }elsif ($action eq 'Cart'){ my $purchase; print "-" x 25; printf("%-15s %s\n", "\nItem", "Quantity"); foreach my $element (keys%cart){ if ($cart{Eggs}){ printf ("%-15s %s\n", "\n$element", "$grocerytotals{0}", ); } } # show how much money is left printf("\nCash left \$$cash\n"); print "\nEnter an item name, 'cart' to view your current purchases,\n" . "or 'showall' to show all available items.\n"; chomp ($action = ucfirst); if ($action eq 'Q'){ &quit(); } next; }elsif ($action ne 'Showall' or $action ne 'Cart'){ if ($action eq 'Q'){ &quit(); } my $purchase = $action; print "How many?\n"; chomp ($count = ); #validate item if (exists $grocerylist{$purchase}){ $cart{$purchase} = $grocerylist{$purchase}; printf("%-15s %s\n", "You purchased $purchase", $grocerylist{$purchase}); }else{ print "Sorry we don't stock $purchase! Please choose an item from the list\n"; chomp ($purchase = ucfirst); if ($purchase eq 'Q'){&quit(); } } $quantity = $count + $quantity; $grocerytotals{0} = $quantity; print "adding $grocerytotals{0}\n"; #still need to calculate if we have enough to buy more $cash -= ($grocerylist{$purchase} * $count); print "\nEnter an item name, 'cart' to view your current purchases,\n" . "or 'showall' to show all available items.\n"; chomp ($action = ucfirst); if ($action eq 'q'){ &quit(); } next; } }