workshop 2
This commit is contained in:
@@ -3,12 +3,24 @@ package CourseRegistrationStarter;
|
|||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// TODO:
|
// TODO:
|
||||||
// 1. Create at least 3 courses
|
Course c1 =new Course("Advanced Programming","AP1404",2);
|
||||||
// 2. Create 1 student
|
Course c2 =new Course("Data Structures","DS2201",1);
|
||||||
// 3. Create a registration system
|
Course c3=new Course("Database Systems","DB3301",3);
|
||||||
// 4. Add courses to the system
|
|
||||||
// 5. Register the student in some courses
|
Student s1=new Student("Ali","402222001");
|
||||||
// 6. Drop one course
|
|
||||||
// 7. Print the final registered course list
|
RegistrationSystem system= new RegistrationSystem();
|
||||||
|
|
||||||
|
system.addCourseToSystem(c1);
|
||||||
|
system.addCourseToSystem(c2);
|
||||||
|
system.addCourseToSystem(c3);
|
||||||
|
|
||||||
|
s1.addCourse(c1);
|
||||||
|
s1.addCourse(c2);
|
||||||
|
|
||||||
|
s1.dropCourse(c1);
|
||||||
|
|
||||||
|
s1.showRegisteredCourses();
|
||||||
|
system.showAllCourses();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,15 +10,25 @@ 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
|
||||||
|
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
|
for (Course c:courses){
|
||||||
|
if (c.getCourseCode().equals(code)){
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
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 c:courses){
|
||||||
|
System.out.println(
|
||||||
|
c.getCourseName()+"-"+ c.getCourseCode()+"-capacity:"+c.getCapacity()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ public class Student {
|
|||||||
private String name;
|
private String name;
|
||||||
private String studentId;
|
private String studentId;
|
||||||
private ArrayList<Course> registeredCourses;
|
private ArrayList<Course> registeredCourses;
|
||||||
|
private int maxCourses = 3;
|
||||||
|
|
||||||
public Student(String name, String studentId) {
|
public Student(String name, String studentId) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@@ -21,19 +22,50 @@ public class Student {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean addCourse(Course c) {
|
public boolean addCourse(Course c) {
|
||||||
|
//bonus1:جلوگیری از ثبت نام تکراری
|
||||||
|
for (Course course : registeredCourses) {
|
||||||
|
if (course.getCourseCode().equals(c.getCourseCode())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//bonus2:محدودیت برای تکرار
|
||||||
|
if (registeredCourses.size() >= maxCourses) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
// 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
|
if (c.getCapacity()>0){
|
||||||
|
registeredCourses.add(c);
|
||||||
|
c.reduceCapacity();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
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
|
if (registeredCourses.contains(c)){
|
||||||
// Return true if the course was dropped, otherwise false
|
registeredCourses.remove(c);
|
||||||
|
c.increaseCapacity();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showRegisteredCourses() {
|
public void showRegisteredCourses() {
|
||||||
// TODO: Print all registered courses
|
// TODO: Print all registered courses
|
||||||
|
System.out.println("Registered courses for"+name+":");
|
||||||
|
for (Course c :registeredCourses){
|
||||||
|
System.out.println(c.getCourseName()+"-"+ c.getCourseCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//bonus3
|
||||||
|
public Course findCourseByName(String name){
|
||||||
|
|
||||||
|
for (Course c: registeredCourses){
|
||||||
|
if (c.getCourseName().equalsIgnoreCase(name)){
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user