在删除课程时无法删除时段

时间:2016-03-26 07:12:25

标签: java arraylist static

我正在为课程项目创建一个计划和注册系统。我需要能够添加和删除房间和课程。每个课程只能持续一个小时,可能不会与同一个房间的另一个课程同时发生。我可以删除课程本身,但是我无法删除与该课程相关的时间。

我所做的是创建一个房间的ArrayList,每个房间都可以容纳一个ArrayList of Courses。这些课程中的每一个都有一个特定的小时,如果使用ArrayList of times,它将被检查。我能够将课程时间添加到列表中,并阻止用户在同一个房间中创建具有确切时间段的另一个课程。但是,每当我删除课程时,我都会尝试删除时间,以便创建的其他课程可以使用该时间段。问题是,即使在删除课程后,时间段也会被填满并保持填充状态,我不确定原因。

一些指导原则:

  1. 用户可以添加和删除房间。 (要删除房间,不得在此房间安排课程。)
  2. 用户可以删除所有课程。
  3. 用户可以通过指定房间号和参与者来创建课程。
  4. 在任何一小时的时段内,一个房间不能容纳多个课程。
  5. 说实话,我已经连续工作了大约7个小时,我对自己的代码没有信心,如果我甚至做得对,甚至是我自己做的事情。谈论真的。如果我不够具体或没有任何意义,我会道歉,如果需要澄清,请告诉我。如果您有任何其他提示/指示或看到任何其他错误,请告诉我。提前谢谢。

    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);
      }
    }
    

    输出:

    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)
    

    更新:

    我设法通过完全删除课程和房间号来解决问题,而是通过了课程的名称。由于我在每个课程的索引(courseNum)中传递,我最后删除了错误的课程,这就是为什么我的时间没有正确删除。通过在我的课程列表和我的房间课程列表中搜索课程名称,我能够从两个列表中准确地删除正确的课程。这是我修复的内容。

    &#13;
    &#13;
    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
    */
    &#13;
    &#13;
    &#13;

    &#13;
    &#13;
    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 ");
          }
        }
      }
    }
    &#13;
    &#13;
    &#13;

    &#13;
    &#13;
     public String getName() {
       return name;
     }
    &#13;
    &#13;
    &#13;

    &#13;
    &#13;
    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;
    }
    &#13;
    &#13;
    &#13;

    现在我可以继续前进。谢谢!

2 个答案:

答案 0 :(得分:0)

我认为主要问题是您正在使用索引删除CourseList。而且我认为您假设RoomNum 99与课程Num 0相关联。事实上,您的应用程序中没有课程编号的概念。默认情况下,它将成为List的索引。

 private static ArrayList < Course > courseList = new ArrayList < > ();
 courseList.add(object of type Course);

 courseList.remove(courseNum); // Note course Num becomes an index here

这样就删除了错误的条目。位于房间号码99的Course2仍然在列表中,因此说该课程仍在运行。

编辑:如果你必须记住课程编号和课程名称之间的关系,这个设计很混乱,不会起作用。您尚未在设计中的任何位置建模关系。

答案 1 :(得分:0)

当您声明某些内容static时,会使该成员属于该类,而不是属于该实例。因此,当您运行该程序时,请查看您的ArrayLists内容。

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);
// courseList : {course0} | hours : {9}
Course course1 = new Course("Course2", 9, 99, 12);
// courseList : {course0,course1} | hours : {9,9}
Course.deleteCourse(0, 99);
// courseList : {course1} | hours : {9}

由于您的Arraylist是静态的,因此deleteCourse方法不会删除会议室99的课程0.它会从您的会议课程列表中删除课程1,但不会从static数组列表中删除!