forked from ArefeRoosta/WS-02-intro-to-oop
Compare commits
3
Commits
c0c283e0c5
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bb9cc82763 | ||
|
|
8332230000 | ||
|
|
3cb0489679 |
@@ -1,27 +1,19 @@
|
||||
package CourseRegistrationStarter;
|
||||
|
||||
public class Course {
|
||||
private String courseName;
|
||||
private String courseCode;
|
||||
private String name;
|
||||
private String code;
|
||||
private int capacity;
|
||||
|
||||
public Course(String courseName, String courseCode, int capacity) {
|
||||
this.courseName = courseName;
|
||||
this.courseCode = courseCode;
|
||||
public Course(String name, String code, int capacity) {
|
||||
this.name = name;
|
||||
this.code = code;
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public String getCourseName() {
|
||||
return courseName;
|
||||
}
|
||||
|
||||
public String getCourseCode() {
|
||||
return courseCode;
|
||||
}
|
||||
|
||||
public int getCapacity() {
|
||||
return capacity;
|
||||
}
|
||||
public String getName() { return name; }
|
||||
public String getCode() { return code; }
|
||||
public int getCapacity() { return capacity; }
|
||||
|
||||
public boolean reduceCapacity() {
|
||||
if (capacity > 0) {
|
||||
|
||||
@@ -4,72 +4,58 @@ import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Course course1 = new Course("َAP", "1414", 2);
|
||||
Course course2 = new Course("DS", "2201", 1);
|
||||
Course course3 = new Course("DB", "3301", 3);
|
||||
Student student1 = new Student("Ali", "402222001");
|
||||
RegistrationSystem registrationSystem = new RegistrationSystem();
|
||||
registrationSystem.addCourseToSystem(course1);
|
||||
registrationSystem.addCourseToSystem(course2);
|
||||
registrationSystem.addCourseToSystem(course3);
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
RegistrationSystem system = new RegistrationSystem();
|
||||
|
||||
boolean isChosen = false;
|
||||
while (!isChosen)
|
||||
{
|
||||
System.out.println("\n--- Menu ---");
|
||||
System.out.println("1) Add course");
|
||||
System.out.println("2) Drop course");
|
||||
System.out.println("3) Show registered courses");
|
||||
System.out.println("4) Show all courses");
|
||||
System.out.println("5) Exit");
|
||||
System.out.print("Enter your choice: ");
|
||||
Scanner in = new Scanner(System.in);
|
||||
int chosen = in.nextInt();
|
||||
if (chosen == 1)
|
||||
{
|
||||
System.out.print("Enter Course Code to add: ");
|
||||
String codeToAdd = in.next();
|
||||
Course courseToAdd = registrationSystem.findCourseByCode(codeToAdd);
|
||||
|
||||
if (courseToAdd != null)
|
||||
{
|
||||
student1.addCourse(courseToAdd);
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("Error: Course not found in system.");
|
||||
}
|
||||
}
|
||||
else if (chosen == 2)
|
||||
{
|
||||
System.out.println("\n--- Drop Course ---");
|
||||
System.out.print("Enter Course Code to drop: ");
|
||||
String codeToDrop = in.next();
|
||||
Course courseToDrop = registrationSystem.findCourseByCode(codeToDrop);
|
||||
system.addCourseToSystem(new Course("Advanced Programming", "CS101", 30));
|
||||
system.addCourseToSystem(new Course("Data Structures", "CS102", 25));
|
||||
system.addCourseToSystem(new Course("Database Systems", "CS103", 20));
|
||||
|
||||
if (courseToDrop != null)
|
||||
{
|
||||
student1.dropCourse(courseToDrop);
|
||||
|
||||
System.out.print("Enter Student Name: ");
|
||||
String sName = scanner.nextLine();
|
||||
System.out.print("Enter Student ID: ");
|
||||
String sId = scanner.nextLine();
|
||||
Student student = new Student(sName, sId);
|
||||
|
||||
boolean running = true;
|
||||
while (running) {
|
||||
System.out.println("\n1. View All Courses\n2. Add Course\n3. Drop Course\n4. My Courses\n5. Exit");
|
||||
System.out.print("Choice: ");
|
||||
String choice = scanner.nextLine();
|
||||
|
||||
switch (choice) {
|
||||
case "1":
|
||||
system.showAllCourses();
|
||||
break;
|
||||
case "2":
|
||||
System.out.print("Enter course code to add: ");
|
||||
String addCode = scanner.nextLine();
|
||||
Course toAdd = system.findCourseByCode(addCode);
|
||||
if (toAdd != null) {
|
||||
if (student.addCourse(toAdd)) System.out.println("Successfully added.");
|
||||
} else {
|
||||
System.out.println("Course not found!");
|
||||
}
|
||||
}
|
||||
else if (chosen == 3)
|
||||
{
|
||||
System.out.println("\n--- Registered Courses ---");
|
||||
student1.showRegisteredCourses();
|
||||
}
|
||||
else if (chosen == 4)
|
||||
{
|
||||
System.out.println("\n--- All Available Courses ---");
|
||||
registrationSystem.showAllCourses();
|
||||
}
|
||||
else if (chosen == 5)
|
||||
{
|
||||
isChosen = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("Invalid! Please enter a number between 1 and 5.");
|
||||
break;
|
||||
case "3":
|
||||
System.out.print("Enter course code to drop: ");
|
||||
String dropCode = scanner.nextLine();
|
||||
if (student.dropCourse(dropCode)) System.out.println("Successfully dropped.");
|
||||
else System.out.println("You are not registered in this course.");
|
||||
break;
|
||||
case "4":
|
||||
student.showRegisteredCourses();
|
||||
break;
|
||||
case "5":
|
||||
running = false;
|
||||
System.out.println("Exiting...");
|
||||
break;
|
||||
default:
|
||||
System.out.println("Invalid option.");
|
||||
}
|
||||
}
|
||||
scanner.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,43 +1,29 @@
|
||||
package CourseRegistrationStarter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class RegistrationSystem {
|
||||
private ArrayList<Course> courses;
|
||||
private ArrayList<Course> allCourses;
|
||||
|
||||
public RegistrationSystem() {
|
||||
courses = new ArrayList<>();
|
||||
this.allCourses = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addCourseToSystem(Course c) {
|
||||
courses.add(c);
|
||||
public void addCourseToSystem(Course course) {
|
||||
allCourses.add(course);
|
||||
}
|
||||
|
||||
public Course findCourseByCode(String code) {
|
||||
for (Course a : courses)
|
||||
{
|
||||
if (a.getCourseCode().equals(code))
|
||||
{
|
||||
return a;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public Course findCourseByName(String name)
|
||||
{
|
||||
for (Course a : courses)
|
||||
{
|
||||
if (a.getCourseName().equals(name))
|
||||
{
|
||||
return a;
|
||||
}
|
||||
for (Course c : allCourses) {
|
||||
if (c.getCode().equalsIgnoreCase(code)) return c;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void showAllCourses() {
|
||||
for (Course c : courses)
|
||||
{
|
||||
System.out.println(c.getCourseName() + " - " + c.getCourseCode() + " - capacity: " + c.getCapacity());
|
||||
System.out.println("\n--- Available Courses in System ---");
|
||||
for (Course c : allCourses) {
|
||||
System.out.println("Name: " + c.getName() + " | Code: " + c.getCode() + " | Capacity: " + c.getCapacity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,79 +1,61 @@
|
||||
package CourseRegistrationStarter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Student {
|
||||
private String name;
|
||||
private String studentId;
|
||||
private String id;
|
||||
private ArrayList<Course> registeredCourses;
|
||||
private final int MAX_COURSES = 5;
|
||||
|
||||
public Student(String name, String studentId) {
|
||||
public Student(String name, String id) {
|
||||
this.name = name;
|
||||
this.studentId = studentId;
|
||||
this.id = id;
|
||||
this.registeredCourses = new ArrayList<>();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public boolean addCourse(Course course) {
|
||||
|
||||
public String getStudentId() {
|
||||
return studentId;
|
||||
}
|
||||
|
||||
public boolean addCourse(Course c) {
|
||||
if (registeredCourses.size() == 5)
|
||||
{
|
||||
System.out.println("Maximum course limit (5) reached.");
|
||||
if (registeredCourses.size() >= MAX_COURSES) {
|
||||
System.out.println("Error: Maximum course limit reached (5).");
|
||||
return false;
|
||||
}
|
||||
for (Course a : registeredCourses)
|
||||
{
|
||||
if (a.getCourseCode().equals(c.getCourseCode()))
|
||||
{
|
||||
System.out.println("Error: you are already registered for this course!");
|
||||
|
||||
for (Course c : registeredCourses) {
|
||||
if (c.getCode().equals(course.getCode())) {
|
||||
System.out.println("Error: Already registered in " + course.getName());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (c.getCapacity() > 0)
|
||||
{
|
||||
registeredCourses.add(c);
|
||||
c.reduceCapacity();
|
||||
System.out.println("Registration was successful.");
|
||||
|
||||
if (course.reduceCapacity()) {
|
||||
registeredCourses.add(course);
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
System.out.println("Error: No capacity left in " + course.getName());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean dropCourse(Course c) {
|
||||
Course courseToRempove = null;
|
||||
for (Course a : registeredCourses)
|
||||
{
|
||||
if (a.getCourseCode().equals(c.getCourseCode()))
|
||||
{
|
||||
courseToRempove = a;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (courseToRempove == null)
|
||||
{
|
||||
System.out.println("Error: you are not registered for this course!");
|
||||
return false;
|
||||
}
|
||||
registeredCourses.remove(courseToRempove);
|
||||
System.out.println("Successfully dropped course.");
|
||||
public boolean dropCourse(String courseCode) {
|
||||
for (Course c : registeredCourses) {
|
||||
if (c.getCode().equals(courseCode)) {
|
||||
c.increaseCapacity();
|
||||
registeredCourses.remove(c);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void showRegisteredCourses() {
|
||||
if (registeredCourses.isEmpty())
|
||||
{
|
||||
System.out.println("No courses registered.");
|
||||
return;
|
||||
System.out.println("\n--- Registered Courses for " + name + " (" + id + ") ---");
|
||||
if (registeredCourses.isEmpty()) {
|
||||
System.out.println("No courses registered yet.");
|
||||
} else {
|
||||
for (Course c : registeredCourses) {
|
||||
System.out.println("- " + c.getName() + " [" + c.getCode() + "]");
|
||||
}
|
||||
for(Course a : registeredCourses)
|
||||
{
|
||||
System.out.println(a.getCourseName() + " - " + a.getCourseCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user