<<

PROBLEM: Create a program which will accept the price of a number of items. • You must state the number of items in the basket. • The program will then accept the unit price and the quantity for each item. • It must then calculate the item prices and the total price. • The program must also calculate and add a 5% tax. • If the total price exceeds $450, a discount of $15 is given to the customer; else he/she pays the full amount. • Display the total price, tax, discounted total if applicable, as well as the total number of items bought. • A thank you message must appear at the bottom.

SOLUTION First establish your inputs and your outputs, and then develop your IPO chart. Remember: this phase is called - Analysis Of The Problem. So what are your inputs & output? :

Variable names:

Number of items (input & output) Num (Assume there are 4 items)

Unit price of each item (input) Price1, Price2, Price3, Price4

Quantity of each item (Input) Quan1, Quan2, Quan3, Quan4

Total price (output) TotPrice

Tax amount (output) Tax

Discounted total price (output) DiscTotPrice

In your IPO chart, you will describe the logical steps to move from your inputs to your outputs.

INPUT PROCESSING OUTPUT

Price1, Price2, 1. Get the number of items TotPrice, Tax, Price3, Price4, 2. Get the price and quantity of the each item DiscTaxedTotal, Num 3. Calculate a sub-total for each item TaxedTotal Quan1, Quan2, 4. Calculate the overall total Num Quan3, Quan4 5. Calculate the tax payable {5% of total} 6. If TaxedTotal > 450 then Apply discount {Total minus 15} Display: TotPrice, Tax, DiscTaxedTotal, TaxedTotal, Num Else TaxedTotal is paid Display: TotPrice, Tax, TaxedTotal, Num

Note that there are some variables that are neither input nor output. These variable are either constants which are used in your calculations (TaxRate & Discount), or necessary resulting variables which are not displayed, since they were are not required to be shown (ItemTotalN).

We now convert the IPO chart to pseudocode (i.e. write an ). In other words, we will model our program from our analysis. After that, there is the testing phase (which we will bypass for this exercise), and finally the actual program code in Pascal . ALGORITHM (in pseudocode) {Note: anything written in braces {}is a comment and not part of the program}

Start {absolutely necessary key word} TaxRate ² 0.05 {built-in constant} Discount ² 15 {built-in constant} Prompt: “Please enter the number of Items” {ask user to type a number} Get Num {store the number in a variable called Num} Prompt: “Please enter the unit price and quantity for Item 1" Get Price1, Quan1 {store the 1st entry in Price1 and the 2nd in Quan1} Prompt: “Please enter the unit price and quantity for Item 2" Get Price1, Quan2 {store the 1st entry in Price2 and the 2nd in Quan2} Prompt: “Please enter the unit price and quantity for Item 3" Get Price1, Quan3 {store the 1st entry in Price3 and the 2nd in Quan3} Prompt: “Please enter the unit price and quantity for Item 4" Get Price1, Quan4 {store the 1st entry in Price4 and the 2nd in Quan4}

Calc: ItemTotal1 ² Price1 x Quan1 {calculate total for item 1} Calc: ItemTotal2 ² Price2 x Quan2 {calculate total for item 2} Calc: ItemTotal3 ² Price3 x Quan3 {calculate total for item 3} Calc: ItemTotal4 ² Price4 x Quan4 {calculate total for item 4} Calc: TotPrice ² ItemTotal1+ItemTotal2+ItemTotal3+ ItemTotal4 {calcluate total price} Calc: Tax ² TotPrice x TaxRate {claculate tax payable} Calc: TaxedTotal ² TotPrice + Tax {add tax to total price}

If TaxedTotal > 450 then {if statement to decide whether discount is applicable} DiscTaxedTotal ² TaxedTotal - Discount {calculate discounted total} Print: TotPrice, Tax, DiscTaxedTotal, TaxedTotal, Num {display required data} Else Print: TotPrice, Tax, TaxedTotal, Num {end if statement} Print: “Thank you, have a good day” Stop

-----**** Next page: the actual code in Pascal programming language ***---- PROGRAM PriceCalc; {A program to calculate the total cost of items} {Author: Howard D Jones} {Date: 2012 - 05 - 24}

Const TaxRate = 0.05; Discount = 15; Var Price1, Price2, Price3, Price4 : real; Var ItemTotal1, ItemTotal2, ItemTotal3, ItemTotal4 : real; Var Num, Quan1, Quan2, Quan3, Quan4 : integer; Var TotPrice, Tax, DiscTaxedTotal, TaxedTotal : real;

Begin Price1:=0.0; Price2:=0.0; Price3:=0.0; Price4:=0.0; ItemTotal1:=0.0; ItemTotal2:=0.0; ItemTotal3:=0.0; ItemTotal4:=0.0; Num:=0; Quan1:=0; Quan2:=0; Quan3:=0; Quan4:=0; TotPrice:=0.0; DiscTaxedTotal:=0.0; TaxedTotal:=0.0;

Write('Please enter the number of Items: '); Readln(Num); Write('Please enter the unit price and quantity for Item 1: '); Readln(Price1, Quan1); Write('Please enter the unit price and quantity for Item 2: '); Readln(Price2, Quan2); Write('Please enter the unit price and quantity for Item 3: '); Readln(Price3, Quan3); Write('Please enter the unit price and quantity for Item 4: '); Readln(Price4, Quan4);

ItemTotal1:= Price1 * Quan1; {calculate total for item 1} ItemTotal2:= Price2 * Quan2; {calculate total for item 2} ItemTotal3:= Price3 * Quan3; {calculate total for item 3} ItemTotal4:= Price4 * Quan4; {calculate total for item 4} TotPrice:= ItemTotal1+ItemTotal2+ItemTotal3+ ItemTotal4; {calcluate total price} Tax:= TotPrice * TaxRate; {claculate tax payable} TaxedTotal:= TotPrice + Tax;

If TaxedTotal > 450 then Begin DiscTaxedTotal:= TaxedTotal - Discount; Writeln(' '); Writeln('No. of items: ',Num); Writeln('Total Price: $',TotPrice:5:2); Writeln('Tax amount: $',Tax:5:2); Writeln('Taxed total: $',TaxedTotal:5:2); Writeln('Discounted Total: $',DiscTaxedTotal:5:2); End Else Begin Writeln(' '); Writeln('No. of items: ',Num); Writeln('Total Price: $',TotPrice:5:2); Writeln('Tax amount: $',Tax:5:2); Writeln('Taxed total: $',TaxedTotal:5:2); Writeln('No Discount today'); End; Writeln (' '); Writeln('Thank you, have a good day!');

End.