gitarwmn has asked for the wisdom of the Perl Monks concerning the following question:
foreach my $element (keys%cart){ print "\n$element\n"; $grocerytotals{$count} = $count + $grocerytotals{$coun +t}; print "$grocerytotals{$count} items added to current t +otal"; }
My problem is that I can't figure out how get it to add the item to show how many items they have purchesed and list that in the output. Since the user can add an item multiple times I need to take that into consideration as well. If anyone can tell me how to go about doing this with out giving me the out right answere (since I need to learn this stuff), I would be very gratefull.
Thanks#!/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 = <STDIN>); 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<STDIN>); 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 purch +ases,\n" . "or 'showall' to show all available items.\n"; chomp ($action = ucfirst<STDIN>); 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 = <STDIN>); #validate item if (exists $grocerylist{$purchase}){ $cart{$purchase} = $grocerylist{$purchase}; printf("%-15s %s\n", "You purchased $purchase", $groceryli +st{$purchase}); }else{ print "Sorry we don't stock $purchase! Please choose an i +tem from the list\n"; chomp ($purchase = ucfirst<STDIN>); 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 purch +ases,\n" . "or 'showall' to show all available items.\n"; chomp ($action = ucfirst<STDIN>); if ($action eq 'q'){ &quit(); } next; } }
Janitored by Arunbear - added readmore tags, as per Monastery guidelines
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Need help on part of homework
by Grygonos (Chaplain) on Nov 05, 2004 at 21:53 UTC | |
by gitarwmn (Beadle) on Nov 06, 2004 at 00:06 UTC | |
|
Re: Need help on part of homework
by ikegami (Patriarch) on Nov 05, 2004 at 21:52 UTC | |
|
Re: Need help on part of homework
by artist (Parson) on Nov 05, 2004 at 22:45 UTC | |
by gitarwmn (Beadle) on Nov 05, 2004 at 23:55 UTC | |
|
Re: Need help on part of homework
by Anonymous Monk on Nov 06, 2004 at 23:42 UTC | |
|
Re: Need help on part of homework
by zentara (Cardinal) on Nov 06, 2004 at 20:03 UTC |