NSSCO Computer Studies (6134) study materials
NSSCO Computer Studies (6134) study resources

Debugging a prepaid electricity vending program

NSSCO Computer Studies (6134) · Put your knowledge to work in context.

All case studies

Debugging a prepaid electricity vending program

A municipal prepaid electricity vendor uses a small program at a kiosk. Customers pay an amount in Namibian dollars, and the program calculates the number of electricity units to load onto a meter. The tariff has three bands: the first 50 units cost N$1.20 each, the next 100 units cost N$1.80 each, and any remaining units cost N$2.40 each. The program is supposed to reject meter numbers that are not exactly six digits and to reject amounts that are not positive multiples of N$10. Staff have received complaints that customers buying N$300 get too many units, while customers buying exactly N$60 get the correct 50 units. A customer buying N$100 was given 67 units instead of the expected amount. The kiosk sometimes loses power during a transaction, and the vendor wants a printed receipt and an audit log.

1. Using the tariff, calculate the correct number of units for N$100 and N$300. Show each band and the arithmetic.

Read the worked answer

For N$100:

  • First 50 units cost 50 × N$1.20 = N$60.00. Remaining money = N$40.00.
  • N$40.00 at N$1.80 buys 40 / 1.80 = 22.22 units.
  • Total = 50 + 22.22 = 72.22 units. For N$300:
  • First 50 units = N$60.00; next 100 units = 100 × N$1.80 = N$180.00. Total for 150 units = N$240.00. Remaining = N$60.00.
  • N$60.00 at N$2.40 buys 60 / 2.40 = 25.00 units.
  • Total = 150 + 25 = 175.00 units. So N$60 correctly gives 50 units, N$100 should give 72.22 units (not 67), and N$300 should give 175 units (not 150).

2. The kiosk pseudocode is: BEGIN INPUT amount, meterNumber IF meterNumber < 100000 OR meterNumber > 999999 THEN DISPLAY 'Invalid meter' END IF units = amount / 1.20 IF amount > 60 THEN units = 50 + (amount - 60) / 2.40 END IF units = ROUND(units, 0) DISPLAY units END. Identify three logic errors and one validation error, and explain how each produces wrong output or unsafe behaviour.

Read the worked answer

Errors:

  1. Wrong second-band rate: the code divides by 2.40 for all money above N$60. The second band is 1.80, so N$100 gives 50 + 40/2.40 = 66.67, rounded to 67, instead of 72.22.
  2. No third band: above N$240 the code still uses 2.40 for the remainder but never charges the first 150 units correctly; it replaces the whole calculation with 50 + ... and produces 150 for N$300 instead of 175.
  3. Invalid meter does not stop processing: after DISPLAY 'Invalid meter' there is no ELSE or exit, so the program continues and may issue units to an invalid meter. It is a validation error and a logic error.
  4. Rounding to 0 decimal places gives away or withholds fractions; units should be displayed to two decimals. There is also no check that amount is a positive multiple of N$10, and no receipt or audit record.

3. Rewrite the pseudocode to correct the errors and include validation for the amount and meter number, calculation of the three bands, and output of units to two decimal places. Explain how you would test it with boundary values.

Read the worked answer

Corrected pseudocode: BEGIN INPUT amount, meterNumber IF meterNumber < 100000 OR meterNumber > 999999 THEN DISPLAY 'Invalid meter number' STOP END IF IF amount <= 0 OR amount MOD 10 <> 0 THEN DISPLAY 'Amount must be a positive multiple of 10' STOP END IF remaining = amount units = 0 IF remaining > 0 THEN band1 = MIN(remaining / 1.20, 50) units = units + band1 remaining = remaining - band1 * 1.20 END IF IF remaining > 0 THEN band2 = MIN(remaining / 1.80, 100) units = units + band2 remaining = remaining - band2 * 1.80 END IF IF remaining > 0 THEN units = units + remaining / 2.40 END IF DISPLAY 'Units: ', ROUND(units, 2) SAVE transaction(amount, meterNumber, units, timestamp) END

Test with boundary values: amount 10 (8.33 units, only band 1), 60 (50 units, boundary), 70 (55.56), 240 (150 units, band 2 boundary), 250 (154.17), 300 (175), 0 and -10 (rejected), 25 (rejected, not multiple of 10), meter 99999 and 100000 (boundary), 1000000 (rejected). Check that the sum of money allocated equals the amount paid and that the audit log records rejected attempts.