Mini Project: The Till — Pseudocode

Here's the receipt your till program will print by the end of this lesson: Mrs. Sri's Stall -------------------- Fried rice x 1 = 15000 Iced tea x 2 = 8000…

Here's the receipt your till program will print by the end of this lesson:

Mrs. Sri's Stall
--------------------
Fried rice x 1 = 15000
Iced tea x 2 = 8000
--------------------
Total: 23000
Paid: 50000
Change: 27000

The customer orders one fried rice and two iced teas, then pays 50000. The program that prints it reads the orders one at a time, looks up each price on the menu, adds everything up, then works out the change.

There are no new commands in this lesson. Lists, searching, loops, functions, procedures and IF are all things you know. The only new thing is the job: putting those pieces together into one whole program, like a real till.

The plan first

The Breaking a Problem Down lesson said: steps first, code later. Here's the plan for the till:

  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 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've done every step before in another lesson. The next sections go through them one by one, each as a small program you can run on its own.

The menu and its prices

The menu is two lists that come in pairs, as in the Searching lesson: names in menu, prices in prices, in the same order. To find a price, first find the menu item's number:

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 ← [15000, 12000, 4000, 5000]
index ← findIndex(menu, "Iced tea")
PRINT "Iced tea", prices[index]
PRINT "Meatballs", findIndex(menu, "Meatballs")
Iced tea 4000
Meatballs -1

Iced tea is at number 2, so its price is prices[2], 4000. Meatballs aren't on the menu, so the result is -1. The list menu is sent through the parameter list, because a function can't see the variables outside it.

Reading orders until "done"

How many orders there are isn't known at the start. The pattern from the While Loops lesson comes back: read once before the loop, then read again on the last line of its body. Each order is two input lines, the menu name and 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 0. Here what's read is a name, so the stop sign is the word done, which can never be a menu name.

Receipt lines and the total

Each order becomes one line: the name, the count and its subtotal. The total is added up along the way, starting from 0 outside the loop. The dividing line is the procedure from the Procedures lesson:

PROCEDURE divider()
    PRINT "--------------------"
END PROCEDURE

total ← 0
divider()
subtotal ← 4000 * 2
PRINT "Iced tea", "x", 2, "=", subtotal
total ← total + subtotal
subtotal ← 5000 * 1
PRINT "Coffee", "x", 1, "=", subtotal
total ← total + subtotal
divider()
PRINT "Total:", total
--------------------
Iced tea x 2 = 8000
Coffee x 1 = 5000
--------------------
Total: 13000

The three middle lines for each order are what goes inside the WHILE later, with the price from findIndex and the count from READ.

Paying and change

Exact money has to be accepted too, so the condition is >=, not >. With the input 20000 for a bill of 23000:

total ← 23000
READ paid
PRINT "Paid:", paid
IF paid >= total THEN
    PRINT "Change:", paid - total
ELSE
    PRINT "Short by:", total - paid
END IF
Paid: 20000
Short by: 3000

If the customer pays exactly 23000, Change: 0 is printed.

Putting it together and testing it

In the exercise, the frame is already there: the menu, the divider procedure, and the loop that reads the orders. You fill in three parts: the body of findIndex, what happens to each order inside the WHILE, and the paying part below it. Do them one at a time, run it after each part, and watch the receipt grow.

Your program runs three times, each testing a different edge. One customer orders the first menu item and pays more. One orders something not on the menu and the last menu item, then pays exactly. The last one orders three items and doesn't have enough money. If something slips, the Playground's Trace table tab shows what total and index hold in each round.

⚠️ A receipt that slips

TL;DR: A food stall's till combines everything you've learned: the menu and prices in two lists that come in pairs, findIndex to find a menu item's number, WHILE name ≠ "done" to read any number of orders, a subtotal and total added up along the way, the divider procedure for the receipt, and IF paid >= total for the change. Start from the plan, build one part, run it, then move on to the next part.

What you will learn