gpt4 book ai didi

java - 删除类(class)时无法删除时段

转载 作者:行者123 更新时间:2023-12-02 09:15:11 25 4
gpt4 key购买 nike

我正在为一个类(class)项目创建一个日程安排和注册系统。我需要能够添加和删除房间和类(class)。每门类(class)只能持续一个小时,并且不得与同一房间内的另一门类(class)同时进行。我可以删除类(class)本身,但在删除与该类(class)关联的时间时遇到问题。

我所做的是创建一个房间数组列表,每个房间都能够保存一个类(class)数组列表。每门类(class)都有一个特定的时间,使用时间数组列表检查是否正在使用。我能够将类(class)时间添加到列表中,并阻止用户在同一房间内创建具有确切时间段的另一门类(class)。但是,每当我删除类(class)时,我也会尝试删除时间,以便创建的另一门类(class)可以使用该时间段。问题是,即使删除类(class)后,时间段也会被填满,并且仍然被填满,我不知道为什么。

一些准则:

  1. 用户可以添加和删除房间。 (要删除房间,该房间中不应安排任何类(class))。
  2. 用户可以删除所有类(class)。
  3. 用户可以通过指定房间号和参与者来创建类(class)。
  4. 一个房间在任何一小时时段内不能容纳多于一门类(class)。

说实话,我已经连续工作了大约 7 个小时,而且我对自己的代码不太有信心,即使我做的事情是正确的,甚至是我真正谈论的内容。如果我不够具体或没有任何意义,我深表歉意,如果有什么需要澄清,请告诉我。如果您有任何其他提示/指示或发现任何其他错误,请告诉我。提前致谢。

类(class).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;
}
}

房间.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)

更新:

我成功地通过完全删除类(class)和房间号并传递类(class)名称来解决问题。由于我传递了每门类(class)的索引(courseNum),因此我最终删除了错误的类(class),这就是为什么我的时间没有正确删除的原因。通过在我的类(class)列表和我的房间类(class)列表中搜索类(class)名称,我能够准确地从两个列表中删除正确的类(class)。这是我修复的内容。

主要

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
*/

类(class)

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

房间

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

现在我可以继续做其他事情了。谢谢!

最佳答案

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

 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 仍然保留在列表中,这表明该类(class)仍在运行。

编辑:这种设计非常困惑,如果您必须记住类(class)编号和类(class)名称之间的关系,则该设计将不起作用。您尚未在设计中的任何地方对这种关系进行建模。

关于java - 删除类(class)时无法删除时段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36232673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com