Lesson 5, Introduction to Control Structures
Part 4

New Function:  FormatCurrency().  Example:  FormatCurrency(23.4) returns $23.40.  Be very careful with this function though because it changes numbers into text.  Remember, the $ sign is a ASCII (text) symbol so that means when we store $23.40 it is stored as text so we can no longer use it in any math calculations.  Because of that, you only want to use that function when you are using that value to output, not to store for future use.

This next problem is well within your ability to program but it does require some thought.  I recommend that you try to go through the five steps only looking at the answer after each step to check your work.  If you just read my answers you will not be developing your problem solving skills.

You have been asked to write a computer program to figure out the price of pizzas at a restaurant.  They want to reward people who make large orders (orders over $50 get a 10% discount) and recover some of the energy costs of people who live more than 5 miles from the restaurant by charging them a $5 delivery charge.  In addition, they want to give their employees extra incentive to purchase their product by offering them a 10% discount (they don't get a large order discount too though).  Once a clerk figures out the total of an order then they must determine whether a customer lives outside the five mile limit and whether or not the pizza order is for an employee.  The sales tax is 6%.  You must output the initial amount of the order, the deliver charge if any, the discount if any, the pretax amount if there's a discount, the sales tax and finally the total for the order. You will not display any of these categories if the answer is $0.  You need to also format the answers correctly.  Note:  a delivery charge is not taxable.

If the order was $10, not an employee, and lived Initial Order: $10.00
Tax: $0.60
Grand total: $10.60
If the order was $500, not an employee and it was delivered 10 miles away Initial Order: $500.00
Discount: $50.00
Subtotal: $450.00
Tax: $27.00
Post Tax Subtotal: $477.00
Distance Fee: $0.00
Grand total: $477.00

 

First Let's do the problem statement for this: 
Assumptions
Input
Calculations
Output
Answer

 

 

 

 

 

 

 

 

 The want to reward people who make large orders (orders over $50 get a 10% discount) and recover some of the energy costs of people who live more than 5 miles from the restaurant by charging them a $5 deliver charge.  In addition, we want to give our employees extra incentive to purchase our product by offering them a 10% discount (they don't get a large order discount too though).  Once a clerk figures out the total of an order then must determine whether a customer lives outside the five mile limit and whether or not the pizza order is for an employee.  The sales tax is 6%.  You must output the initial amount of the order, the deliver charge if any, the discount if any, the pretax amount if there's a discount, the sales tax and finally the total for the order. You will any of these categories if the answer is $0.  You need to also format the answers correctly.

Problem Statement
Assumptions:
1. Only one order will be analyzed at a time.
2. Take out or pick up orders only.  
3.  Delivery charge can not be taxed
4.  Amount of the order will be calculated outside this problem
5.  Sales tax is a constant = .06
6. Over 50 discount = .10
7.  Employee discount = .10
8.  Delivery fee = 5.00

Input:
1.  The amount of the order
2.  Whether the address is outside the 5 mile limit
3.  Whether the customer is an employee

Calculations
If a customer is also an employee  then
         discount = initial  * employee disount
Else IF the total is greater than 50 then
         discount = initial  * over50 discount
Subtotal = initial - discount
Tax = Subtotal * slaes tax
If outside five mile limit then
    distance fee =  delivery fee
Grandtotal = subtotal + tax + distance fee

Output:
1.  initial amount
2.  If one is offered the discount.
3.  If a discount is applied pretax (subtotal) amount
4.  tax
5. if a delivery fee is applied delivery fee
6. total amount
Now write the human Algorithm  Answer

 

 

 

 

 

 

Human Algorithm

 

1.  Sales tax is a constant = .06
2. Over 50 discount = .10
3.  Employee discount = .10
4.  Delivery fee = 5.00

5.  Input the total amount of the pizza
6.  Input if the customer lives outside the five mile limit for free pizza
We don't ask if they are an employee yet because that question is only asked if the amount isn't over $50

7.  If order is over $50
8.     discount = over 50 discount
else
9.  Input if the customer is an employee        
10   if  the customer is an employee
11.       discount = employee discount
12..  Subtotal = Total order - discount
13.  Tax = Subtotal * salestax
14.  If the customer is outside the five mile limit
15.  delivery = delivery fee

16.  Output initial amount
11.  Output the discount if greater than zero.
12.  Output the pretax amount if the discount is greater than zero
13.  Output the tax
14.  Output delivery fee if greater than zero
15.  Output the total amount

Problem statement

Assumptions

Sales tax is a constant = .06
Over 50 discount = .10
Employee discount = .10
Delivery fee = 5.00

Input:
The amount of the order
Whether the address is outside the 5 mile limit
Whether the customer is an employee

Calculations
If a customer is also an employee  then
         discount = initial  * employee disount
Else IF the total is greater than 50 then
         discount = initial  * over50 discount
Subtotal = initial - discount
Tax = Subtotal * slaes tax
If outside five mile limit then
    distance fee =  delivery fee
Grandtotal = subtotal + tax + distance fee

Output:
1.  initial amount
2.  If one is offered the discount.
3.  If a discount is applied pretax (subtotal) amount
4.  tax
5. if a delivery fee is applied delivery fee
6. total amount

 

Human Algorithm:
1.  Input the total amount of the pizza
2.  Input if the customer is an employee
3.  Input if the customer lives outside the five mile limit for free pizza
4.  If the order is over $50 or the customer is an employee then calculate the discount by multiplying the total amount by .10
5.  If the customer is outside the five mile limit then add $5.00 to the fee.
6.  Calculate tax (Total order - discount) * .06
7.  Output initial amount
8.  Output the discount if greater than zero.
9.  Output the pretax amount if the discount is greater than zero
10.  Output the tax
11.  Output delivery fee if greater than zero
12.  Output the total amount
Now Create the flowchart   Answer

 

 

 

 

 

 

 

 

Flow Chart:


Now Create the VB Script   Answer

 

 

 

VB Script Code:

 

 

Notice I have Option Explicit underneath Script Language.  When we type this in it will give us an error message when we make a typo in our code.  Without it, if we typed "InitialOrder" in the Dim statement then "InitialOrdr" in our code we wouldn't get an error message warning us that we had a problem.  I recommend typing
 Option Explicit under the <script language... tag in all your code.

<Script Language = "VBScript">
Option Explicit ' Lots of typing so turn this on to check your spelling
Dim InitialOrder, EmployeeDiscount, OutsideLimit, Discount, DistanceFee, Tax, GrandTotal, subtotal

InitialOrder = FormatCurrency(Inputbox("What is the initial order amount?"))
EmployeeDiscount = Inputbox("Is the customer an employee? - yes/no")
OutsideLimit = Inputbox("Is the delivery location more than 5 miles? - yes/no")

If InitialOrder => 50 then
   Discount = InitialOrder * .1
ElseIF EmployeeDiscount = "yes" then
   Discount = InitialOrder * .1
End If


subtotal = InitialOrder - Discount
Tax = subtotal * .06


GrandTotal = FormatCurrency(InitialOrder - Discount + tax + distanceFee)

Document.write "Initial Order: " & FormatCurrency(initialorder) & "<br>"
If Discount > 0 then
    Document.write "Discount: " & FormatCurrency(Discount) & "<br>"
    Document.write "Subtotal: " & FormatCurrency((initialorder - discount)) & "<br>"
End if

Document.write "Tax: " & FormatCurrency(Tax) & "<br>"

If outsidelimit = "yes" then
    Document.write "Post Tax Subtotal: " & FormatCurrency(initialorder - discount + tax) & "<br>"
    Document.write "Distance Fee: " & FormatCurrency(DistanceFee) & "<br>"
End If

Document.write "Grand total: " & GrandTotal

</script>