This is the receipt that your till program will print by the end of this lesson:
Mrs. Sri's Stall
--------------------
Fried rice x 1 = 15
Iced tea x 2 = 8
--------------------
Total: 23
Paid: 50
Change: 27
On that receipt, the customer orders one fried rice and two iced teas, then pays $50. The program that prints the receipt reads the orders one at a time, finds the price of each order on the menu, adds everything up, then calculates the change.
There are no new commands in this lesson. You already know lists, searching, loops, functions, procedures and IF. Only the job is new: putting those pieces together into one complete program, like a real till program.
The plan first
The Breaking a Problem Down lesson taught: write the steps first, the code later. Here is the plan for the till program:
- Keep the menu names and their prices.
- Print the top of the receipt.
- Read the orders one at a time until the customer says
done. For each order: find its price, print its receipt line, add it to the total. - Print the total.
- Read the money paid, then print the change, or how much is missing.
You have done each of those steps before in another lesson. The next sections go through them one at a time, each with a small program that you can run on its own.
The menu and its prices
The menu is kept in two lists that come in pairs, as in the Searching lesson: the names in the list menu, their prices in the list prices, in the same order. To find the price of a menu item, first find its item number in menu with the function findIndex. The result is below the code:
FUNCTION findIndex(list, name)
FOR i ← 0 TO LENGTH(list) - 1
IF list[i] = name THEN
RETURN i
END IF
END FOR
RETURN -1
END FUNCTION
menu ← ["Fried rice", "Chicken noodles", "Iced tea", "Coffee"]
prices ← [15, 12, 4, 5]
index ← findIndex(menu, "Iced tea")
PRINT "Iced tea", prices[index]
PRINT "Meatballs", findIndex(menu, "Meatballs")
Iced tea 4
Meatballs -1
Iced tea is at item number 2, so index holds 2 and its price is prices[2], which is 4. Meatballs are not on the menu, so the result is -1. The list menu is sent to the function through the parameter list, because a function cannot use variables from outside.
Reading orders until "done"
The number of orders is not known at the start. So the pattern from the While Loops lesson is used again: READ once before the loop, then READ again on the last line of the loop's body. Each order is two lines of input: the name of the menu item, then the count. With the input Iced tea, 2, Coffee, 1, done:
READ name
WHILE name ≠ "done" DO
READ count
PRINT "Order:", name, count
READ name
END WHILE
PRINT "Orders closed"
Order: Iced tea 2
Order: Coffee 1
Orders closed
In the While Loops lesson, the stop sign was the number 0. In this program a name is read, so the stop sign is the word done, which can never be the name of a menu item.
Receipt lines and the total
Each order becomes one receipt line: the name, the count and its subtotal, which is the price times the count. total starts at 0, then the subtotal of each order is added to it. The dividing line is printed by the divider procedure from the Procedures lesson. This small program has no loop yet, so its two orders are written out directly:
PROCEDURE divider()
PRINT "--------------------"
END PROCEDURE
total ← 0
divider()
subtotal ← 4 * 2
PRINT "Iced tea", "x", 2, "=", subtotal
total ← total + subtotal
subtotal ← 5 * 1
PRINT "Coffee", "x", 1, "=", subtotal
total ← total + subtotal
divider()
PRINT "Total:", total
--------------------
Iced tea x 2 = 8
Coffee x 1 = 5
--------------------
Total: 13
Each order takes three lines: calculate subtotal, print the receipt line, and add to total. In the complete program, those three lines are inside the WHILE, with index from findIndex, the price from prices[index], and the count from READ. The line total ← 0 stays outside the loop.
Paying and change
Exact money must be accepted too, so the condition uses >=, not >. With the input 20 for a bill of 23:
total ← 23
READ paid
PRINT "Paid:", paid
IF paid >= total THEN
PRINT "Change:", paid - total
ELSE
PRINT "Short by:", total - paid
END IF
Paid: 20
Short by: 3
20 is less than 23, so the ELSE branch runs. If the customer pays exactly 23, Change: 0 is printed.
Putting it together and testing it
In the exercise, the frame of the program is already there: the menu, the divider procedure, and the loop that reads the orders. You fill in three parts: the body of the function findIndex, what is done with each order inside the WHILE, and the paying part below it. Do them one at a time. Run the program each time you finish a part, and watch the receipt grow.
In the exercise, your program runs three times, and each run tests a different edge:
- The first customer orders the first menu item and pays more than the total.
- The second customer orders something that is not on the menu and the last menu item, then pays exactly.
- The third customer orders three items and does not have enough money.
If a result is wrong, open the Trace table tab in the Playground. It shows what total and index hold in each round.
⚠️ A receipt that goes wrong
- A function that uses
menudirectly. AFUNCTION findIndex(name)that usesmenuinside it stops with the message "Line 3:menucan't be seen inside the functionfindIndex." Send the list in through a parameter:findIndex(menu, name). - The price taken before
indexis checked. For Meatballs,indexholds -1, andprices[index]stops with the message "Line 3:pricesonly has 4 items (0 to 3), butindexholds -1." The message also reminds you that -1 usually means "not found", not an item number. Check withIF index = -1first, and for an item that is not on the menu printMeatballs is not on the menu. - Forgetting
READ nameat the end of the loop.namenever changes, soREAD countalso reads the next menu name. The order loop above then printsOrder: Iced tea 2,Order: Iced tea Coffee,Order: Iced tea 1,Order: Iced tea done, then stops with the message "Line 3: READ needs more input, but the 5 lines of input are used up." - A stop sign with different capital letters.
WHILE name ≠ "Done"does not stop when it readsdone. The loop keeps going until the input is used up, then the program stops with the same message. Write exactly"done". total ← 0inside the loop.totalgoes back to 0 for every order, so the total that is printed is only the subtotal of the last order. There is no error message. Writetotal ← 0above theWHILE.- Text plus a number.
PRINT "Total: " + totalstops with the message "Line 2: text can't be added to a number directly." Use a comma:PRINT "Total:", total.
TL;DR: The till program for a food stall combines everything you have learned. The menu names and their prices are kept in two lists that come in pairs, and the function findIndex finds the item number of a menu item. The loop WHILE name ≠ "done" reads the orders, however many there are, until the word done is read. For each order, its subtotal (the price times the count) is added to the total. The divider procedure prints the dividing lines of the receipt, and IF paid >= total chooses between printing the change and printing how much is missing. Start from the plan, build one part, run it, then go on to the next part.
⚡ The plan first
Meaning: In this lesson you put together a till program for a food stall, which prints a receipt. There are no new commands. The plan has five steps: keep the menu and its prices, print the top of the receipt, read the orders one at a time until the customer says done, print the total, then read the money paid and print the change. You have done each step in another lesson.
⚡ Read until "done"
Write:
READ name
WHILE name ≠ "done" DO
READ count
PRINT "Order:", name, count
READ name
END WHILE
PRINT "Orders closed"
Guess first: the input is Iced tea, 2, Coffee, 1, done. Is the word done printed as an order too?
No. As soon as name holds done, the WHILE condition is checked and it is FALSE. The loop stops before the PRINT inside it runs again.
Meaning: This program reads orders until the word done is read, however many orders there are. The pattern: READ name once before the loop, then READ name again on the last line of the loop's body. Each order is two lines of input: the name of the menu item, then the count.
⚡ Find the price, check first
Meaning: The menu names are kept in the list menu, and their prices in the list prices, in the same order. The function findIndex(menu, name) finds the item number of a menu item. Store the result in index, and the price is prices[index]. For an item that is not on the menu, like Meatballs, findIndex hands back -1, which is not an item number. So check with IF index = -1 before you take the price.
⚡ Paying and change
Guess first: the paying part prints Change: when its IF condition is TRUE, and Short by: in the ELSE branch. The bill is 23 and the customer pays exactly 23, but the condition is written IF paid > total THEN. What is printed?
Short by: 0. 23 > 23 is FALSE, so the ELSE branch runs.
Meaning: Exact money must be accepted too, so the condition uses >=: IF paid >= total. With >=, a customer who pays exactly gets Change: 0.
In the exercise, the frame of the program is already there. You fill in the body of the function findIndex, what is done with each order inside the WHILE, and the paying part. Run the program each time you finish a part.