我一直在创建一个添加搜索删除预订等程序... 几个小时后,我最终认为我正在取得进展,但是当我删除预订时,我的程序发现正确的预订会返回该预订的正确信息,但会删除不同的预订。
我已将文件附加到zip中,就像我显示它们一样,它们占用了大量的屏幕空间。该计划已在BlueJay中制作: hostelgreamenew.zip 18.4 KB
将数据删除和添加到我的数组列表中的代码
public Hostel(String hostelName)
{
this.hostelName = "Newcastle Hostel";
bookings = new ArrayList<Booking>();
}
public String getHostelName()
{
return hostelName;
}
public String addBooking(String roomID, String roomType, String guest)
{
if (roomID.equals(""))
return "Error Please Entre Room ID";
else if (roomType.equals(""))
return "Error Please Entre Room Type";
else if (guest.equals(""))
return "Error Please Entre Guest Name";
bookings.add(new Booking(roomID,roomType,guest));
return "Room " + roomID + " " + roomType + " Has Been Booked For " + guest;
}
这取自我的宿舍课程
public String deleteBooking(String roomID)
{
int index = 0;
for ( Booking s : bookings )
{
if ( s.getRoomID().equals(roomID))
{
//return "Room ID: " + roomID + " Room Type: " + s.getRoomType() + " Guest: " + s.getGuest();
String deleteMessage = "Room ID: " + roomID + " Room Type: " + s.getRoomType() + " Guest: " + s.getGuest();
int response = JOptionPane.showConfirmDialog(null, deleteMessage, "Confirm Delete",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION)
{
} else if (response == JOptionPane.YES_OPTION)
{
bookings.remove(index);
}
index++;
}
}
return " Cannot find room";
}
这取自我的GUI类
else if (item.equals("Cancel Booking"))
{
newBookingButton.setEnabled(false);
cancelBookingButton.setEnabled(false);
String roomID = JOptionPane.showInputDialog(this, "Enter a room ID", "Delete a Booking", JOptionPane.QUESTION_MESSAGE);
output.setText(hostel.deleteBooking(roomID));
newBookingButton.setEnabled(true);
cancelBookingButton.setEnabled(true);
}
任何附加代码都需要询问或上面的链接中有完整的副本,谢谢
答案 0 :(得分:2)
如果当前房间的房间ID等于要删除的房间的ID,则您的循环仅递增索引。这条线
index++;
应该在if
区块之外。
编辑:
另一个问题是你在迭代时试图删除元素。只有在使用迭代器迭代集合并使用迭代器的remove方法删除当前元素时,才可以执行此操作。请注意,即使有可能,由于您删除了给定索引处的元素,因此您不应该递增索引,因为您刚刚删除了此索引处的元素。
使用迭代器的示例:
for (Iterator<Booking> it = bookings.iterator(); it.hasNext(); ) {
Booking b = it.next();
if (...) {
it.remove();
}
}
答案 1 :(得分:1)
基本上当s.getRoomID().equals(roomID)
为真时,您的if
块将被执行,因此无论用户的响应是什么,index
都会递增。所以,这样做:
if ( s.getRoomID().equals(roomID))
{
//your code
}
index++
答案 2 :(得分:0)
我只是查看了你的代码,看起来你正试图迭代一个集合并同时修改这些值。使用增强的for循环,这样的事情会产生错误,因此不必使用增强的for循环,而是必须使用普通for循环。所以我修改了你的deleteBookings方法以进行相应的更改。
public String deleteBooking(String roomID)
{
//for ( Booking s : bookings )
for (int i = 0; i < bookings.size(); i++)
{
Booking s = bookings.get(i);
if ( s.getRoomID().equals(roomID))
{
//return "Room ID: " + roomID + " Room Type: " + s.getRoomType() + " Guest: " + s.getGuest();
String deleteMessage = "Room ID: " + roomID + " Room Type: " + s.getRoomType() + " Guest: " + s.getGuest();
//int r = JOptionPane.showOptionDialog,null("Are you sure you would like to delete the following \n"
//+ "deleteMessage",
//"Delete a booking",
//JOptionPane.YES_NO_OPTION,
//JOptionPane.QUESTION_MESSAGE,null,null,null);
//if (r == JOptionPane.YES_OPTION) {
// bookings.remove(index);
//}
//if (r == JOptionPane.NO_OPTION){
// return "Booking Was Not Canceled";
// }
int response = JOptionPane.showConfirmDialog(null, deleteMessage, "Confirm Delete",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION)
{
} else if (response == JOptionPane.YES_OPTION)
{
//bookings.remove(index);
bookings.remove(i);
return deleteMessage + " has been DELETED."; /*I did this.*/
}
}
}
return " Cannot find room";
}
此外,在此之后
bookings.remove(i);
你忘了回复
之类的东西return deleteMessage + " has been DELETED."; /*I did this.*/
由于您在成功完成时未能返回String,因此即使成功删除后,它也会返回“无法找到空间”。 其余的代码是完美的。 希望这可以解决您的疑问。
此致