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.