Checkout

Checkout

Lookup product description and price.

A touchscreen till displays a number of virtual buttons. Each button corresponds to a product for sale. For example, button 1 is a Snickers Bar. The products are held in a file called `products.txt`. The format of the file is: button number, product name, price.

0,CADBURY DAIRY MILK WHOLENUT 49G,0.70

1,SNICKERS BAR 48G,0.70

2,CADBURY DAIRY MILK MILK CARAMEL,0.70

3,MARS BAR ORIGINAL 51G,0.80

4,CADBURY FLAKE 32G,0.80

5,GALAXY CARAMEL STANDARD 48G,0.80

6,GALAXY MILK CHOCOLATE 46G,0.80

7,CADBURY STAR BAR 49G,0.60

8,NESTLE MUNCHIES 52G,0.60

9,KINDER BUENO 43g,1.00

10,SNICKERS DUO 83.4G,1.00

For the purposes of testing, the till is simulated with the user entering a button number followed by Enter. Pressing Enter without a number simulates the end of a transaction. A user can input any number of products in a single transaction. When a transaction is complete the total price is displayed.

Make


Write a program that allows the user to enter a button number. Invalid numbers are rejected. The program outputs then name of the product and the price. The user can keep entering numbers until they press Enter, at which point the total of the transaction is shown.

Use this boilerplate code as a starting point:

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called `read_database` that:

  1. Reads the `products.txt` file into a 2D list.
  2. Returns the list.

Complete the subprogram called `get_product` that:

  1. Prompts the user to enter a button number.
  2. Validates the input so that only no input or a number between 0 and the maximum button number in the product list is accepted.
  3. Returns the button number or -1 for no input.

Complete the subprogram called `output_product` that:

  1. Takes a parameter, the button number.
  2. Outputs the product name and price.

Complete the subprogram called `transaction` that:

  1. Calls the subprogram `get_product` to input the button number.
  2. Calls the subprogram `output_product` to output the relevant data.
  3. Continues to input and output products until the transaction is terminated.
  4. Outputs the total price of the transaction.

Complete the `main program` so that:

  1. A products list is assigned to be the returned list from calling `read_database`.
  2. The subprogram `transaction` is called.

Typical inputs and outputs from the program would be:

Enter button number: 2

CADBURY DAIRY MILK MILK CARAMEL £0.70

Enter button number:

Total: £0.70


Enter button number: -1

Enter button number: 13

Enter button number: a

Enter button number:

Total: £0.00


Enter button number: 8

NESTLE MUNCHIES 52G £0.60

Enter button number: 2

CADBURY DAIRY MILK MILK CARAMEL £0.70

Enter button number: 4

CADBURY FLAKE 32G £0.80

Enter button number:

Total: £2.10

Knowledge Organiser

Use these resources as a reference to help you meet the success criteria.

Programming guide:

Evaluate


Run the unit tests below to check that your program has met the success criteria.

Enter button number: -1

Enter button number: g

Enter button number: 13

Enter button number:

Total: £0.00

Enter button number: 5

GALAXY CARAMEL STANDARD 48G £0.80

Enter button number: 8

NESTLE MUNCHIES 52G £0.60

Enter button number: 2

CADBURY DAIRY MILK MILK CARAMEL £0.70

Enter button number: 9

KINDER BUENO 43g £1.00

Enter button number:

Total: £3.10

Check that you have:

  • Used comments within the code to describe the purpose of subprograms, conditions and iterations.
  • Used meaningful identifier names. That means the names of subprograms and variables indicate what they are for.
Craig'n'Dave logo

Craig ‘n’ Dave

Bett Awards 2024 Finalist