Mini Project: The Till — Pseudocode

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…

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:

  1. Keep the menu names and their prices.
  2. Print the top of the receipt.
  3. 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.
  4. Print the total.
  5. 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:

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

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.

What you will learn