I'm creating a scheduling and registration system for a class project. I'm required to be able to add and delete rooms and courses. Each course can only last an hour and may not happen at the same time as another course in the same room. I'm able to delete the course itself, but I'm having trouble deleting the time associated with that course.
What I've done is create an ArrayList of Rooms with each room being able to hold an ArrayList of Courses. Each of these courses has a specific hour which is checked if it's in use using an ArrayList of times. I was able to add the courses hour to the list and halt the user from creating another course with the exact time slot in the same room. However, whenever I remove the course I'm trying to remove the time as well so that another course that's created may use that time slot. Problem is, the time slot gets filled and stays filled even after removing the course and I'm not sure why.
Some guidelines:
- Rooms can be added and deleted by the user. (To delete a room, no course should be scheduled in this room).
- All courses can be deleted by the user.
- The user can create courses by specifying a room number and the participants.
- A room cannot hold more than one course at any one-hour slot.
To be honest I've been working for around 7 hours straight and I'm not to confident in my code, if I'm even doing things right, or even what I'm talking about really. I apologize if I'm not being specific enough or making any sense, please let me know if something needs clarification. If you have any other tips/pointers or see any other mistakes please let me know. Thanks in advance.
Course.java
package Schedule;
import java.util.ArrayList;
public class Course extends Room {
private String name;
private int roomNum, hour, students;
private static ArrayList < Course > courseList = new ArrayList < > ();
private static ArrayList < Integer > times = new ArrayList < > (24);
public Course() {}
public Course(String name, int hour, int roomNum, int students) { //Constructor
this.name = name;
if (hour > 7 && hour < 18) {
this.hour = hour;
} else {
System.out.println("Not a valid time slot. Time set to 6:00PM/1800 HOURS. ");
this.hour = 18;
}
this.students = students;
this.roomNum = roomNum;
boolean inUse = checkTime(hour, roomNum);
if (inUse == false) {
times.add(hour);
Room.addCourse(roomNum, this);
courseList.add(this);
}
}
public static void deleteCourse(int courseNum, int roomNum) {
boolean pass;
pass = Room.removeCourse(courseNum, roomNum);
if (pass == true) {
times.remove(courseNum);
courseList.remove(courseNum);
System.out.println("Course Removed ");
}
}
public static boolean checkTime(int hour, int roomNum) {
boolean exist = false;
for (int i = 0; i < courseList.size(); i++) {
if (courseList.get(i).hour == hour && courseList.get(i).roomNum == roomNum) {
exist = true;
System.out.println("Time already in use, course could not be added. ");
}
}
return exist;
}
}
Room.java
package Schedule;
import java.util.ArrayList;
public class Room {
private int number, numOfClasses;
private static int numOfRooms = 1000;
private static ArrayList < Room > roomList = new ArrayList < > ();
private ArrayList < Course > courseList = new ArrayList < > ();
public Room() {}
public Room(int number, int numOfClasses) { //Constructor
this.number = number;
this.numOfClasses = numOfClasses;
if (roomList.size() < numOfRooms) {
roomList.add(this);
System.out.println("Room added");
} else {
System.out.println("Room couldn't be added, not enough rooms available.");
}
}
public static void numOfRooms(int r) {
numOfRooms = r;
}
public static void deleteRoom(int roomNum) { //Delete room
boolean exist = false;
for (int i = 0; i < roomList.size(); i++) {
if (roomList.get(i).getRoomNum() == roomNum) {
if (roomList.get(i).courseList.size() > 0) {
System.out.printf("%s%d%s%n", "Cannot delete room ", roomNum, " " + "There is currently a course in the room. ");
} else {
roomList.remove(i);
System.out.printf("%s%d%s%n", "Room ", roomNum, " Deleted");
}
exist = true;
}
}
if (exist == false) {
System.out.printf("%s%d%s%n", "Room ", roomNum, " does not exist, could not delete.");
}
}
public int getRoomNum() {
return number;
}
public static ArrayList < Room > getRoomList() {
return roomList;
}
public static void addCourse(int roomNum, Course c) { //Add Course to room.
boolean empty = true;
for (int i = 0; i < roomList.size(); i++) {
if (roomList.get(i).getRoomNum() == roomNum) {
roomList.get(i).courseList.add(c);
System.out.printf("%s%d%n", "Course added to room ", roomNum);
empty = false;
}
}
if (empty == true) {
System.out.println("No rooms with that room number. ");
}
}
public static boolean removeCourse(int courseNum, int roomNum) {
boolean exist = false;
try {
for (int i = 0; i < roomList.size(); i++) {
if (roomList.get(i).getRoomNum() == roomNum) {
roomList.get(i).courseList.remove(courseNum);
exist = true;
}
}
if (exist == false) {
System.out.println("Could not find course to delete. ");
}
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: Could not find a room or course to delete. ");
}
return exist;
}
}
ScheduleDemo.java
//For adding rooms, create a room object and input the room number and number of courses.
//For adding courses, create a course object and input the Name, hour1, room number, and # of students.
//For Deleting rooms, type Room.deleteRoom("room number").
//For Deleting Courses, type Course.deleteCourse("Course number", "Room Number").
package Schedule;
public class ScheduleDemo {
public static void main(String[] args) {
Room.numOfRooms(100);
Room room0 = new Room(0, 1);
Room room1 = new Room(3, 1);
Room room2 = new Room(99, 1);
Course course0 = new Course("Course", 9, 3, 10);
Course course1 = new Course("Course2", 9, 99, 12);
Course.deleteCourse(0, 99);
Course course2 = new Course("Help", 9, 99, 122);
Room.deleteRoom(56);
Room.deleteRoom(99);
Course.deleteCourse(1, 99);
}
}
Output:
Room added
Room added
Room added
Course added to room 3
Course added to room 99
Course Removed
Time already in use, course could not be added.
Room 56 does not exist, could not delete.
Room 99 Deleted
Could not find course to delete.
BUILD SUCCESSFUL (total time: 0 seconds)
UPDATE:
I managed to fix the issues by removing course and room number completely and instead passed the name of the course. Since I was passing in the index (courseNum) of each course, I ended up deleting the wrong course which is why my times didn't delete properly. By searching the name of the course in both my Course list and my room course list, I was able to accurately delete the right course from both list. Here's what I fixed.
Main
Course course1 = new Course("Course2", 9, 99, 12); //Creates Course2 and time slot
Course.deleteCourse("Course2"); //Deletes Course2 and time slot
Course course2 = new Course("Help", 9, 99, 122); //Adds course Help into same hour
/*
New Output
Course added to room 99
Course Removed
Course added to room 99
*/
Course
public static void deleteCourse(String name) {
boolean pass;
pass = Room.removeCourse(name);
if (pass == true) {
for (int i = 0; i < courseList.size(); i++) {
if (courseList.get(i).getName().equals(name)) {
times.clear();
courseList.remove(i);
System.out.println("Course Removed ");
}
}
}
}
public String getName() {
return name;
}
Room
public static boolean removeCourse(String name) {
boolean exist = false;
try {
for (int j = 0; j < roomList.size(); j++) {
for (int i = 0; i < roomList.get(j).courseList.size(); i++) {
if (roomList.get(j).courseList.get(i).getName().equals(name)) {
roomList.get(j).courseList.remove(i);
exist = true;
}
}
}
if (exist == false) {
System.out.println("Could not find course to delete. ");
}
} catch (IndexOutOfBoundsException e) {
System.out.println("Could not find a room or course to delete. ");
}
return exist;
}
Now I can move forward to other things. Thanks!