Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Just another Perl shrine
 
PerlMonks  

Need help on part of homework

by gitarwmn (Beadle)
on Nov 05, 2004 at 21:38 UTC ( #405647=perlquestion: print w/ replies, xml ) Need Help??
gitarwmn has asked for the wisdom of the Perl Monks concerning the following question:


Here was an idea I had to add the quantity to the %grocerytotals hash. Somewhere in here I'm off a bit when I try to print it
foreach my $element (keys%cart){ print "\n$element\n"; $grocerytotals{$count} = $count + $grocerytotals{$coun +t}; print "$grocerytotals{$count} items added to current t +otal"; }

I'm trying to write a program for my class which is a grocery purchasing program. First, its supposed ask the user how much money they can spend at the grocery store. Then, read items they want to purchase and how many of each item. If they enter the same item multiple times, then it should add that to the current total for the item.

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

Comment on Need help on part of homework
Select or Download Code
Re: Need help on part of homework
by ikegami (Pope) on Nov 05, 2004 at 21:52 UTC

    ah, I see the problem. 7 Eggs and 4 Milk gives:
    Milk 11
    Eggs 11

    You want a quantity for each item type, not one total. The simple would be to create my %groceryquantities and use that instead of my $quantity.

Re: Need help on part of homework
by Grygonos (Chaplain) on Nov 05, 2004 at 21:53 UTC

    You need to expand your %grocerytotals hash. It needs to be a HoH. i.e.

    my %grocerytotals( 'Eggs' => {quantity => 0, total => 0});
    That way you can update the value for the quantity key and the total key when they add a new item. Does this make some sense?

    edit or you could just implement the price key in that HoH as well and keep all the datatucked in a %grocery hash.

    my %grocery = ('Eggs' = > {quantity => 3, price => 1.45});
    Then you can just compute the total via the printout, unless you want to store it.. in which case you would just add the total key back to the hash

      I see what you are saying, I'll give it a shot and see what happens. Thanks for your input.
Re: Need help on part of homework
by artist (Parson) on Nov 05, 2004 at 22:45 UTC
    If you are asking for homework help and show your code, make sure you won't have problem about other 'students' in your class copying the code. It might create potential problems for you for grading.
      Good thought but since it doesn't work I didn't think of that.
Re: Need help on part of homework
by zentara (Archbishop) on Nov 06, 2004 at 20:03 UTC
    Just curious, what kind of homework is this? Are you in High School or College? I'm just trying to get an idea of how advanced Perl training is getting in the schools.

    I'm not really a human, but I play one on earth. flash japh
Re: Need help on part of homework
by Anonymous Monk on Nov 06, 2004 at 23:42 UTC

    You need to learn about program structure and data structures. Here is an example. I recommend you don't submit it verbatim unless you understand it properly:

    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} = <STDIN>); 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 hav +e \$%.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 = <STDIN>); $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 %$grocerylis +t; } sub checkout { my $cart = shift; require Data::Dumper; print Data::Dumper::Dumper($cart); exit 0; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://405647]
Approved by skx
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-04-20 17:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls