private static void AddMember(Role role) { Console.WriteLine("Enter username"); string username = Console.ReadLine(); if (string.IsNullOrEmpty(username)) { throw new Exception("You must enter username"); } Admin admin = SearchAdmins(username); Trainer trainer = SearchTrainers(username); Student student = SearchStudents(username); if (admin != null || trainer != null || student != null) { throw new Exception("User with this username already exists"); } Console.WriteLine("Enter first name"); string firstName = Console.ReadLine(); if (string.IsNullOrEmpty(firstName)) { throw new Exception("You must enter first name"); } Console.WriteLine("Enter last name"); string lastName = Console.ReadLine(); if (string.IsNullOrEmpty(lastName)) { throw new Exception("You must enter last name"); } int age = int.Parse(Console.ReadLine()); if (age < 16) { throw new Exception("Age must be greater than 16"); } if (role == Role.Admin) { Admins.Add(new Admin(username, firstName, lastName, age)); } else if (role == Role.Trainer) { Trainers.Add(new Trainer(username, firstName, lastName, age)); } else { Student newStudent = new Student(username, firstName, lastName, age); Console.WriteLine("Enter subject name"); string currentSubject = Console.ReadLine(); Subject current = Subjects.FirstOrDefault(x => x.Name == currentSubject); newStudent.CurrentSubject = current; Students.Add(newStudent); } }
static void Main(string[] args) { PopulateDatabase(); while (true) { try { Console.WriteLine("Enter username"); string userName = Console.ReadLine(); if (string.IsNullOrEmpty(userName)) { throw new Exception("You must enter username"); } //first check admins Admin admin = SearchAdmins(userName); if (admin != null) { Console.WriteLine("" + "1) Add teacher," + "2) Remove teacher," + "3) Add student," + "4) Remove student," + "5) Add admin," + "6) Remove admin"); int option = int.Parse(Console.ReadLine()); switch (option) { case 1: AddMember(Role.Trainer); break; case 2: Console.WriteLine("Enter username"); string username = Console.ReadLine(); RemoveTrainer(username); break; case 3: AddMember(Role.Student); break; case 4: Console.WriteLine("Enter username"); string studentUsername = Console.ReadLine(); RemoveStudent(studentUsername); break; case 5: AddMember(Role.Admin); break; case 6: Console.WriteLine("Enter username"); string adminUsername = Console.ReadLine(); if (adminUsername == admin.UserName) { throw new Exception("You can not delete yourself"); } RemoveAdmin(adminUsername); break; default: throw new Exception("You must enter valid option 1-6"); } } else { //not an admin Trainer trainer = Trainers.FirstOrDefault(x => x.UserName == userName); if (trainer != null) { //trainer Console.WriteLine("Enter option 1) See students 2) See subjects"); int trainerOption = int.Parse(Console.ReadLine()); switch (trainerOption) { case 1: PrintStudents(); break; case 2: PrintSubjects(); break; default: throw new Exception("You must enter valid option 1/2"); } } else { //not an admin, not a trainer Student student = Students.FirstOrDefault(x => x.UserName == userName); if (student == null) { throw new Exception("The user does not exist"); } student.PrintDetails(); } } } catch (FormatException ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Input was not a number"); Console.WriteLine(ex.Message); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("An error occured"); Console.WriteLine(ex.Message); } Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Would you like to continue? Type yes"); string response = Console.ReadLine(); if (response.ToLower() == "yes") { continue; } else { break; } } Console.ReadLine(); }