所以我尝试用checkindoubleroom方法建立一个酒店:这个方法在availabledoubleroomsvector中查找一个可用的双人房,将其状态更改为“已占用”,并将其放入“已占用房间”树中。但是,将其插入此树会产生错误(错误总是以粗体显示)。
public int checkInDoubleRoom(int clientId) {
//x will store the roomid so it can be assigned to a client
Comparable room_id = 0;
//Look for a DoubleRoom in availableRooms
//Search in availableRooms should be << O(n) if we assume that Family Rooms and Double Rooms get added randomly when they are 'READY'.
for (int i = 0; i < availableRooms.size(); i++) {
if (availableRooms.get(i) instanceof DoubleRoom) {
Room room_get = (Room)(availableRooms.get(i));
//Room_get is a double room
room_get.setStatus("OCCUPIED");
room_id = room_get.getId();
//Then put this room in the tree of non- Ready rooms
**rooms.insert(room_get);**
//find the client in the client tree with the right id: assign the room to the client
Object a = clients.get(clientId);
//TODO wtf this is wrong af
// for (int j = 0; j < clients.depth(); j++) {
// Object a = clients.get(j);
((Client)(a)).setRoom((int)room_id);
}
我在树中插入内容的代码如下所示:(这里我们在树中插入doubleroom(扩展room类的类)
public void insert(Comparable element)
{
insertAtNode(element,root,null);
}
// we traverse the tree.
// Current holds the pointer to the TreeNode we are currently checking
// Parent holds the pointer to the parent of the current TreeNode
private void insertAtNode(Comparable element,TreeNode current,TreeNode parent)
{
// if the node we check is empty: empty tree
if(current == null)
{
TreeNode newNode = new TreeNode(element);
// the current node is empty, but we have a parent
if(parent != null)
{
// do we add it to the left?
if(element.compareTo(parent.getValue()) < 0)
{
parent.leftNode = newNode;
}
// or do we add it to the right?
else
{
parent.rightNode = newNode;
}
}
// the current node is empty and it has no parent, we actually have an empty tree
else root = newNode;
}
**else if(current.compareTo(element) == 0) {**
System.out.println("The element is already in the tree");
// if the element is already in the tree, what to do?
}
// if the element is smaller than current, go left
else if(current.compareTo(element) < 0)
{
insertAtNode(element,current.getLeftTree(),current);
}
// if the element is bigger than current, go right
else insertAtNode(element,current.getRightTree(),current);
}
然后,compareto方法将我们带到树类compareto方法:
public int compareTo(Object o) {
if (o instanceof Integer) {
return value.compareTo(o);
}
else if (o instanceof TreeNode) {
TreeNode node = ( TreeNode )o;
return value.compareTo(node.value);
}
else if (o instanceof Object){
**return value.compareTo(o);**
}
else {
throw new IllegalArgumentException();
}}
我试图将treenode的值(应该是room类的示例)与room对象进行比较,但是它抛出了错误
"lass mainPackage.DoubleRoom cannot be cast to class java.lang.Integer (mainPackage.DoubleRoom is in unnamed module of loader 'app'; java.lang.Integer is in module java.base of loader 'bootstrap')
at java.base/java.lang.Integer.compareTo(Integer.java:64)"
但我不明白为什么用整数。比较??
编辑:完成堆栈跟踪:
Exception in thread "main" java.lang.ClassCastException: class mainPackage.DoubleRoom cannot be cast to class java.lang.Integer (mainPackage.DoubleRoom is in unnamed module of loader 'app'; java.lang.Integer is in module java.base of loader 'bootstrap')
at java.base/java.lang.Integer.compareTo(Integer.java:64)
at importedClass.Tree$TreeNode.compareTo(Tree.java:73)
at importedClass.Tree.insertAtNode(Tree.java:202)
at importedClass.Tree.insert(Tree.java:172)
at mainPackage.HotelManagement.checkInDoubleRoom(HotelManagement.java:94)
at mainPackage.Main.main(Main.java:37)
提前谢谢!
暂无答案!
目前还没有任何答案,快来回答吧!