CS102/enrolment
Report
enrolment›docs›VALIDATION.md

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.

MethodPasses whenPassesFailsUsed by
ValidateNamelength() > 3Lina HaddadBoAdding students and instructors, admin profile updates
ValidatePasswordlength() > 8Student1234secret12Creating accounts and every profile update
ValidateEmailcontains("@") && contains(".")sara.k@uni.examplesara-at-uniCreating accounts and every profile update
ValidatePhoneNumberlength() == 10050000010105000001Creating accounts and every profile update
ValidateAddresslength() > 3East GateN/ACreating 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#

WhereRulePrints
Course.setCourseNameNot null or emptyCourse name cannot be null or empty.
Course.setCourseCodeNot null or emptyCourse code cannot be null or empty.
Course.setCapacityGreater than 0Error: Capacity must be > 0.
Course.setCreditHoursGreater than 0Credit hours can not be <= 0
Student.setCreditLimitNot negativeMust be greater than 0
Helpers.getSafeIntInputAn integer was typedInvalid input. Please enter a number.
Helpers.checkValidityOfRole1, 2 or 3Invalid selection. Please enter 1, 2, or 3.
Instructor.gradeStudentScore 0–100Score must be between 0 and 100.
Helpers.validatePasswordWithRetriesAt most 3 attemptsToo many failed attempts. Returning to main menu.
main() menu switchesA listed optionInvalid option.
main() role promptstudent, instructor, admin or exitInvalid role. Try again.

Exception handling#

WhereCatchesWhat happens
main()Exception, around the whole loopPrints “Error: <message>” and the program ends without saving.
load* methodsFileNotFoundException“<Type> file not found. Starting with empty <type> list.”
load* methodsIOException“Error reading <type> file: <message>”
loadStudentsNumberFormatException“Error parsing student data: <message>”
loadCourses, loadAssessmentsIllegalArgumentException (bad enum or number)“Error parsing course/assessment data: <message>”
save* methodsIOException“Error writing <type> file: <message>”
Helpers.getSafeIntInputAny exception from nextInt()Clears the line and asks again.
Helpers flows (enrol, drop, add, update…)ExceptionPrints the exception's message and returns to the menu.
Instructor.gradeStudentInputMismatchException for the score“Invalid input. Please enter a number.” and asks again.