4 Commits
4 changed files with 81 additions and 41 deletions
+28 -4
View File
@@ -70,7 +70,12 @@ public class Main {
boolean success; boolean success;
switch (choice) { switch (choice) {
case 1: case 1:
//TODO success = school.registerStudent(username, password);
if (success) {
System.out.println(" Student registered successfully!");
} else {
System.out.println(" Username already exists!");
}
break; break;
case 2: case 2:
success = school.registerTeacher(username, password); success = school.registerTeacher(username, password);
@@ -103,7 +108,12 @@ public class Main {
switch (choice) { switch (choice) {
case 1: case 1:
//TODO Student student = school.loginStudent(username, password);
if (student != null) {
studentMenu(student);
} else {
System.out.println(" Invalid username or password!");
}
break; break;
case 2: case 2:
Teacher teacher = school.loginTeacher(username, password); Teacher teacher = school.loginTeacher(username, password);
@@ -141,7 +151,7 @@ public class Main {
enrollInCourse(student); enrollInCourse(student);
break; break;
case 3: case 3:
//System.out.print(student.getMyCoursesDisplay()); System.out.print(student.getMyCoursesDisplay());
break; break;
case 4: case 4:
System.out.println(" Goodbye, " + student.getUsername() + "!"); System.out.println(" Goodbye, " + student.getUsername() + "!");
@@ -153,7 +163,21 @@ public class Main {
} }
private static void enrollInCourse(Student student) { private static void enrollInCourse(Student student) {
//TODO System.out.print(school.getAllCoursesDisplay());
System.out.print("\nEnter Course ID to enroll: ");
int courseId = getIntInput();
Course course = school.findCourseById(courseId);
if (course == null) {
System.out.println(" Course not found! Please enter a valid ID.");
return;
}
if (student.enrollInCourse(course)) {
System.out.println(" Successfully enrolled in " + course.getName());
} else {
System.out.println(" You are already enrolled in this course!");
}
} }
// ==================== TEACHER MENU ==================== // ==================== TEACHER MENU ====================
+19 -11
View File
@@ -11,20 +11,28 @@ public class Student extends User {
this.enrolledCourses = new ArrayList<>(); this.enrolledCourses = new ArrayList<>();
} }
public void enrollCourse(Course course) { public boolean enrollInCourse(Course course) {
this.enrolledCourses.add(course); if (enrolledCourses.contains(course)) {
} return false;
public void showMyCourses() {
for (Course course : this.enrolledCourses) {
System.out.println(course);
} }
enrolledCourses.add(course);
course.addStudent(this);
return true;
} }
public Course getEnrolledCourses(int id) {
for (Course course : this.enrolledCourses) { public List<Course> getEnrolledCourses() {
if (course.getId() == id) { return enrolledCourses;
return course; }
public String getMyCoursesDisplay() {
if (enrolledCourses.isEmpty()) {
return " You are not enrolled in any course yet.";
} else {
StringBuilder sb = new StringBuilder("\n My Courses:\n");
for (int i = 0; i < enrolledCourses.size(); i++) {
sb.append(" ").append(i+1).append(". ").append(enrolledCourses.get(i)).append("\n");
} }
return sb.toString();
} }
return null;
} }
} }
+12 -8
View File
@@ -32,22 +32,26 @@ public class Teacher extends User {
return myCourses; return myCourses;
} }
public void showMyCourses() { public String getMyCoursesDisplay() {
if (myCourses.isEmpty()) { if (myCourses.isEmpty()) {
System.out.println(" You don't teach any course yet."); return " You don't teach any course yet.";
} else { } else {
System.out.println("\n My Courses:"); StringBuilder sb = new StringBuilder("\n My Courses:\n");
for (int i = 0; i < myCourses.size(); i++) { for (int i = 0; i < myCourses.size(); i++) {
System.out.println(" " + (i+1) + ". " + myCourses.get(i)); sb.append(" ").append(i+1).append(". ").append(myCourses.get(i)).append("\n");
} }
return sb.toString();
} }
} }
public void showStudentsInCourse(Course course) { public boolean checkCourseOwnership(Course course) {
return myCourses.contains(course);
}
public String getStudentsDisplayForCourse(Course course) {
if (!myCourses.contains(course)) { if (!myCourses.contains(course)) {
System.out.println(" You don't teach this course!"); return "You don't teach this course!";
return;
} }
course.showStudents(); return course.getStudentsDisplay();
} }
} }
+22 -18
View File
@@ -16,23 +16,25 @@ public class SchoolSystem {
} }
// ========== Student Management ========== // ========== Student Management ==========
public void registerStudent(String username, String password) { public boolean registerStudent(String username, String password) {
Student student = new Student(username, password); if (findStudent(username) != null) {
this.students.add(student); return false;
}
students.add(new Student(username, password));
return true;
} }
public Student loginStudent(String username, String password) { public Student loginStudent(String username, String password) {
for (Student student : this.students) { Student student = findStudent(username);
if (student.getUsername().equals(username) && student.checkPassword(password)) { if (student != null && student.checkPassword(password)) {
return student; return student;
}
} }
return null; return null;
} }
private Student findStudent(String username) { private Student findStudent(String username) {
for (Student student : this.students) { for (Student s : students) {
if (student.getUsername().equals(username)) { if (s.getUsername().equals(username)) return s;
return student;
}
} }
return null; return null;
} }
@@ -63,17 +65,19 @@ public class SchoolSystem {
// ========== Course Management ========== // ========== Course Management ==========
public boolean addCourseToSystem(String courseName) { public boolean addCourseToSystem(String courseName) {
Course course = new Course(courseName); for (Course c : allCourses) {
this.allCourses.add(course); if (c.getName().equals(courseName)) {
return false;
}
}
allCourses.add(new Course(courseName));
return true; return true;
} }
public Course findCourseById(int id) { public Course findCourseById(int id) {
for (Course course : this.allCourses) { for (Course c : allCourses) {
if(course.getId() == id) { if (c.getId() == id) return c;
return course; }
}
}
return null; return null;
} }