Worked solution from the Programming Paper 2 Algorithms, Programming and Logic booklet, Part 2 of 3.
Write one procedure per action, each with the account ID as its parameter, then a main program that checks the details and shows the menu. Valid, AccountNumber and Choice are assumed to be global.
// check the account number, name and password
PROCEDURE CheckDetails(AccID : INTEGER)
DECLARE Name, Password : STRING
Valid ← FALSE
IF AccID < 1 OR AccID > Size
THEN
OUTPUT "Invalid account number"
ELSE
OUTPUT "Enter your name"
INPUT Name
OUTPUT "Enter your password"
INPUT Password
IF Name <> Account[AccID, 1] OR Password <> Account[AccID, 2]
THEN
OUTPUT "Invalid name or password"
ELSE
Valid ← TRUE
ENDIF
ENDIF
ENDPROCEDURE
// show the balance
PROCEDURE Balance(AccID : INTEGER)
OUTPUT "Your balance is ", AccDetails[AccID, 1]
ENDPROCEDURE
// withdraw money within the withdrawal and overdraft limits
PROCEDURE Withdrawal(AccID : INTEGER)
DECLARE Amount : REAL
OUTPUT "Enter the amount to withdraw"
INPUT Amount
IF Amount > AccDetails[AccID, 3]
THEN
OUTPUT "Amount is more than the withdrawal limit"
ELSE
IF Amount > AccDetails[AccID, 1] + AccDetails[AccID, 2]
THEN
OUTPUT "Amount is more than the cash available"
ELSE
AccDetails[AccID, 1] ← AccDetails[AccID, 1] - Amount
OUTPUT "Withdrawal complete"
ENDIF
ENDIF
ENDPROCEDURE
// deposit money
PROCEDURE Deposit(AccID : INTEGER)
DECLARE Amount : REAL
REPEAT
OUTPUT "Enter a positive amount to deposit"
INPUT Amount
UNTIL Amount > 0
AccDetails[AccID, 1] ← AccDetails[AccID, 1] + Amount
ENDPROCEDURE
// main program
OUTPUT "Enter your account number"
INPUT AccountNumber
CALL CheckDetails(AccountNumber)
IF Valid = TRUE
THEN
REPEAT
OUTPUT "1. Display balance"
OUTPUT "2. Withdraw money"
OUTPUT "3. Deposit money"
OUTPUT "4. Exit"
OUTPUT "Enter your choice (1 to 4)"
INPUT Choice
CASE OF Choice
1 : CALL Balance(AccountNumber)
2 : CALL Withdrawal(AccountNumber)
3 : CALL Deposit(AccountNumber)
4 : OUTPUT "Goodbye"
OTHERWISE OUTPUT "Invalid choice"
ENDCASE
UNTIL Choice = 4
ENDIFAccount ID checked to exist (from 1 to Size) with an error message [2]
Name and password input and compared with Account[] before any action [2]
Menu of the four actions displayed [1]
Choice input, acted on with CASE or IF, and repeated until 4 (exit) [2]
Balance procedure with the account ID as a parameter outputs the balance [1]
Withdrawal checks the withdrawal limit and the overdraft limit and gives error messages [3]
Balance reduced when a withdrawal is allowed [1]
Deposit procedure checks for a positive amount and adds it to the balance [1]
Meaningful identifiers [1]
Comments that explain how the code works [1]
Other correct programs score too, for example using IF instead of CASE.
▶ Whole paper
Video solution of the whole paper · by CS BY AQIB KHAN. Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.