4 Commits
3 changed files with 44 additions and 43 deletions
+20 -13
View File
@@ -71,10 +71,9 @@ public class Main {
switch (choice) {
case 1:
success = school.registerStudent(username, password);
if(success){
if (success) {
System.out.println(" Student registered successfully!");
}
else{
} else {
System.out.println(" Username already exists!");
}
break;
@@ -110,10 +109,9 @@ public class Main {
switch (choice) {
case 1:
Student student = school.loginStudent(username, password);
if(student != null){
if (student != null) {
studentMenu(student);
}
else{
} else {
System.out.println(" Invalid username or password!");
}
break;
@@ -153,7 +151,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() + "!");
@@ -165,12 +163,21 @@ public class Main {
}
private static void enrollInCourse(Student student) {
System.out.println("\n Select a course:\n");
school.getAllCoursesDisplay();
System.out.println();
String courseName = scanner.toString();
Course course = school.findCourseByName(courseName);
student.enrollInCourse(course);
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 ====================
+12 -14
View File
@@ -11,12 +11,8 @@ public class Student extends User {
this.enrolledCourses = new ArrayList<>();
}
public List<Course> getEnrolledCourses() {
return enrolledCourses;
}
public boolean enrollInCourse(Course course){
if(enrolledCourses.contains(course)){
public boolean enrollInCourse(Course course) {
if (enrolledCourses.contains(course)) {
return false;
}
enrolledCourses.add(course);
@@ -24,17 +20,19 @@ public class Student extends User {
return true;
}
public String getMyCoursesDisplay(){
if(enrolledCourses.isEmpty()){
return "You are not enrolled in any course yet.";
}
else{
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(enrolledCourses.get(i)).append("\n");
for (int i = 0; i < enrolledCourses.size(); i++) {
sb.append(" ").append(i+1).append(". ").append(enrolledCourses.get(i)).append("\n");
}
return sb.toString();
}
}
}
+12 -16
View File
@@ -16,27 +16,25 @@ public class SchoolSystem {
}
// ========== Student Management ==========
public boolean registerStudent(String username, String password){
if(findStudent(username) != null){
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){
public Student loginStudent(String username, String password) {
Student student = findStudent(username);
if(student != null && student.checkPassword(password)){
if (student != null && student.checkPassword(password)) {
return student;
}
return null;
}
public Student findStudent(String username){
for(Student s : students){
if(s.getUsername().equals(username)){
return s;
}
private Student findStudent(String username) {
for (Student s : students) {
if (s.getUsername().equals(username)) return s;
}
return null;
}
@@ -67,8 +65,8 @@ public class SchoolSystem {
// ========== Course Management ==========
public boolean addCourseToSystem(String courseName) {
for(Course c : allCourses){
if(c.getName().equals(courseName)){
for (Course c : allCourses) {
if (c.getName().equals(courseName)) {
return false;
}
}
@@ -77,11 +75,9 @@ public class SchoolSystem {
}
public Course findCourseById(int id) {
for(Course c : allCourses){
if(c.getId() == id){
return c;
}
}
for (Course c : allCourses) {
if (c.getId() == id) return c;
}
return null;
}