the workshop thecher and studend courses #1
+25
-3
@@ -70,7 +70,12 @@ public class Main {
|
||||
boolean success;
|
||||
switch (choice) {
|
||||
case 1:
|
||||
//TODO
|
||||
success = school.registerStudent(username, password);
|
||||
if(success){
|
||||
System.out.println(" Student registered successfully!");
|
||||
}else {
|
||||
System.out.println(" Username already exists!");
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
success = school.registerTeacher(username, password);
|
||||
@@ -103,7 +108,12 @@ public class Main {
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
//TODO
|
||||
Student student = school.loginStudent(username , password);
|
||||
if(student != null){
|
||||
studentMenu(student);
|
||||
}else{
|
||||
System.out.println(" Invalid username or password!");
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
Teacher teacher = school.loginTeacher(username, password);
|
||||
@@ -153,7 +163,19 @@ public class Main {
|
||||
}
|
||||
|
||||
private static void enrollInCourse(Student student) {
|
||||
//TODO
|
||||
System.out.print(school.getAllCoursesDisplay());
|
||||
System.out.print("\n Enter 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.enrolledCourses(course)){
|
||||
System.out.println("Successfully enrolled in" + course.getName());
|
||||
}else{
|
||||
System.out.println(" You are already enrolled in this course!");
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== TEACHER MENU ====================
|
||||
|
||||
@@ -11,5 +11,28 @@ public class Student extends User {
|
||||
this.enrolledCourses = new ArrayList<>();
|
||||
}
|
||||
|
||||
//TODO
|
||||
public boolean enrolledCourses(Course course){
|
||||
if(enrolledCourses.contains(course)){
|
||||
return false;
|
||||
}
|
||||
enrolledCourses.add(course);
|
||||
course.addStudent(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
public List <Course> getEnrolledCourses(){
|
||||
return enrolledCourses;
|
||||
}
|
||||
|
||||
public String getMyCoursesDisplay(){
|
||||
if(enrolledCourses.isEmpty()){
|
||||
return " You are not enrolled in any course yet.";
|
||||
}else{
|
||||
StringBuilder sb = new StringBuilder("\n My Course: \n");
|
||||
for (int i = 0; i < enrolledCourses.size(); i++) {
|
||||
sb.append(" ").append(i+1).append(". ").append(enrolledCourses.get(i)).append("\n");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,22 +32,26 @@ public class Teacher extends User {
|
||||
return myCourses;
|
||||
}
|
||||
|
||||
public void showMyCourses() {
|
||||
public String getMyCoursesDisplay() {
|
||||
if (myCourses.isEmpty()) {
|
||||
System.out.println(" You don't teach any course yet.");
|
||||
return " You don't teach any course yet.";
|
||||
} else {
|
||||
System.out.println("\n My Courses:");
|
||||
StringBuilder sb = new StringBuilder("\n My Courses:\n");
|
||||
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) {
|
||||
if (!myCourses.contains(course)) {
|
||||
System.out.println(" You don't teach this course!");
|
||||
return;
|
||||
public boolean checkCourseOwnership(Course course) {
|
||||
return myCourses.contains(course);
|
||||
}
|
||||
course.showStudents();
|
||||
|
||||
public String getStudentsDisplayForCourse(Course course) {
|
||||
if (!myCourses.contains(course)) {
|
||||
return "You don't teach this course!";
|
||||
}
|
||||
return course.getStudentsDisplay();
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,28 @@ public class SchoolSystem {
|
||||
}
|
||||
|
||||
// ========== Student Management ==========
|
||||
//TODO
|
||||
public boolean registerStudent(String username , String password){
|
||||
if(findStudent(username) != null){
|
||||
return false;
|
||||
}
|
||||
students.add(new Student(username , password));
|
||||
return true;
|
||||
}
|
||||
|
||||
public Student loginStudent(String username, String password) {
|
||||
Student student = findStudent(username);
|
||||
if (student != null && student.checkPassword(password)) {
|
||||
return student;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Student findStudent(String username) {
|
||||
for (Student s : students) {
|
||||
if (s.getUsername().equals(username)) return s;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ========== Teacher Management ==========
|
||||
public boolean registerTeacher(String username, String password) {
|
||||
@@ -44,12 +65,21 @@ public class SchoolSystem {
|
||||
|
||||
// ========== Course Management ==========
|
||||
public boolean addCourseToSystem(String courseName) {
|
||||
//TODO
|
||||
for (Course c : allCourses){
|
||||
if(c.getName().equals(courseName)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
allCourses.add(new Course(courseName));
|
||||
return true;
|
||||
}
|
||||
|
||||
public Course findCourseById(int id) {
|
||||
//TODO
|
||||
for(Course c : allCourses){
|
||||
if (c.getId() == id){
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user