previous page main index next page
 

Example 21

We'll go through a complete example now to make sure we've got the hang of things so far.

A company gets orders from its customers and gives a discount of 10% on orders worth more than $200. Whatever the total comes to, sales tax at 5% has to be added. A typical bill, or invoice, might look like this:

         value     =  220.00
         discount  =   22.00
         subtotal  =  198.00
         sales tax =    9.90
         total     =  207.90

If the value is less than $200 then the discount would, of course, be zero but you would still need to add sales tax.

See your teacher now if you're not sure how this works.

1. Design a Solution

Our first attempt is:

1.  get value of order
2.  calculate total
3.  print invoice

[HINT: already you should see that the main program looks like being made up of three procedures]

Step 1 is straightforward...

1.1  prompt user for value of order
1.2  read in value of order

Step 2 is more complicated...

2.1  if value is greater than $200
2.2  then
2.3     discount is 10% of value
2.4  else
2.5     discount is zero
2.6  subtotal is value - discount
2.7  sales tax is 5% of subtotal
2.8  total is subtotal + sales tax

Step 3 is simply

3.1  print value
3.2  print discount
3.3  print subtotal
3.4  print sales tax
3.5  print total

The complete design, then, looks like this:
 

1.  get value of order
2.  calculate total
3.  print invoice

 

1.1  prompt user for value of order
1.2  read in value of order

2.1  if value is greater than $200
2.2  then
2.3     discount is 10% of value
2.4  else
2.5     discount is zero
2.6  subtotal is value - discount
2.7  sales tax is 5% of subtotal
2.8  total is subtotal + sales tax

3.1  print value
3.2  print discount
3.3  print subtotal
3.4  print sales tax
3.5  print total

 
So if we use get_value, calc_total and invoice for the procedure names, the outline of the program will look like this:
 
PROGRAM orders;      {to print out an invoice}

VAR
  value     : REAL;
  discount  : REAL;
  subtotal  : REAL;
  sales_tax : REAL;
  total     : REAL;

{procedure get_value goes here}

{procedure calc_total goes here}

{procedure invoice goes here}

BEGIN               {main program starts here}
  get_value;
  calc_total;
  invoice
END.

  • type in the outline of the program as above and save it as invoice.pas
  • see your teacher now who will show you how to compile it without getting error messages
  • add the procedure get_value and compile it to make sure there are no mistakes
  • add each of the other two procedures in turn, compiling the program after each one
  • save the complete program and run it

If you've got this far and understood the course material then, once again, you've done well. There's more to learn, of course, but you've done much of the hard work!
 

previous page main index next page
 
© 2001 by Mike Hardy