Invoice

February 17, 2015

Today’s exercise comes from a text for a first-level programming course.

          Praxis Grocery Store
               17 Feb 2015

1  2% Milk               2  3.30    6.60
2  93% Ground Beef       1  5.98    5.98
3  Clam Chowder          2  1.78    3.56
4  Honey                 1  4.42    4.42
5  6 Eggs                1  1.18    1.18
   Subtotal                        21.74
   Tax 5.25%                        1.14
   Total                           22.88

Your task is to write a program that prompts the user to enter item descriptions, quantities and amounts and produces an invoice as shown above. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2 3

One Response to “Invoice”

  1. use strict;
    use Date::Format qw(time2str);
    my $c = 0;
    my $t = 0;
    my @Q;
    while(1) {
      print STDERR "Product > "; my $p = <STDIN>; chomp $p;
      last unless $p =~ /\S/;
      print STDERR "Quantity> "; my $q = <STDIN>; chomp $q;
      print STDERR "Price   > "; my $a = <STDIN>; chomp $a;
      $c++;
      $t+=$q*$a;
      push @Q, [$c,$p,$q,$a,$q*$a];
    }
    printf '
               Praxis Grocery Store
                   %s
    %s
       Subtotal                          %7.2f
       Tax 5.25%%                         %7.2f
       Total                             %7.2f
    ',
     time2str( "%e %b %Y", time ),
     (join q(), map { sprintf "\n%2d %-20s %2d  %7.2f  %7.2f", @{$_} } @Q ),
     $t, $t*0.0525, $t*1.0525;
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: