From b84b57d40bbbfcf5e257efb467f81c94bf7352ea Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Wed, 7 Feb 2024 09:42:26 +0100 Subject: [PATCH 01/10] Added basic registerUser Method to Administration Class --- src/main/java/org/example/Administration.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/main/java/org/example/Administration.java b/src/main/java/org/example/Administration.java index 2be870c..4e4c4f7 100644 --- a/src/main/java/org/example/Administration.java +++ b/src/main/java/org/example/Administration.java @@ -2,6 +2,7 @@ package org.example; import java.util.ArrayList; import java.util.List; +import java.util.Scanner; public class Administration { /** @@ -10,6 +11,7 @@ public class Administration { private List professors; private List admins; private List students; + Scanner scanner = new Scanner(System.in); /** * Constructor @@ -158,6 +160,54 @@ public class Administration { return null; } + public void registerUser() + { + String UserId = null; + String UserName = null; + + System.out.println("Enter the type of user (1-Student, 2-Admin, 3-Professor): "); + int userType = scanner.nextInt(); + + + boolean correctData = false; + while(!correctData) + { + System.out.println("Enter the User Details!"); + System.out.println("Name: "); + UserName = scanner.next(); + System.out.println("ID: "); + UserId = scanner.next(); + + if(findProfessorById(UserId) != null || findAdminById(UserId) != null || findStudentById(UserId) != null) + { + 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) + { + System.out.println("The Id has not been used already!"); + correctData = true; + } + + switch (userType) + { + case 1: + Student student = new Student(UserName, UserId, "Student"); + addStudents(student); + break; + case 2: + Admin admin = new Admin(UserName, UserId, "Admin"); + addAdmin(admin); + break; + case 3: + Professor professor = new Professor(UserId, UserName, "Professor"); + addProfessor(professor); + default: + break; + } + } + } + } From ef7d53509ca8b7f476abaf4fea35836f0de8c4c3 Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Wed, 7 Feb 2024 09:43:26 +0100 Subject: [PATCH 02/10] Implemented registerUser Feature in Main class so the Method cann be called --- src/main/java/org/example/Main.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index abbf297..3ba5d3a 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -4,6 +4,7 @@ import java.util.Scanner; public class Main { public static void main(String[] args) {System.out.println("Hello world!"); + Administration administration = new Administration(); Scanner scanner = new Scanner(System.in); boolean exit = false; @@ -23,6 +24,7 @@ public class Main { switch (choice) { case 1: System.out.println("Register User selected"); + administration.registerUser(); break; case 2: System.out.println("Enter Student ID and Course Code to enroll:"); From 3840659063e268253f8a27b1a7b08ad07a855043 Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Wed, 7 Feb 2024 10:10:19 +0100 Subject: [PATCH 03/10] Added isNumber Method to Administration class --- src/main/java/org/example/Administration.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/org/example/Administration.java b/src/main/java/org/example/Administration.java index 4e4c4f7..c9e5d90 100644 --- a/src/main/java/org/example/Administration.java +++ b/src/main/java/org/example/Administration.java @@ -208,6 +208,22 @@ public class Administration { } } + public boolean isNumber(String substring) + { + if(substring == null || substring.isEmpty()) + { + return false; + } + for(char c : substring.toCharArray()) + { + if(!Character.isDigit(c)) + { + return false; + } + } + return true; + } + } From 1334a57469af65ee0f893e72988186b159b8edc7 Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Wed, 7 Feb 2024 10:34:45 +0100 Subject: [PATCH 04/10] Added rightPrefix Method to Administration Class --- src/main/java/org/example/Administration.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/main/java/org/example/Administration.java b/src/main/java/org/example/Administration.java index c9e5d90..a27384c 100644 --- a/src/main/java/org/example/Administration.java +++ b/src/main/java/org/example/Administration.java @@ -223,6 +223,64 @@ public class Administration { } return true; } + public boolean rightPrefix(String userId, int i) + { + switch (i){ + case 1: + if(userId.startsWith("S")) + { + return true; + } + else if(userId.startsWith("s")) + { + 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; + } + break; + case 2: + if(userId.startsWith("A")) + { + return true; + } + else if(userId.startsWith("a")) + { + 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; + } + break; + case 3: + if(userId.startsWith("P")) + { + return true; + } + else if(userId.startsWith("p")) + { + 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; + } + break; + + default: + System.out.println("Error"); + break; + } + return false; + } } From 8dd3285cbba5638065bc171caba799d689cb4af0 Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Wed, 7 Feb 2024 10:38:04 +0100 Subject: [PATCH 05/10] Added exception handling for registerUser Method --- src/main/java/org/example/Administration.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/example/Administration.java b/src/main/java/org/example/Administration.java index a27384c..14b2ae9 100644 --- a/src/main/java/org/example/Administration.java +++ b/src/main/java/org/example/Administration.java @@ -185,8 +185,23 @@ public class Administration { } else if(findProfessorById(UserId) == null || findAdminById(UserId) == null || findStudentById(UserId) == null) { - System.out.println("The Id has not been used already!"); - correctData = true; + String substring = UserId.substring(1); + + if(!rightPrefix(UserId, userType)) + { + correctData = false; + } + else if(!isNumber(substring)) + { + System.out.println("Every character besides the first one need to be digits"); + correctData = false; + + } + else{ + System.out.println("The Id has not been used already!"); + correctData = true; + } + } switch (userType) From 8694e81cb0bdab7ebedbcc642131bdc9ed734c0b Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Wed, 7 Feb 2024 11:38:12 +0100 Subject: [PATCH 06/10] 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; } From d0ca2545fc6efbc6ee0723924ee53b78068c21f6 Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Wed, 7 Feb 2024 11:50:21 +0100 Subject: [PATCH 07/10] test: added isNumber test --- .../java/org/example/AdministrationTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/test/java/org/example/AdministrationTest.java b/src/test/java/org/example/AdministrationTest.java index 8edf5c7..1875272 100644 --- a/src/test/java/org/example/AdministrationTest.java +++ b/src/test/java/org/example/AdministrationTest.java @@ -20,4 +20,27 @@ class AdministrationTest { } + + @Test + void isNumber() { + + Administration administration = new Administration(); + String full01 = "S1002"; + String str1 = full01.substring(1); + String full02 = "S100Z"; + String str2 = full02.substring(1); + String full03 = "P1004"; + String str3 = full03.substring(1); + + + boolean test01 = administration.isNumber(str1); + boolean test02 = administration.isNumber(str2); + boolean test03 = administration.isNumber(str3); + + assertEquals(true, test01); + assertEquals(false, test02); + assertEquals(true, test03); + + } + } \ No newline at end of file From 067b7b4b832a4791e4c40f8ef7df678082fc11f6 Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Wed, 7 Feb 2024 11:54:13 +0100 Subject: [PATCH 08/10] test: added rightPrefix test --- .../java/org/example/AdministrationTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/test/java/org/example/AdministrationTest.java b/src/test/java/org/example/AdministrationTest.java index 1875272..745926d 100644 --- a/src/test/java/org/example/AdministrationTest.java +++ b/src/test/java/org/example/AdministrationTest.java @@ -43,4 +43,21 @@ class AdministrationTest { } + @Test + void rightPrefix() { + Administration administration = new Administration(); + + String full01 = "S1002"; + String full02 = "s100Z"; + String full03 = "X1004"; + + + boolean test01 = administration.rightPrefix(full01, 1); + boolean test02 = administration.rightPrefix(full02, 1); + boolean test03 = administration.rightPrefix(full03, 3); + + assertEquals(true, test01); + assertEquals(false, test02); + assertEquals(false, test03); + } } \ No newline at end of file From 697a44fcb3d076f9923cc02fe4fd713a19f6721a Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Wed, 7 Feb 2024 11:59:22 +0100 Subject: [PATCH 09/10] documentation: isNumber and rightPrefix test Methods --- .../java/org/example/AdministrationTest.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/test/java/org/example/AdministrationTest.java b/src/test/java/org/example/AdministrationTest.java index 745926d..048b51b 100644 --- a/src/test/java/org/example/AdministrationTest.java +++ b/src/test/java/org/example/AdministrationTest.java @@ -25,21 +25,21 @@ class AdministrationTest { void isNumber() { Administration administration = new Administration(); - String full01 = "S1002"; - String str1 = full01.substring(1); - String full02 = "S100Z"; - String str2 = full02.substring(1); - String full03 = "P1004"; - String str3 = full03.substring(1); - - + String full01 = "S1002"; //Supposed student id + 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 full03 = "P1004"; //Supposed professor id + 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); - assertEquals(true, test01); - assertEquals(false, test02); - assertEquals(true, test03); + assertEquals(true, test01); //only digits besides the first letter + assertEquals(false, test02); // false, because the Z at the end + assertEquals(true, test03); //only digits besides the first letter } @@ -47,14 +47,14 @@ class AdministrationTest { void rightPrefix() { Administration administration = new Administration(); - String full01 = "S1002"; - String full02 = "s100Z"; - String full03 = "X1004"; + String full01 = "S1002"; //Supposed Student id + String full02 = "s100Z"; //Supposed Student id + String full03 = "X1004"; // Supposed Professor id - boolean test01 = administration.rightPrefix(full01, 1); - boolean test02 = administration.rightPrefix(full02, 1); - boolean test03 = administration.rightPrefix(full03, 3); + boolean test01 = administration.rightPrefix(full01, 1); //checks if the id is correct, "1" because we are expecting a student id + boolean test02 = administration.rightPrefix(full02, 1); //checks if the id is correct, "1" because we are expecting a student id + boolean test03 = administration.rightPrefix(full03, 3); //checks if the id is correct, "3" because we are expecting a professor id assertEquals(true, test01); assertEquals(false, test02); From 0dd4950057d4141c01419f06618e16f6d8a5c230 Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Wed, 7 Feb 2024 12:16:45 +0100 Subject: [PATCH 10/10] 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