Reputation: 190
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.
Upvotes: 1
Views: 57504
Reputation: 4207
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;
}
Upvotes: 1
Reputation: 6909
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.
Upvotes: 0
Reputation: 408
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;
}
Upvotes: 0
Reputation: 4418
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.
Upvotes: 0