Browse Source

refactoring: Administration and AdministrationTest (registerUser, isNumber, rightPrefix)

remotes/origin/fdai7780
Tobias Herbert 11 months ago
parent
commit
0dd4950057
  1. 60
      src/main/java/org/example/Administration.java
  2. 12
      src/test/java/org/example/AdministrationTest.java

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

@ -188,25 +188,23 @@ public class Administration {
{ {
//the entered id is used //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;
} }
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 //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)) if(!rightPrefix(UserId, userType))
{ {
//the prefix is wrong //the prefix is wrong
correctData = false;
} }
else if(!isNumber(substring))
else if(!isNumber(UserId))
{ {
//the substring does not contain only digits //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;
} }
else{ else{
@ -238,15 +236,18 @@ public class Administration {
/** /**
* *
* @param substring the userID without the prefixes S (for Student ), A ( Admin), P (Professor)
* @param userId the userID
* @return returns true if the substring is only containing digits, returns false either the substring is empty or includes not only digits * @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 userId)
{ {
if(substring == null || substring.isEmpty())
{
return false;
}
//creates a substring to check if the id-"syntax" is correct
String substring = userId.substring(1);
//if(substring == null || substring.isEmpty())
//{
// return false;
//}
for(char c : substring.toCharArray()) for(char c : substring.toCharArray())
{ {
if(!Character.isDigit(c)) if(!Character.isDigit(c))
@ -267,53 +268,50 @@ public class Administration {
{ {
switch (i){ switch (i){
case 1: //Student case 1: //Student
if(userId.startsWith("S"))
if(!userId.startsWith("S"))
{ {
return true;
System.out.println("The ID needs to start with a capital S!");
return false;
} }
else if(userId.startsWith("s")) else if(userId.startsWith("s"))
{ {
System.out.println("The ID needs to start with a capital S!"); System.out.println("The ID needs to start with a capital S!");
return false; return false;
} }
else if(!userId.startsWith("S"))
else
{ {
System.out.println("The ID needs to start with a capital S!");
return false;
return true;
} }
break;
case 2: //Admin
if(userId.startsWith("A"))
case 2: //Admin
if(!userId.startsWith("A"))
{ {
return true;
System.out.println("The ID needs to start with a capital A!");
return false;
} }
else if(userId.startsWith("a")) else if(userId.startsWith("a"))
{ {
System.out.println("The ID needs to start with a capital A!"); System.out.println("The ID needs to start with a capital A!");
return false; return false;
} }
else if(!userId.startsWith("A"))
else
{ {
System.out.println("The ID needs to start with a capital A!");
return false;
return true;
} }
break;
case 3: //Professor case 3: //Professor
if(userId.startsWith("P"))
if(!userId.startsWith("P"))
{ {
return true;
System.out.println("The ID needs to start with a capital P!");
return false;
} }
else if(userId.startsWith("p")) else if(userId.startsWith("p"))
{ {
System.out.println("The ID needs to start with a capital P!"); System.out.println("The ID needs to start with a capital P!");
return false; return false;
} }
else if(!userId.startsWith("P"))
else
{ {
System.out.println("The ID needs to start with a capital P!");
return false;
return true;
} }
break;
default: //Error default: //Error
System.out.println("Error"); System.out.println("Error");

12
src/test/java/org/example/AdministrationTest.java

@ -26,16 +26,16 @@ class AdministrationTest {
Administration administration = new Administration(); Administration administration = new Administration();
String full01 = "S1002"; //Supposed student id String full01 = "S1002"; //Supposed student id
String str1 = full01.substring(1); //String without the Letter
// String str1 = full01.substring(1); //String without the Letter
String full02 = "S100Z"; //Supposed student id String full02 = "S100Z"; //Supposed student id
String str2 = full02.substring(1); //String without the Letter
// String str2 = full02.substring(1); //String without the Letter
String full03 = "P1004"; //Supposed professor id String full03 = "P1004"; //Supposed professor id
String str3 = full03.substring(1); //String without the Letter
// String str3 = full03.substring(1); //String without the Letter
//checks if the substrings contain only digits //checks if the substrings contain only digits
boolean test01 = administration.isNumber(str1);
boolean test02 = administration.isNumber(str2);
boolean test03 = administration.isNumber(str3);
boolean test01 = administration.isNumber(full01);
boolean test02 = administration.isNumber(full02);
boolean test03 = administration.isNumber(full03);
assertEquals(true, test01); //only digits besides the first letter assertEquals(true, test01); //only digits besides the first letter
assertEquals(false, test02); // false, because the Z at the end assertEquals(false, test02); // false, because the Z at the end

Loading…
Cancel
Save