From 0dd4950057d4141c01419f06618e16f6d8a5c230 Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Wed, 7 Feb 2024 12:16:45 +0100 Subject: [PATCH] refactoring: Administration and AdministrationTest (registerUser, isNumber, rightPrefix) --- src/main/java/org/example/Administration.java | 60 +++++++++---------- .../java/org/example/AdministrationTest.java | 12 ++-- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/example/Administration.java b/src/main/java/org/example/Administration.java index 95f0e2c..8e6053b 100644 --- a/src/main/java/org/example/Administration.java +++ b/src/main/java/org/example/Administration.java @@ -188,25 +188,23 @@ public class Administration { { //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)) + else if(!isNumber(UserId)) { //the substring does not contain only digits System.out.println("Every character besides the first one need to be digits"); - correctData = false; + } 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 */ - 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()) { if(!Character.isDigit(c)) @@ -267,53 +268,50 @@ public class Administration { { switch (i){ 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")) { System.out.println("The ID needs to start with a capital S!"); 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")) { System.out.println("The ID needs to start with a capital A!"); 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 - 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")) { System.out.println("The ID needs to start with a capital P!"); 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 System.out.println("Error"); diff --git a/src/test/java/org/example/AdministrationTest.java b/src/test/java/org/example/AdministrationTest.java index 048b51b..6d0ca33 100644 --- a/src/test/java/org/example/AdministrationTest.java +++ b/src/test/java/org/example/AdministrationTest.java @@ -26,16 +26,16 @@ class AdministrationTest { Administration administration = new Administration(); 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 str2 = full02.substring(1); //String without the Letter + // String str2 = full02.substring(1); //String without the Letter 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 - 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(false, test02); // false, because the Z at the end