A class grade book
The problem
Build a command-line program that keeps the marks of students: add a mark, show a student's average, list the class ranked by average and report every wrong command clearly.
Requirements
- A GradeBook class that owns the data in a private associative array of lists
- Commands read from the standard input in a loop until it ends: add, average, rank
- An exception for marks outside 0 to 100 and for unknown students
- A ranking sorted with usort and the spaceship operator, with ties broken by name
Technologies
- PHP 8.4
- Associative arrays
- Classes
- Exceptions
- usort
Step by step
- Write down the commands and the rules, and list the nouns and verbs.
- Write the GradeBook class and test add and average with a few fixed calls.
- Add the exceptions for bad marks and unknown names.
- Write the command loop with explode and a switch, and add the rank report.
Expected result
A reliable program where a wrong command prints an error message and never damages the data.
How it is evaluated
- Every rule is enforced inside the class
- Averages and ranks match hand calculations
- Errors give clear messages
- Code is split into small methods
Ways to improve it
- Add a command that removes a student
- Print averages with two decimals using printf
- Add grade letters with match