2 Commits
3 changed files with 152 additions and 20 deletions
+98 -8
View File
@@ -1,14 +1,104 @@
package CourseRegistrationStarter; package CourseRegistrationStarter;
import java.util.Scanner;
public class Main { public class Main {
static Scanner input = new Scanner(System.in);
static Course x1 =new Course("Advanced Programming","AP1404",2);
static Course x2 =new Course("Data Structures","DS2201",1);
static Course x3 =new Course("Database Systems","DB3301",3);
static RegistrationSystem n3 = new RegistrationSystem();
static Student st = new Student("navid","41148");
public static void main(String[] args) { public static void main(String[] args) {
// TODO: n3.addCourseToSystem(x1);
// 1. Create at least 3 courses n3.addCourseToSystem(x2);
// 2. Create 1 student n3.addCourseToSystem(x3);
// 3. Create a registration system
// 4. Add courses to the system
// 5. Register the student in some courses while(true){
// 6. Drop one course asli();
// 7. Print the final registered course list }
}
public static boolean asli(){
System.out.println("----Course Registration Menu ---"+"\n"+"1. Show all courses "+"\n"+"2. Register course"+"\n"+"3. Drop course"+"\n"+"4. Show my courses"+"\n"+"5. Exit"+"\n"+"Choose an option: ");
int n1 = input.nextInt();
if(n1 == 1){
System.out.println("All courses in system: ");
n3.showAllCourses();
return (true);
}else if (n1 ==2){
System.out.println("Choos an option: ");
System.out.println("1. Enter course code ");
System.out.println("2. Enter course name ");
int n2 = input.nextInt();
input.nextLine();
if (n2 == 1){
System.out.println("Enter course code to register: ");
String courseCode = input.nextLine();
if(n3.findCourseByCode(courseCode) != null ){
if (st.addCourse(n3.findCourseByCode(courseCode))){
System.out.println("Registration was successful."+"\n");
return(true);
}
}else{
System.out.println("No course found with this code!"+"\n");
return(true);
}
}else if (n2 == 2){
System.out.println("Enter course name to register: ");
String courseName = input.nextLine();
if(n3.findCourseByName(courseName) != null ){
if (st.addCourse(n3.findCourseByName(courseName))){
System.out.println("Registration was successful."+"\n");
return(true);
}
}else{
System.out.println("No course found with this name!"+"\n");
return(true);
}
}
}else if (n1==3){
System.out.println("Choos an option: ");
System.out.println("1. Enter course code ");
System.out.println("2. Enter course name ");
int n2 = input.nextInt();
input.nextLine();
if (n2 == 1){
System.out.println("Enter course code to register: ");
String courseCode = input.nextLine();
if(n3.findCourseByCode(courseCode) != null ){
if (st.dropCourse(n3.findCourseByCode(courseCode))){
System.out.println("Course dropped successfully."+"\n");
return (true);
}
}else{
System.out.println("No course found with this code!"+"\n");
return(true);
}
}else if (n2==2){
System.out.println("Enter course name to register: ");
String courseName = input.nextLine();
if(n3.findCourseByName(courseName) != null ){
if (st.dropCourse(n3.findCourseByName(courseName))){
System.out.println("Course dropped successfully."+"\n");
return (true);
}
}else{
System.out.println("No course found with this name!"+"\n");
return(true);
}
}
return (true);
}else if (n1==4 ){
System.out.println("Registered courses for "+ st.getName()+":");
st.showRegisteredCourses();
return (true);
}else if (n1==5 ){
System.exit(0);
return (true);
}
return false;
} }
} }
@@ -9,16 +9,33 @@ 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(int i = 0 ; i <= courses.size()-1; i++){
// Return the matching course if found, otherwise return null if (code.equals(courses.get(i).getCourseCode())){
return (courses.get(i));
}
}
return null; return null;
} }
public Course findCourseByName(String name){
for(int i = 0 ; i < courses.size(); i++){
if (name.equals(courses.get(i).getCourseName())){
return (courses.get(i));
}
}
return null;
}
public void showAllCourses() { public void showAllCourses() {
// TODO: Print all courses in the system
for (int i = 0; i <= courses.size() - 1; i++) {
System.out.println(courses.get(i).getCourseName() + "(" + courses.get(i).getCourseCode() + ")" + " = Remaining capacity: " + courses.get(i).getCapacity() + "\n");
}
} }
} }
+33 -8
View File
@@ -21,19 +21,44 @@ 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.contains(c)){
// Return true if registration was successful, otherwise false System.out.println("You have already registered in this course!"+"\n");
return false; return (false);
} else if (registeredCourses.size()== 2){
System.out.println("You have reached the maximum number of allowed courses!"+"\n");
return (false);
}
else if (c.getCapacity()>0){
registeredCourses.add(c);
c.reduceCapacity();
return (true);
}else{
System.out.println("Registration was not successful!"+"\n"+"This course has no capacity left."+"\n");
return (false);
}
} }
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 false; return (true);
}else{
System.out.println("Course drop failed!"+"\n"+"You are not registered for this course."+"\n");
return (false);
}
} }
public void showRegisteredCourses() { public void showRegisteredCourses() {
// TODO: Print all registered courses if(registeredCourses.size()==0){
System.out.println(Main.st.getName() + "hasn't registered for any course."+"\n");
}
else {
for (int i = 0; i <= registeredCourses.size() - 1; i++) {
System.out.println(registeredCourses.get(i).getCourseName() + "(" + registeredCourses.get(i).getCourseCode() + ")" + "\n");
}
}
} }
} }