From 8694e81cb0bdab7ebedbcc642131bdc9ed734c0b Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Wed, 7 Feb 2024 11:38:12 +0100 Subject: [PATCH] documentation: registerUser Method in Administration class --- src/main/java/org/example/Administration.java | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/example/Administration.java b/src/main/java/org/example/Administration.java index 14b2ae9..95f0e2c 100644 --- a/src/main/java/org/example/Administration.java +++ b/src/main/java/org/example/Administration.java @@ -160,8 +160,13 @@ public class Administration { return null; } + /** + * Method to register a User + */ public void registerUser() { + //Instance variables + String UserId = null; String UserName = null; @@ -178,26 +183,34 @@ public class Administration { System.out.println("ID: "); UserId = scanner.next(); + //Checks if the entered id is already being used if(findProfessorById(UserId) != null || findAdminById(UserId) != null || findStudentById(UserId) != null) { + //the entered id is used System.out.println("Please enter an ID which is not being used!"); correctData = false; } else if(findProfessorById(UserId) == null || findAdminById(UserId) == null || findStudentById(UserId) == null) { + //the entered id is not used + + //creates a substring to check if the id-"syntax" is correct String substring = UserId.substring(1); if(!rightPrefix(UserId, userType)) { + //the prefix is wrong correctData = false; } else if(!isNumber(substring)) { + //the substring does not contain only digits System.out.println("Every character besides the first one need to be digits"); correctData = false; } else{ + //the id is not being used and has the right id-"syntax" System.out.println("The Id has not been used already!"); correctData = true; } @@ -206,23 +219,28 @@ public class Administration { switch (userType) { - case 1: + case 1: //Student Student student = new Student(UserName, UserId, "Student"); addStudents(student); break; - case 2: + case 2: //Admin Admin admin = new Admin(UserName, UserId, "Admin"); addAdmin(admin); break; - case 3: + case 3: //Professor Professor professor = new Professor(UserId, UserName, "Professor"); addProfessor(professor); - default: + default: //Error break; } } } + /** + * + * @param substring the userID without the prefixes S (for Student ), A ( Admin), P (Professor) + * @return returns true if the substring is only containing digits, returns false either the substring is empty or includes not only digits + */ public boolean isNumber(String substring) { if(substring == null || substring.isEmpty()) @@ -238,10 +256,17 @@ public class Administration { } return true; } + + /** + * + * @param userId the userId to be checked + * @param i equals userType + * @return returns true if the right prefix is being used + */ public boolean rightPrefix(String userId, int i) { switch (i){ - case 1: + case 1: //Student if(userId.startsWith("S")) { return true; @@ -257,7 +282,7 @@ public class Administration { return false; } break; - case 2: + case 2: //Admin if(userId.startsWith("A")) { return true; @@ -273,7 +298,7 @@ public class Administration { return false; } break; - case 3: + case 3: //Professor if(userId.startsWith("P")) { return true; @@ -290,7 +315,7 @@ public class Administration { } break; - default: + default: //Error System.out.println("Error"); break; }