Browse Source

documentation: registerUser Method in Administration class

remotes/origin/fdai7780
Tobias Herbert 11 months ago
parent
commit
8694e81cb0
  1. 41
      src/main/java/org/example/Administration.java

41
src/main/java/org/example/Administration.java

@ -160,8 +160,13 @@ public class Administration {
return null; return null;
} }
/**
* Method to register a User
*/
public void registerUser() public void registerUser()
{ {
//Instance variables
String UserId = null; String UserId = null;
String UserName = null; String UserName = null;
@ -178,26 +183,34 @@ public class Administration {
System.out.println("ID: "); System.out.println("ID: ");
UserId = scanner.next(); UserId = scanner.next();
//Checks if the entered id is already being used
if(findProfessorById(UserId) != null || findAdminById(UserId) != null || findStudentById(UserId) != null) 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!"); System.out.println("Please enter an ID which is not being used!");
correctData = false; correctData = false;
} }
else if(findProfessorById(UserId) == null || findAdminById(UserId) == null || findStudentById(UserId) == null) 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); String substring = UserId.substring(1);
if(!rightPrefix(UserId, userType)) if(!rightPrefix(UserId, userType))
{ {
//the prefix is wrong
correctData = false; correctData = false;
} }
else if(!isNumber(substring)) else if(!isNumber(substring))
{ {
//the substring does not contain only digits
System.out.println("Every character besides the first one need to be digits"); System.out.println("Every character besides the first one need to be digits");
correctData = false; correctData = false;
} }
else{ else{
//the id is not being used and has the right id-"syntax"
System.out.println("The Id has not been used already!"); System.out.println("The Id has not been used already!");
correctData = true; correctData = true;
} }
@ -206,23 +219,28 @@ public class Administration {
switch (userType) switch (userType)
{ {
case 1:
case 1: //Student
Student student = new Student(UserName, UserId, "Student"); Student student = new Student(UserName, UserId, "Student");
addStudents(student); addStudents(student);
break; break;
case 2:
case 2: //Admin
Admin admin = new Admin(UserName, UserId, "Admin"); Admin admin = new Admin(UserName, UserId, "Admin");
addAdmin(admin); addAdmin(admin);
break; break;
case 3:
case 3: //Professor
Professor professor = new Professor(UserId, UserName, "Professor"); Professor professor = new Professor(UserId, UserName, "Professor");
addProfessor(professor); addProfessor(professor);
default:
default: //Error
break; 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) public boolean isNumber(String substring)
{ {
if(substring == null || substring.isEmpty()) if(substring == null || substring.isEmpty())
@ -238,10 +256,17 @@ public class Administration {
} }
return true; 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) public boolean rightPrefix(String userId, int i)
{ {
switch (i){ switch (i){
case 1:
case 1: //Student
if(userId.startsWith("S")) if(userId.startsWith("S"))
{ {
return true; return true;
@ -257,7 +282,7 @@ public class Administration {
return false; return false;
} }
break; break;
case 2:
case 2: //Admin
if(userId.startsWith("A")) if(userId.startsWith("A"))
{ {
return true; return true;
@ -273,7 +298,7 @@ public class Administration {
return false; return false;
} }
break; break;
case 3:
case 3: //Professor
if(userId.startsWith("P")) if(userId.startsWith("P"))
{ {
return true; return true;
@ -290,7 +315,7 @@ public class Administration {
} }
break; break;
default:
default: //Error
System.out.println("Error"); System.out.println("Error");
break; break;
} }

Loading…
Cancel
Save