OCR GCSE Computer Science June 2022 Paper 2, Question 5(b): validating a hotel booking

OCR J277/02 June 2022, Question 5(b): complete a program that checks names are not empty, the room is basic or premium and nights is 1 to 5, then outputs ALLOWED or NOT ALLOWED. The valid flag pattern, the AND and OR traps, and a test plan.

Past paper questionOCR J277/02June 2022 Paper 28 marksWrite a program

Question 5(b) of the OCR GCSE Computer Science Paper 2 sat on 27 May 2022 (J277/02) asks for three different validation checks in one program (5 marks), and then a test plan for one of them (3 marks). Every check here has an AND or OR trap in it.

We do not copy the exam paper here. Open it beside this page: OCR June 2022 J277/02 question paper (PDF). When you have finished, check the mark scheme too.

The question in short

Four values have been input: firstName, surname, room and nights. The booking is valid if:

  • firstName and surname are not empty (a presence check);
  • room is either "basic" or "premium" (a lookup check);
  • nights is between 1 and 5 inclusive (a range check).

If anything is invalid, display "NOT ALLOWED". If everything is valid, display "ALLOWED".

The pattern: a valid flag

Assume the booking is good. Let each check knock the flag down. Output once, at the end.

valid = True
if firstName == "" or surname == "" then
    valid = False
endif
if room != "basic" and room != "premium" then
    valid = False
endif
if nights < 1 or nights > 5 then
    valid = False
endif
if valid then
    print("ALLOWED")
else
    print("NOT ALLOWED")
endif

You can also write it as one long if that tests for good values: both names not empty and (room is basic or premium) and nights from 1 to 5.

AND or OR?

Check Looking for bad values Looking for good values
names firstName == "" or surname == "" firstName != "" and surname != ""
room room != "basic" and room != "premium" room == "basic" or room == "premium"
nights nights < 1 or nights > 5 nights >= 1 and nights <= 5

The room check is the one that catches people. room != "basic" or room != "premium" is always true: a basic room is not premium, so it fails the second half.

Where the five marks are

One for each of the three checks. One for "NOT ALLOWED" being output whenever any check fails. One for "ALLOWED" being output only when all three pass. The output marks need an attempt at all three checks.

Part (ii): the test plan for nights

Test data Type of test Expected output
2 Normal ALLOWED
1 or 5 Boundary ALLOWED
7 (or 0, or "bananas") Erroneous / Invalid NOT ALLOWED

The boundary value must be 1 or 5. Not 0 or 6: the expected output printed on the paper is ALLOWED.

Where the marks are lost

  • if firstName or surname == "". Each side of or must be a whole comparison. The mark scheme refuses this by name.
  • Printing in every check. Then a booking with two faults prints twice, and a booking with one fault may print both messages.
  • > and < for the range. 1 and 5 nights are allowed, so the good-value test needs >= and <=.
  • Re-doing the inputs. They are given. Carry on from where the paper stops.

Run it

The checks as a function, run on six bookings: one good, and one that fails each check. Add your own to TESTS.

Only the first and last bookings are ALLOWED. Change the room check to use or and watch every booking fail.
The program
from bugbot import *
connect()

TESTS = [["Amaya", "Taylor-Ling", "premium", 3],
         ["", "Taylor-Ling", "premium", 3],
         ["Amaya", "", "basic", 2],
         ["Amaya", "Taylor-Ling", "deluxe", 3],
         ["Amaya", "Taylor-Ling", "basic", 6],
         ["Sam", "Okoro", "basic", 5]]

def check(firstName, surname, room, nights):
    valid = True
    if firstName == "" or surname == "":
        valid = False
    if room != "basic" and room != "premium":
        valid = False
    if nights < 1 or nights > 5:
        valid = False
    if valid:
        return "ALLOWED"
    else:
        return "NOT ALLOWED"

for firstName, surname, room, nights in TESTS:
    print([firstName, surname, room, nights], check(firstName, surname, room, nights))
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Questions

What is the answer to OCR J277 June 2022 Paper 2 Question 5(b)(i)?

Set valid to True. Set it to False if firstName or surname is empty, if room is neither "basic" nor "premium", or if nights is less than 1 or more than 5. Then print ALLOWED if valid is still True and NOT ALLOWED otherwise.

What is a presence check?

Validation that makes sure something has been entered, by testing that the input is not an empty string.

Why is room != "basic" or room != "premium" always true?

Any room fails to be at least one of the two. A basic room is not premium, and a premium room is not basic, so one side of the OR is always true. Use AND when testing that a value is none of a list.

More from this paper

Every OCR J277 question we have worked · Guide: Logic gates and truth tables explained

Learn it step by step

  1. F6.1 Defensive design and validation Robust programs
  2. F6.3 Testing and test data Robust programs
  3. F2.3 else, elif and Boolean operators Decisions and loops
Open the lessons

This is our own explanation of a published exam question. It is not written or endorsed by OCR, and the question paper and mark scheme remain OCR's copyright. Read them on OCR's site with the links on this page.