Reputation: 313
I have to design and implement a reservation system for a hotel. I have
I want to 'reserve' the room at a given date.
To book a room without a date would be easy but its the date part that's complicating it. I'm struggling with the design of this and confident with a nudge in the right direction I could code it.
How do you say that a room is booked at this date but not at that date?
There is no database or anything its just an abstracted reservation system.
(I've been staring at this for a while, forgive me if if the solution is easy)
Thanks.
Upvotes: 9
Views: 17238
Reputation: 1
import java.util.*;
public class Final_Project {
public static void main(String[] args) {
Scanner ScanObj = new Scanner(System.in);
String choice="";
int a=1;
int x;
int stay=0;
int totalBill;
String name="";
String num="";
String checkIn="";
String checkOut="";
System.out.println("Kindly Fillup");
Date d = new Date();
System.out.print("Name:");
name = ScanObj.nextLine();
System.out.print("Phone Number:");
num =ScanObj.nextLine();
System.out.print("Check-in: ");
checkIn = ScanObj.nextLine();
System.out.print("Check-Out: ");
checkOut = ScanObj.nextLine();
System.out.println("Hello," + name + ". Welcome to Kimberly Hotel");
do{
try{
for(x=1; x==1;) {
System.out.println("[1] Room Reservations");
for(x=1; x==1;) {
System.out.print("ENTER : ");
choice=ScanObj.nextLine();
if (choice.equals("1")) {
System.out.println(" Rooms");
System.out.println("*********************************");
System.out.println("1.)Room 101 | Php 10,000 |5 Persons");
System.out.println("2.)Room 102 | Php 8,000 |4 Persons");
System.out.println("3.)Room 103 | Php 6,500 |3 Persons");
System.out.println("4.)Room 104 | Php 5,000 |2 Persons");
System.out.println("5.)Room 105 | Php 5,000 |2 Persons");
x=0;
}
else
throw new InputMismatchException("");
a=2;
}
}
}catch(Exception e) {
System.out.println("Invalid Input. Please try again.");
}
}while(a==1);
int choices;
int control=1;
do{
try{
System.out.print("Please select a room: ");
choices = ScanObj.nextInt();
System.out.print("How long will you stay: ");
stay = ScanObj.nextInt();
switch(choices)
{
case 1:
System.out.println("Here is your reservation as of: " + d.toString());
System.out.println(name+" |"+num+"|1 Room 101 | Php 10,000 | 5 Persons|");
System.out.println("Check-in Date: " + checkIn);
System.out.println("Check-Out Date: " + checkOut);
totalBill = stay * 10000;
System.out.println("Your total bill is: " + totalBill);
System.out.println("Additional 2 beddings");
System.out.println("Free Breakfast");
System.out.println("No pets allowed!");
System.out.println("Thank You!");
break;
case 2:
System.out.println("Here is your reservation as of: " + d.toString());
System.out.println("Here is your reservation: ");
System.out.println(name+" |"+num+"| Room 102 | Php 8,000 | 4 Persons|");
System.out.println("Check-in Date: " + checkIn);
System.out.println("Check-Out Date: " + checkOut);
totalBill = stay * 8000;
System.out.println("Your total bill is: " + totalBill);
System.out.println("Additional 2 beddings");
System.out.println("Free Breakfast");
System.out.println("No pets allowed!");
System.out.println("Thank You!");
break;
case 3:
System.out.println("Here is your reservation as of: " + d.toString());
System.out.println("Here is your reservation: ");
System.out.println(name+" |"+num+"| Room 103 | Php 6,500 | 3 Persons|");
System.out.println("Check-in Date: " + checkIn);
System.out.println("Check-Out Date: " + checkOut);
totalBill = stay * 8000;
System.out.println("Your total bill is: " + totalBill);
System.out.println("Additional 2 beddings");
System.out.println("Free Breakfast");
System.out.println("No pets allowed!");
System.out.println("Thank You!");
break;
case 4:
System.out.println("Here is your reservation as of: " + d.toString());
System.out.println("Here is your reservation: ");
System.out.println(name+" |"+num+"| Room 104 | Php 5,000 | 2 Persons|");
System.out.println("Check-in Date: " + checkIn);
System.out.println("Check-Out Date: " + checkOut);
totalBill = stay * 8000;
System.out.println("Your total bill is: " + totalBill);
System.out.println("Additional 1 beddings");
System.out.println("Free Breakfast");
System.out.println("No pets allowed!");
System.out.println("Thank You!");
break;
case 5:
System.out.println("Here is your reservation as of: " + d.toString());
System.out.println("Here is your reservation: ");
System.out.println(name+" |"+num+"| Room 105 | Php 5,000 | 2 Persons|");
System.out.println("Check-in Date: " + checkIn);
System.out.println("Check-Out Date: " + checkOut);
System.out.println("Additional 1 beddings");
System.out.println("Free Breakfast");
System.out.println("No pets allowed!");
System.out.println("Thank You!");
break;
default:
System.out.println("Number"+" "+choices + " is not given. Please select within the given rooms.");
throw new InputMismatchException("");
}
control=2;
}catch(InputMismatchException ex) {
}
}while(control==1);
System.out.println("");
int choicess;
boolean b=true;
do{
try{
System.out.println("[1] CANCEL");
System.out.println("[2] RESERVE");
System.out.println("Select from above: ");
choicess=ScanObj.nextInt();
switch(choicess)
{
case 1:
System.out.println("Cancellation Done Thanks!");
break;
case 2:
System.out.print("Your hotel room has been successfully reserved. Thank you!");
break;
default:
System.out.println("Error. Please select within the given numbers.");
System.out.println("Thank you!");
throw new InputMismatchException("");
}
b=false;
}catch(Exception exx) {
System.out.println("");
}
}while(b!=false);
}
}
Upvotes: -1
Reputation: 13934
Create three classes (Hotel
, Room
& Reservation
) :
Reservation
object is used like an invoice here, and is kept decoupled from booking process.
Each Room
object (dedicated for each room number in the hotel) contains a map
which stores reservedDates
as key and reservationObject
as value.
Hotel
composes of rooms
. For each booking request, Hotel
loops through the room
list, and with that, each room
traverse through its own map
to find if the booking is possible for asked days.
Note that the booking function is taking dates as list, not just two dates (as startDate and endDate). It's same thing as the former can be derived from the later.
Sample Code is as below:
class Hotel {
private String name, address;
private List<Room> roomList; //key : roomNumber
public Hotel(){
roomList = new ArrayList<Room>();
}
public Reservation bookRoomForDates(List<Integer> dateList, Guest guest){
for(Room room : roomList){
Reservation reservation = room.bookForGivenDates(dateList, guest);
if(reserved != null) return reservation; //Reservation successFull!
}
return null; //Reservation failed!
}
}
class Reservation {
private String id;
private Date inDate, outDate;
private Guest guest;
public Reservation(Room room, int startDate, int endDate, Guest guest){
//populate the member variables.
}
}
class Room {
private String id;
private int roomNumber, floorNum;
private Map<Integer, Reservation> reservedDates; // key : date as Integer (YYYYMMDD)
public Room(int roomNumber){
reservedDates = new HashMap<Integer, Reservation>();
this.roomNumber = roomNumber;
}
//A guest request for booking on dates(in YYYYMMDD format) in the dateList
public Reservation bookForGivenDates(List<Integer> dateList, Guest guest)
{
if(dateList.isEmpty()) return null;
for(Integer date : dateList){
Reservation res = reservedDates.get(date);
if(res != null) { // We don't store null value for unreserved dates for the room.
return null; // Room is reserved on this date by another guest. So, this room is unavailable.
}
}
//this room is unreserved on all requested dates. So go on and reserve this room for asked dates
int startDate = dateList.get(0);
int endDate = dateList.get(dateList.size() - 1);
Reservation newReservation = new Reservation(this, startDate, endDate, guest);
for(Integer date : dateList){
reservedDates.put(date, newReservation);
}
return newReservation;
}
}
Upvotes: 10
Reputation: 340733
Think about it, Room is a resource that you can reserve for a given time range. Also you have several rooms which can be reserved independently. How would you implement the following methods:
class Room {
boolean isAvailable(Date date) {/*...*/}
Date nextAvailableDate() {/*...*/}
}
Hint: room has to know about its reservations.
It's not clear from your question what is the purpose of Reservation. Does it only contain a date range or is it assigned to a particular room? If the former, you might encounter a problem of finding a free room for a new reservations - this can be easily implemented by looping over all rooms and using methods above.
If the latter: since the reservations knows about the room, the room may also know about the reservation. So implementing the methods above is trivial by iterating over reservations.
Upvotes: 1
Reputation: 120198
EDIT -- on second thought, why not just let each Room
have a list of Reservation
instances, which in turn have start/end properties that tell you when the reservation occurs?
That way, to tell if a room has a reservation for a certain time, you just loop thru the reservations for the room and see if the time in question is within the start/end range of any of the reservations...granted that code is not too easy (nor too hard) to implement, but that's the basic idea.
Upvotes: 7