1

I am new to Java and I try to write a simple room booking program.

Here is my plan:

There will be a class called Booking. It will ask the user to enter room ID to book a room and the user can press x to terminate the program.

I would like to have another class called RoomDB. I will use arraylist to store the room ID.

I guess I also need constructor, so I have another class called Room.

My idea is when the program run, it will display the available room ID for user to choose. If the user enters the incorrect room ID, the program will show the error message and ask the user to enter again.

If the user enters a correct room ID and the room ID is available, it will display a success message to the user.

If the user enters a correct room ID and the room ID is already booked, it will ask the user to enter again.

That's all for the idea and I begin to write the code.

Here is my code for the three classes

Booking.java

package test;

import java.util.Scanner;

public class Booking 
{
static RoomDB roomDB = new RoomDB();

public static void main(String[] args) {
    String roomID;      
    Room room;  
    Scanner inputID = new Scanner(System.in);

        while(true){

            System.out.println("Please choose the room you want to book \n"+getRoomList(roomDB));

            while(true){
                roomID = inputID.nextLine();
                if(roomID.equals ("x")){
                    break;
                }

                if(getRoom(roomID) == null){

                    System.out.println("The room ID is incorrect, please enter again or enter x to quit");

                }
                else{

                    room = getRoom(roomID);

                    if(!room.isBooked()){
                        System.out.println("Book successfully");
                        break;
                    }
                    else{
                        System.out.println("please enter the room ID again or enter x to quit");
                    }
                }

            }

        }

    }

public static String getRoomList(RoomDB roomDB)
{

    String roomList = "";
    for(Room r:roomDB.getRoom())
    {

        if(!r.isBooked())
        {
            roomList = roomList+r.getRoomID()+" is free"+"   ";
        }
        else
        {
            roomList = roomList+r.getRoomID()+" has been booked ";
        }

    }
    return roomList;
}


public static Room getRoom(String roomID){

    for(Room r:roomDB.getRoom()){

        if(r.getRoomID()==roomID){

            return r;

        }

    }
    return null;

}
}

RoomDB.java

package test;

import java.util.ArrayList;

public class RoomDB 
{
private ArrayList<Room> room;
private String[] roomID = {"Room1","Room2"};

RoomDB(){

    room = new ArrayList<Room>();                     
    for(int i=0;i<roomID.length;i++)
    {
        addRoom(new Room(roomID[i]));
    }

}

public void addRoom(Room addRoom){
    room.add(addRoom);
}

public ArrayList<Room> getRoom(){
    return room;
}
}

Room.java

package test;

public class Room 
{
private String roomID  = null;
private boolean booked = false;

Room(String roomID)
{
    this.roomID = roomID;
}

String getRoomID()
{
    return roomID;
}

void cancel()
{
    booked = false;
}


boolean isBooked()
{
    return booked;
}

}

I checked there is no syntax error, but the program cannot run appropriately.

The problem is the program keep asking the user to enter the room ID even the room ID is correct. For example, the user enter Room1, the program still keep asking for the input.

I wonder to know which class cause the problem.

I should be grateful if someone can give advice on this issue please.

demonplus
  • 5,613
  • 12
  • 49
  • 68
beginner
  • 192
  • 1
  • 1
  • 13
  • You are comparing Strings with `==` at `if(r.getRoomID()==roomID)`. If you change it to `if (r.getRoomID().equals(roomID))` then it does work. – SomeJavaGuy Oct 09 '15 at 09:59
  • Hello @Kevin Esche, thank you for your reply, the code works now after use your suggestion. Thank you very much. – beginner Oct 09 '15 at 10:05

4 Answers4

1

This is Perfect Answer, See this just copy it and run below code, please remove modify to your package/class name accordingly as you wish,

Forget to set true after room Allocated, if Room seems to be free then allocate and marked it as a BOOKED->true.

package roombooking;

import java.util.ArrayList;
import java.util.Scanner;

class Room 
{
    private String roomID  = null;
    private boolean booked = false;

    Room(String roomID)
    {
        this.roomID = roomID;
    }

    String getRoomID()
    {
        return roomID;
    }

    void cancel()
    {
        booked = false;
    }


    boolean isBooked()
    {
        return booked;
    }

    public void setBooked(boolean booked) {
        this.booked = booked;
    }


}

class RoomDB 
{
    private ArrayList<Room> room;
    private String[] roomID = {"Room1","Room2"};

    RoomDB(){

        room = new ArrayList<Room>();                     
        for(int i=0;i<roomID.length;i++)
        {
            addRoom(new Room(roomID[i]));
        }

    }

    public void addRoom(Room addRoom){
        room.add(addRoom);
    }

    public ArrayList<Room> getRoom(){
        return room;
    }
}

public class Booking {

    static RoomDB roomDB = new RoomDB();

    public static void main(String[] args) {
        String roomID;      
        Room room;  
        Scanner inputID = new Scanner(System.in);

        while(true){

            System.out.println("Please choose the room you want to book \n"+getRoomList(roomDB));

            while(true){
                System.out.println("Enter your Room no. : (Enter x for quite ) : ");
                roomID = inputID.nextLine();
                System.out.println("X : " + roomID.equals("x"));
                if(roomID.equals("x")){
                    System.out.println("Break");
                    break;
                }

                if(getRoom(roomID) == null){

                    System.out.println("The room ID is incorrect, please enter again or enter x to quit");

                }
                else{

                    room = getRoom(roomID);

                    if(!room.isBooked()){
                        System.out.println("Book successfully");
                        room.setBooked(true);
                        break;
                    }
                    else{
                        System.out.println("please enter the room ID again or enter x to quit");
                    }

                }

            }

        }

    }

    public static String getRoomList(RoomDB roomDB)
    {

        String roomList = "";
        for(Room r:roomDB.getRoom())
        {

            if(!r.isBooked())
            {
                roomList = roomList+r.getRoomID()+" is free"+"   ";
            }
            else
            {
                roomList = roomList+r.getRoomID()+" has been booked ";
            }

        }
        return roomList;
    }


    public static Room getRoom(String roomID){

        for(Room r:roomDB.getRoom()){

            if(r.getRoomID().equals(roomID)){
                System.out.println("r.getRoomID : " + r.getRoomID() + "  AND user.roomID :" + roomID);
                return r;
            }

        }
        return null;

    }

}

EDITED :

Here,

if(!room.isBooked()){
System.out.println("Book successfully");
room.setBooked(true); // this line does, booked room, after booked once it won't assign to any one else.
break;

}

Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55
  • Hello @Mr. Vishal J Gajera , thank you for your reply. – beginner Oct 12 '15 at 00:38
  • I run your code and it works. Thank you very much. May I ask a question about your code? When I run it, I input the room number , e.g. Room1 and press enter. I see there is a for loop but I don't understand why the program will display "r.getRoomID : Room1 AND user.roomID :Room1" twice? Thank you – beginner Oct 12 '15 at 00:59
  • Hey, @beginner, the program display "r.getRoomID : Room1 AND user.roomID :Room1" twice, First, i would like to say - It was i have added because of debugging purpose which was not into your code, Second, it's comes twice because of it's called from 2-place, 1) if(getRoom(roomID) == null){... 2) from else block, room = getRoom(roomID); I hope now you clear with this answer. – Vishal Gajera Oct 12 '15 at 07:47
  • @vishalgajera _This is Perfect Answer, See this just copy it and run below code_ why is stuff in your answer?? Please explain the logic /issue /what the OP is doing wrong .. just copy and paste answers cant help furture visitors of SO.. – Viraj Nalawade Dec 10 '15 at 14:43
  • @VirajNalawade i appreciate by you thought. let me correct it. – Vishal Gajera Dec 10 '15 at 15:54
  • @VirajNalawade kindly refer EDITED part which might be fruitful for others. – Vishal Gajera Dec 10 '15 at 15:58
0
public static Room getRoom(String roomID){

for(Room r:roomDB.getRoom()){

    if(r.getRoomID().equals(roomID)){ //Old : r.getRoomID()==roomID

        return r;

    }

}
return null;

}

For comparison string always use equals.

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
0

By the looks of it your issue is arising at the moment you are comparing Strings with the "==" operator. This is not possible, as the "==" operator tries to identify if the two objects are equal, not their respective values. You can get a better and full explanation of how String comparison works here: How do I compare strings in Java?

As a quick and proper fix of your issue just use:

if(r.getRoomID().equals(roomID))
{ 
    return r;
}
Community
  • 1
  • 1
Phantomazi
  • 408
  • 6
  • 22
  • Hello @ Phantomazi, thank you for your reply, the code works now after use your suggestion. Thank you very much. – beginner Oct 09 '15 at 10:10
  • You're very welcome, @beginner ! If you are happy with this, or any other answer please do up-vote and accept the one you think suits best for you, so that the StackOverflow community knows there is already an answer given here. Please note 15 mins have to pass before you can accept an answer. – Phantomazi Oct 09 '15 at 10:11
0

I would also suggest you compare strings in the same case (in 2 places in Booking.java):

if (roomID.equalsIgnoreCase("x"))

and also

if (r.getRoomID().equalsIgnoreCase(roomID))

hope that helps.

Husman
  • 6,819
  • 9
  • 29
  • 47
  • Hello @Husman, thank you for your reply. I tried your suggestion and it works. So no matter the user enters upper case or lower case, the program will accept the input. Thank you very much. – beginner Oct 09 '15 at 10:15