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