Chapter 7: money, typed

Chapter 7's Receipt adds up a shopping basket in pounds: prices written with the #GBP suffix, multiplied by whole quantities, summed and printed in en_GB format. The IDE writes the program's shell; the amounts are yours to type. To play it in the IDE: Help > Lessons > Getting Started with EK9…, or ek9 -ide -lesson book-receipt.

The EK9 IDE as the lesson opens

What you'll build

The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.

#!ek9
defines module book.receipt

  defines program

    Receipt()
      stdout <- Stdout()
      enGB <- Locale("en_GB")

      applePrice <- 0.35#GBP
      breadPrice <- 1.10#GBP
      milkPrice <- 0.85#GBP

      apples <- 6
      loaves <- 2
      bottles <- 1

      appleLine <- applePrice * apples
      breadLine <- breadPrice * loaves
      milkLine <- milkPrice * bottles

      total <- appleLine + breadLine + milkLine

      stdout.println(`Apples  x${apples}   ${enGB.format(appleLine)}`)
      stdout.println(`Bread   x${loaves}   ${enGB.format(breadLine)}`)
      stdout.println(`Milk    x${bottles}   ${enGB.format(milkLine)}`)
      stdout.println(`Total        ${enGB.format(total)}`)

Step 1: The program's shell

Choose Code > New Construct > Program…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

Receipt is the program's name. Type it and choose Preview.

The EK9 IDE just before choosing Preview: Receipt is the program's name

The preview shows Receipt about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Receipt about to be written

The tool leaves no blank line before the program: select the Receipt() line and, above it, type the one the book opens with. Saves when typed.

The EK9 IDE once this is done: The tool leaves no blank line before the program

Step 2: The body, typed

Select the stub, from ready <- true down to its //AI line, and type the body exactly as the chapter shows it. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type the body exactly as the chapter shows it

Step 3: Run it

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

The project's only program is already chosen. Choose Run.

Apples x6 £2.10, Bread x2 £2.20, Milk x1 £0.85, Total £5.15. Choose Close.

The EK9 IDE just before choosing Close: Apples x6 £2.10, Bread x2 £2.20, Milk x1 £0.85, Total £5.15

The result

Receipt.ek9 now reads exactly as chapter 7 of Getting Started with EK9 shows it: Money times an Integer is Money, Money plus Money is Money, and the Locale formats each as pounds.

The EK9 IDE when the lesson has finished

Where this lesson comes from