Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a97f6a3717 | ||
|
|
ea649bf1fa | ||
|
|
b6c2286570 | ||
|
|
747406a5aa |
Generated
+1
@@ -0,0 +1 @@
|
|||||||
|
Student.java
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
package ClassExamples;
|
package ClassExamples;
|
||||||
|
import CourseRegistrationStarter.Course;
|
||||||
|
|
||||||
public class Example01
|
public class Example01
|
||||||
{
|
{
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
{
|
{
|
||||||
System.out.println("Hi!");
|
Course Advanced_Programming = new Course("AdvancedProgramming", "AP1404", 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,13 +2,28 @@ package CourseRegistrationStarter;
|
|||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// TODO:
|
Course AdvancedProgramming = new Course("Advanced Programming", "AP1404", 2);
|
||||||
// 1. Create at least 3 courses
|
Course DataStructures = new Course("Data Structures", "DS2201", 1);
|
||||||
// 2. Create 1 student
|
Course DatabaseSystems = new Course("Database Systems", "DB3301", 3);
|
||||||
// 3. Create a registration system
|
|
||||||
// 4. Add courses to the system
|
Student Zahra = new Student("Zahra", "404222093");
|
||||||
// 5. Register the student in some courses
|
|
||||||
// 6. Drop one course
|
RegistrationSystem TestSystem = new RegistrationSystem();
|
||||||
// 7. Print the final registered course list
|
|
||||||
|
TestSystem.addCourseToSystem(AdvancedProgramming);
|
||||||
|
TestSystem.addCourseToSystem(DataStructures);
|
||||||
|
TestSystem.addCourseToSystem(DatabaseSystems);
|
||||||
|
|
||||||
|
System.out.print("Adding Advanced Programming: ");
|
||||||
|
System.out.println(Zahra.addCourse(AdvancedProgramming));
|
||||||
|
System.out.print("Adding Data Structures: ");
|
||||||
|
System.out.println(Zahra.addCourse(DataStructures));
|
||||||
|
System.out.print("Adding Database Systems: ");
|
||||||
|
System.out.println(Zahra.addCourse(DatabaseSystems));
|
||||||
|
|
||||||
|
System.out.print("Dropping Data Structures: ");
|
||||||
|
System.out.println(Zahra.dropCourse(DataStructures));
|
||||||
|
|
||||||
|
Zahra.showRegisteredCourses();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package CourseRegistrationStarter;
|
package CourseRegistrationStarter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class RegistrationSystem {
|
public class RegistrationSystem {
|
||||||
private ArrayList<Course> courses;
|
private ArrayList<Course> courses;
|
||||||
@@ -9,16 +10,37 @@ public class RegistrationSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addCourseToSystem(Course c) {
|
public void addCourseToSystem(Course c) {
|
||||||
// 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
|
for (Course i : courses)
|
||||||
// Return the matching course if found, otherwise return null
|
{
|
||||||
|
if (i.getCourseCode().equals(code))
|
||||||
|
{
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Course findCourseByName(String name) {
|
||||||
|
for (Course i : courses)
|
||||||
|
{
|
||||||
|
if (i.getCourseName().equals(name))
|
||||||
|
{
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showAllCourses() {
|
public void showAllCourses() {
|
||||||
// TODO: Print all courses in the system
|
for (Course i : courses)
|
||||||
|
{
|
||||||
|
System.out.println(i.getCourseName());
|
||||||
|
System.out.println(i.getCourseCode());
|
||||||
|
System.out.println(i.getCapacity());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -21,19 +21,32 @@ public class Student {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean addCourse(Course c) {
|
public boolean addCourse(Course c) {
|
||||||
// TODO: Register the student in the course if capacity allows
|
if ((registeredCourses.size() < 3) && (!(registeredCourses.contains(c)) && (c.getCapacity() > 0))) {
|
||||||
// Return true if registration was successful, otherwise false
|
registeredCourses.add(c);
|
||||||
return false;
|
} else if (registeredCourses.size() >= 3)
|
||||||
|
System.out.println("You can only attend 3 courses!");
|
||||||
|
else if (registeredCourses.contains(c))
|
||||||
|
System.out.println("You can't register twice!");
|
||||||
|
return c.reduceCapacity();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean dropCourse(Course c) {
|
public boolean dropCourse(Course c) {
|
||||||
// TODO: Remove the course from the student's list
|
if (registeredCourses.contains(c)) {
|
||||||
// Restore the course capacity if removal was successful
|
registeredCourses.remove(c);
|
||||||
// Return true if the course was dropped, otherwise false
|
c.increaseCapacity();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showRegisteredCourses() {
|
public void showRegisteredCourses() {
|
||||||
// TODO: Print all registered courses
|
System.out.println("_________________________________");
|
||||||
|
System.out.println("You participate in:");
|
||||||
|
for (Course i : registeredCourses) {
|
||||||
|
System.out.println("=======================");
|
||||||
|
System.out.println(i.getCourseName());
|
||||||
|
System.out.print("Class ID: ");
|
||||||
|
System.out.println(i.getCourseCode());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user