diff --git a/src/main/java/org/example/Administration.java b/src/main/java/org/example/Administration.java index 2be870c..8e6053b 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,166 @@ public class Administration { return null; } + /** + * Method to register a User + */ + public void registerUser() + { + //Instance variables + + 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(); + + //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!"); + + } + else if(findProfessorById(UserId) == null || findAdminById(UserId) == null || findStudentById(UserId) == null) + { + //the entered id is not used + + + if(!rightPrefix(UserId, userType)) + { + //the prefix is wrong + + } + else if(!isNumber(UserId)) + { + //the substring does not contain only digits + System.out.println("Every character besides the first one need to be digits"); + + + } + 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; + } + + } + + switch (userType) + { + case 1: //Student + Student student = new Student(UserName, UserId, "Student"); + addStudents(student); + break; + case 2: //Admin + Admin admin = new Admin(UserName, UserId, "Admin"); + addAdmin(admin); + break; + case 3: //Professor + Professor professor = new Professor(UserId, UserName, "Professor"); + addProfessor(professor); + default: //Error + break; + } + } + } + + /** + * + * @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 userId) + { + //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)) + { + return false; + } + } + 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: //Student + 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; + } + else + { + return true; + } + case 2: //Admin + 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; + } + else + { + return true; + } + case 3: //Professor + 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; + } + else + { + return true; + } + + default: //Error + System.out.println("Error"); + break; + } + return false; + } + } 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:");