VALIDATION.md
Validation & errors
Input is checked in three places: the Helpers validators before any setter runs, guards inside a few setters and loops, and try/catch blocks that keep the program alive.
Helpers.Validate*
Validators#
Each returns true or false, so prompts loop until the input passes. They are passed around as Predicate<String> method references, e.g. Helpers::ValidateEmail.
| Method | Passes when | Passes | Fails | Used by |
|---|---|---|---|---|
ValidateName | length() > 3 | Lina Haddad | Bo | Adding students and instructors, admin profile updates |
ValidatePassword | length() > 8 | Student1234 | secret12 | Creating accounts and every profile update |
ValidateEmail | contains("@") && contains(".") | sara.k@uni.example | sara-at-uni | Creating accounts and every profile update |
ValidatePhoneNumber | length() == 10 | 0500000101 | 05000001 | Creating accounts and every profile update |
ValidateAddress | length() > 3 | East Gate | N/A | Creating accounts and every profile update |
Phone numbers are checked for length only, so any ten characters pass. The report suggests a regular expression instead.
Guards & messages#
| Where | Rule | Prints |
|---|---|---|
Course.setCourseName | Not null or empty | Course name cannot be null or empty. |
Course.setCourseCode | Not null or empty | Course code cannot be null or empty. |
Course.setCapacity | Greater than 0 | Error: Capacity must be > 0. |
Course.setCreditHours | Greater than 0 | Credit hours can not be <= 0 |
Student.setCreditLimit | Not negative | Must be greater than 0 |
Helpers.getSafeIntInput | An integer was typed | Invalid input. Please enter a number. |
Helpers.checkValidityOfRole | 1, 2 or 3 | Invalid selection. Please enter 1, 2, or 3. |
Instructor.gradeStudent | Score 0–100 | Score must be between 0 and 100. |
Helpers.validatePasswordWithRetries | At most 3 attempts | Too many failed attempts. Returning to main menu. |
main() menu switches | A listed option | Invalid option. |
main() role prompt | student, instructor, admin or exit | Invalid role. Try again. |
Exception handling#
| Where | Catches | What happens |
|---|---|---|
main() | Exception, around the whole loop | Prints “Error: <message>” and the program ends without saving. |
load* methods | FileNotFoundException | “<Type> file not found. Starting with empty <type> list.” |
load* methods | IOException | “Error reading <type> file: <message>” |
loadStudents | NumberFormatException | “Error parsing student data: <message>” |
loadCourses, loadAssessments | IllegalArgumentException (bad enum or number) | “Error parsing course/assessment data: <message>” |
save* methods | IOException | “Error writing <type> file: <message>” |
Helpers.getSafeIntInput | Any exception from nextInt() | Clears the line and asks again. |
Helpers flows (enrol, drop, add, update…) | Exception | Prints the exception's message and returns to the menu. |
Instructor.gradeStudent | InputMismatchException for the score | “Invalid input. Please enter a number.” and asks again. |