matin mortazavi / i did my Assignment + bonus
This commit is contained in:
@@ -10,5 +10,20 @@ public class Main {
|
|||||||
// 5. Register the student in some courses
|
// 5. Register the student in some courses
|
||||||
// 6. Drop one course
|
// 6. Drop one course
|
||||||
// 7. Print the final registered course list
|
// 7. Print the final registered course list
|
||||||
|
RegistrationSystem reg = new RegistrationSystem() ;
|
||||||
|
Course Ap = new Course("ap" , "1404" , 2 );
|
||||||
|
reg.addCourseToSystem(Ap) ;
|
||||||
|
Course DS = new Course("DS" , "2201" , 1 );
|
||||||
|
reg.addCourseToSystem(DS);
|
||||||
|
Course DB = new Course("Db" , "3301" , 3 );
|
||||||
|
reg.addCourseToSystem(DB);
|
||||||
|
Student Ali = new Student("ali" , "402222001") ;
|
||||||
|
Ali.addCourse(Ap) ;
|
||||||
|
Ali.addCourse(DB) ;
|
||||||
|
Ali.dropCourse(Ap) ;
|
||||||
|
Ali.showRegisteredCourses();
|
||||||
|
reg.showcapacity();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package CourseRegistrationStarter;
|
package CourseRegistrationStarter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class RegistrationSystem {
|
public class RegistrationSystem {
|
||||||
private ArrayList<Course> courses;
|
private ArrayList<Course> courses;
|
||||||
@@ -10,15 +11,61 @@ public class RegistrationSystem {
|
|||||||
|
|
||||||
public void addCourseToSystem(Course c) {
|
public void addCourseToSystem(Course c) {
|
||||||
// TODO: Add the course to the system
|
// TODO: Add the course to the system
|
||||||
|
if (courses.contains(c)) {
|
||||||
|
System.out.println("you cant add this course !!");
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
courses.add(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public Course findCourseByCode(String code) {
|
public Course findCourseByCode(String code) {
|
||||||
// TODO: Search for a course by course code
|
// TODO: Search for a course by course code
|
||||||
// Return the matching course if found, otherwise return null
|
// Return the matching course if found, otherwise return null
|
||||||
|
for (Course item : courses)
|
||||||
|
{
|
||||||
|
if (Objects.equals(item.getCourseCode(), code)) {
|
||||||
|
|
||||||
|
return item ;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
|
||||||
|
}
|
||||||
|
public Course findCourseByName(String name)
|
||||||
|
{
|
||||||
|
for (Course item : courses)
|
||||||
|
{
|
||||||
|
if (Objects.equals(item.getCourseCode(), name)) {
|
||||||
|
|
||||||
|
return item ;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showAllCourses() {
|
public void showAllCourses() {
|
||||||
// TODO: Print all courses in the system
|
// TODO: Print all courses in the system
|
||||||
|
for (Course item : courses)
|
||||||
|
{
|
||||||
|
System.out.print("course name : ");
|
||||||
|
System.out.print(item.getCourseName());
|
||||||
|
System.out.print(" , course code");
|
||||||
|
System.out.print(item.getCourseCode());
|
||||||
|
System.out.print(" , course capacity");
|
||||||
|
System.out.print(item.getCapacity());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void showcapacity(){
|
||||||
|
for (Course item : courses)
|
||||||
|
{
|
||||||
|
System.out.println(item.getCapacity());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,17 +23,46 @@ public class Student {
|
|||||||
public boolean addCourse(Course c) {
|
public boolean addCourse(Course c) {
|
||||||
// TODO: Register the student in the course if capacity allows
|
// TODO: Register the student in the course if capacity allows
|
||||||
// Return true if registration was successful, otherwise false
|
// Return true if registration was successful, otherwise false
|
||||||
|
if (registeredCourses.size()<= 10) {
|
||||||
|
if (c.reduceCapacity()) {
|
||||||
|
registeredCourses.add(c);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public boolean dropCourse(Course c) {
|
public boolean dropCourse(Course c) {
|
||||||
// TODO: Remove the course from the student's list
|
// TODO: Remove the course from the student's list
|
||||||
// Restore the course capacity if removal was successful
|
// Restore the course capacity if removal was successful
|
||||||
// Return true if the course was dropped, otherwise false
|
// Return true if the course was dropped, otherwise false
|
||||||
|
int ind = registeredCourses.indexOf(c) ;
|
||||||
|
if (ind != -1 )
|
||||||
|
{
|
||||||
|
registeredCourses.remove(ind) ;
|
||||||
|
c.increaseCapacity();
|
||||||
|
return true ;
|
||||||
|
}
|
||||||
|
else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void showRegisteredCourses() {
|
public void showRegisteredCourses() {
|
||||||
// TODO: Print all registered courses
|
// TODO: Print all registered courses
|
||||||
|
for (Course item : registeredCourses)
|
||||||
|
{
|
||||||
|
System.out.print("course name : ");
|
||||||
|
System.out.print(item.getCourseName());
|
||||||
|
System.out.print(" , course code");
|
||||||
|
System.out.print(item.getCourseCode());
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user