2

I have been creating a program that is to add search delete bookings etc... After hours I finally thought I was making progress but when I delete a booking my program finds the correct booking returns the correct information for that booking but deletes a different booking.

I have attached the files in a zip as if I displayed them they would take up lots of screen space. The program has been made in BlueJay.

Code for decleration and adding of objects into my array list

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

This is taken from my hostel class

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

this is taken from my GUI class

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

Any additonal code needed either ask or there is a full copy in the link above thanks

5
  • 5
    No-one here is going to download a zip-file and then read all of your source code. Please make a minimal test-case that illustrates the problem (see sscce.org). Commented Jan 7, 2012 at 17:34
  • 1
    Revised Relevent Code Now Displayed Commented Jan 7, 2012 at 17:45
  • @HxMGraeme : Please tell me the starting point of your program, mean to say what must i write in my main method to start this. That's it . Regards Commented Jan 7, 2012 at 18:12
  • i run the GUI class by right clikcing the class new GUI() then just put "" in the box and it should bring up the main GUI window thanks Commented Jan 7, 2012 at 18:34
  • @HxMGraeme : Thankyou, i found the problem, check the answer. Everything is perfect except for one thing i mentioned in the answer. Regards Commented Jan 7, 2012 at 20:00

3 Answers 3

2

Your loop only increments the index if the room ID of the current room is equal to the ID of the room to delete. The line

index++;

should be out of the if block.

EDIT:

The other problem is that you're trying to remove elements a collection while iterating on it. This is only possible if you use an Iterator to iterate over the collection, and use the iterator's remove method to remove the current element. Note that even if it was possible, since you remove the element at the given index, the index should not be incremented since you have just removed the element at this index.

Example of using an iterator:

for (Iterator<Booking> it = bookings.iterator(); it.hasNext(); ) {
    Booking b = it.next();
    if (...) {
        it.remove();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

When i take the index++; to the } below. i press confirm delete and the whole program gets flooded with errors hense i moved it up. im not sure what iv done wrong
See my edits. Trying random moves hoping that it will solve a problem is usually not a good strategy. Try to think about what your algorithm does.
Thanks well il look into that have a play see if i can use it for my program. Just running out of time
1

Basically when s.getRoomID().equals(roomID) is true your if block is executed so no matter what is the response of the user your index is incremented. So, do this:

if ( s.getRoomID().equals(roomID))
{
   //your code
}

index++

1 Comment

Iv taken the index++ outside of the if statement but it doesnt seem to have made a differnce now when i press ok to confirm the bookins.remove(index); the application shows a window with many red lines of error code any ideas? Thanks
0

I just looked into your code, and seems like you are trying to iterate over a collection and also modifying the values at the same time. With enhanced for loop, such things do give errors, so instead of using the enhanced for loop, you must use a normal for loop. So I had modified your deleteBookings Method for the respective change.

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

Moreover, after this

bookings.remove(i);

You forgot to return something like

return deleteMessage + " has been DELETED."; /*I did this.*/

Since you failed to return a String on successful completion, that's the reason why it returns "Cannot find room.", even after successful deletion. Rest of the code is perfect. Hope that might solve your query.

Regards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.