implement student and registration logic #7

Closed
AmirAli_Amiri wants to merge 1 commits from AmirAli_Amiri/WS-02-intro-to-oop:develop into main
3 changed files with 64 additions and 21 deletions
+26 -8
View File
@@ -2,13 +2,31 @@ package CourseRegistrationStarter;
public class Main {
public static void main(String[] args) {
// TODO:
// 1. Create at least 3 courses
// 2. Create 1 student
// 3. Create a registration system
// 4. Add courses to the system
// 5. Register the student in some courses
// 6. Drop one course
// 7. Print the final registered course list
// تعریف ۳ درس
Course c1 = new Course("Advanced Programming" , "AP101" , 25);
Course c2 = new Course("Quantum Mechanics" , "QM202" , 20);
Course c3 = new Course("Electromagnetism" , "EL303" , 28);
//تعریف یک دانشجو
Student student = new Student("AmirAli Amiri" , "402216005");
// ایجاد سیستم ثبت نام
RegistrationSystem system = new RegistrationSystem();
// اضافه کردن دروس به سیستم ثبت نام
system.addCourseToSystem(c1);
system.addCourseToSystem(c2);
system.addCourseToSystem(c3);
// نشان دادن همه دروس ثبت شده در سیستم
system.showAllCourses();
// اضافه کردن دو درس به لیست درس دانشجو
System.out.println("شروع انتخاب واحد");
student.addCourse(c1);
student.addCourse(c3);
// نشان دادن همه دروس ثبت شده در لیست دروس دانشجو
student.showRegisteredCourses();
// حذف کردن یکی از دروس ثبت شده
System.out.println("فرایند حذف واحد");
student.dropCourse(c1);
// نشان دادن همه دروس ثبت شده در لیست دروس دانشجو و سیستم
student.showRegisteredCourses();
system.showAllCourses();
}
}
@@ -8,17 +8,26 @@ public class RegistrationSystem {
courses = new ArrayList<>();
}
public void addCourseToSystem(Course c) {
// TODO: Add the course to the system
public void addCourseToSystem(Course c) { // درس را به سیستم مرکزی اضافه می کند
if (!courses.contains(c)){
courses.add(c);
}
}
public Course findCourseByCode(String code) {
// TODO: Search for a course by course code
// Return the matching course if found, otherwise return null
public Course findCourseByCode(String code) { // درس را بر اساس کد درس پیدا می کند
for ( Course c : courses){
if ( c.getCourseCode().equalsIgnoreCase(code)){
return c;
}
}
return null;
}
public void showAllCourses() {
// TODO: Print all courses in the system
public void showAllCourses() { // تمامی دروس ثبت شده در سیستم را نشان می دهد
System.out.println("لیست تمام دروس موجود در سیستم \n");
for(Course c : courses){
System.out.println("نام درس : " + c.getCourseName()+ "|کد درس :" + c.getCourseCode() + "| ظرفیت باقی مانده درس : " + c.getCapacity());
}
}
}
+22 -6
View File
@@ -21,19 +21,35 @@ public class Student {
}
public boolean addCourse(Course c) {
// TODO: Register the student in the course if capacity allows
// Return true if registration was successful, otherwise false
if(!registeredCourses.contains(c) && c.reduceCapacity()){ // ابتدا چک می کنیم که فرد مورد نظر درس را قبلا اخذ نکرده باشد و درس ظرفیت داشته باشد
registeredCourses.add(c); // اگر شرط بالا رعایت شده باشد درس را اضافه میکند
System.out.println(" درس " + c.getCourseName() + "با موفقیت اخذ شد .");
return true;
}
System.out.println("ظرفیت درس تکمیل است یا درس قبلا اخذ شده است .");
return false;
}
public boolean dropCourse(Course c) {
// TODO: Remove the course from the student's list
// Restore the course capacity if removal was successful
// Return true if the course was dropped, otherwise false
if(registeredCourses.contains(c)){ // اگر درس در لیست دروس فرد باشد آن را حذف می کند و ظرفیت درس را افزایش می دهد
registeredCourses.remove(c);
c.increaseCapacity();
System.out.println("درس با موفقیت حذف شد!");
return true;
}
System.out.println("درس مورد نظر در لیست دروس شما یافت نشد!");
return false;
}
public void showRegisteredCourses() {
// TODO: Print all registered courses
System.out.println("لیست دروس ثبت نام شده برای " + name + "به شماره دانشجویی" + studentId ); // لیست تمامی دروسی که فرد اخذ کرده است را نشان می دهد
if(registeredCourses.isEmpty()){
System.out.println("هیچ درسی ثبت نام نکرده اید!" );
}
else{
for ( Course c : registeredCourses){
System.out.println("- درس " + c.getCourseName() + " به شماره درس " + c.getCourseCode());
}
}
}
}