0% found this document useful (0 votes)
22 views7 pages

Java Hotel Management

The document outlines a Java program for automating room management and booking services at a hotel. It defines classes for Room, Booking, and Hotel, detailing their attributes and methods for adding rooms, booking rooms, and searching for available rooms based on specific criteria. The program includes a user interface for interacting with these functionalities through a console menu.

Uploaded by

tgokulgandhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views7 pages

Java Hotel Management

The document outlines a Java program for automating room management and booking services at a hotel. It defines classes for Room, Booking, and Hotel, detailing their attributes and methods for adding rooms, booking rooms, and searching for available rooms based on specific criteria. The program includes a user interface for interacting with these functionalities through a console menu.

Uploaded by

tgokulgandhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PROGRAMMING WITH JAVA (202044502)

PRACTICAL 13

Seven Seas, a reputed 3-star hotel of Gandhinagar, wants to automate its room
management and booking services for providing better service to their customers.
Define the classes as shown in them below class diagram, specifying all the getters and
setters for each class.

Booking object can only be created by specifying the room number, guest name,
guest email id and number of days guest would be staying at the hotel. Room object can
be created only by-passing room type (“S” - Standard, “D” - Deluxe and “L” - Luxury),
daily rent and specifying whether the room has sea view or not. Room number will be
assigned by the system as and when the room gets added (starting from room number
101). For every new room added, the default occupancy status will be set as false. Hotel
object can be created without passing any arguments.

Hotel class should be defined as public class and it has the following behaviors:

1) addRoom: This behavior accepts 3 arguments, i.e. room type, daily room rent and
sea view flag. It assigns a room number to the added room and returns the room
number.

2) bookRoom: This behavior accepts an unoccupied room number, guest’s name,


email id and the number of days the guest wants to stay at the hotel room (in given
order). The behavior sets the bill attribute and returns the same. If the specified
room is occupied, the behavior does not set the bill attribute and returns -1.

3) searchRoom: This behavior allows to search with two different criteria:


a) It accepts the type of room desired and flag that specifies if sea view is also
required.
b) It accepts the upper limit the guest would like to pay as daily rent for a room
For both (a) and (b) criteria, the behavior is supposed to return the number of
unoccupied room matching the specified criteria.

NOTE: Data and Business validations need not be handled.

12302080501057 Teesha Gokulgandhi


PROGRAMMING WITH JAVA (202044502)

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

class Room {
private static int roomCounter = 100;
private int roomNumber;
private String type; // "S" - Standard, "D" - Deluxe, "L" - Luxury
private double dailyRent;
private boolean hasSeaView;
private boolean isOccupied;

public Room(String type, double dailyRent, String hasSeaView) {


this.roomNumber = ++roomCounter;
this.type = type;
this.dailyRent = dailyRent;
this.hasSeaView = hasSeaView.equalsIgnoreCase("Yes");
this.isOccupied = false;
}
public int getRoomNumber() { return roomNumber; }
public String getType() { return type; }
public double getDailyRent() { return dailyRent; }
public boolean hasSeaView() { return hasSeaView; }
public boolean isOccupied() { return isOccupied; }
public void setOccupied(boolean occupied) { isOccupied = occupied; }
}
class Booking {
private int roomNumber;
private String guestName;
private String guestEmail;
private int numberOfDays;
private double bill;

public Booking(int roomNumber, String guestName, String guestEmail, int


numberOfDays, double dailyRent) {

12302080501057 Teesha Gokulgandhi


PROGRAMMING WITH JAVA (202044502)

this.roomNumber = roomNumber;
this.guestName = guestName;
this.guestEmail = guestEmail;
this.numberOfDays = numberOfDays;
this.bill = numberOfDays * dailyRent;
}

public double getBill() { return bill; }


}
public class Hotel {
private ArrayList<Room> rooms;
private ArrayList<Booking> bookings;

public Hotel() {
rooms = new ArrayList<>();
bookings = new ArrayList<>();
}

public int addRoom(String type, double dailyRent, String hasSeaView) {


Room newRoom = new Room(type, dailyRent, hasSeaView);
rooms.add(newRoom);
return newRoom.getRoomNumber();
}
public double bookRoom(int roomNumber, String guestName, String guestEmail,
int numberOfDays) {
for (Room room : rooms) {
if (room.getRoomNumber() == roomNumber) {
if (!room.isOccupied()) {
room.setOccupied(true);
Booking booking = new Booking(roomNumber, guestName, guestEmail,
numberOfDays, room.getDailyRent());
bookings.add(booking);
return booking.getBill();
}
return -1;
}

12302080501057 Teesha Gokulgandhi


PROGRAMMING WITH JAVA (202044502)

}
return -1;
}

public int searchRoom(String type, String hasSeaView) {


boolean seaViewFlag = hasSeaView.equalsIgnoreCase("Yes");
int count = 0;
for (Room room : rooms) {
if (room.getType().equals(type) && room.hasSeaView() == seaViewFlag &&
!room.isOccupied()) {
count++;
}
}
return count;
}
public int searchRoom(double maxRent) {
int count = 0;
for (Room room : rooms) {
if (room.getDailyRent() <= maxRent && !room.isOccupied()) {
count++;
}
}
return count;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Hotel hotel = new Hotel();
while (true) {
System.out.println("\n1. Add Room");
System.out.println("2. Book Room");
System.out.println("3. Search Room by Type and Sea View");
System.out.println("4. Search Room by Rent");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();

12302080501057 Teesha Gokulgandhi


PROGRAMMING WITH JAVA (202044502)

switch (choice) {
case 1:
System.out.print("Enter Room Type (S/D/L): ");
System.out.print("Enter Maximum Rent: ");

String type = scanner.nextLine();


System.out.print("Enter Daily Rent: ");
double rent = scanner.nextDouble();
scanner.nextLine(); // Consume newline
System.out.print("Has Sea View? (Yes/No): ");
String seaView = scanner.nextLine();
int roomNum = hotel.addRoom(type, rent, seaView);
System.out.println("Room added successfully! Room Number: " +
roomNum);
break;
case 2:
System.out.print("Enter Room Number: ");
int roomNumber = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter Guest Name: ");
String guestName = scanner.nextLine();
System.out.print("Enter Guest Email: ");
String guestEmail = scanner.nextLine();
System.out.print("Enter Number of Days: ");
int days = scanner.nextInt();
double bill = hotel.bookRoom(roomNumber, guestName, guestEmail,
days);
if (bill == -1) {
System.out.println("Room is occupied or not found.");
} else {
System.out.println("Room booked! Total Bill: " + bill);
}
break;
case 3:
System.out.print("Enter Room Type (S/D/L): ");
String searchType = scanner.nextLine();
System.out.print("Require Sea View? (Yes/No): ");

12302080501057 Teesha Gokulgandhi


PROGRAMMING WITH JAVA (202044502)

String searchSeaView = scanner.nextLine();


int availableRooms = hotel.searchRoom(searchType, searchSeaView);
System.out.println("Available Rooms: " + availableRooms);
break;
case 4:
double maxRent = scanner.nextDouble();
int roomsUnderRent = hotel.searchRoom(maxRent);
System.out.println("Available Rooms: " + roomsUnderRent);
break;
case 5:
System.out.println("Exiting... Thank you!");
scanner.close();
return;
default:
System.out.println("Invalid choice. Try again.");
}
}
}
}

12302080501057 Teesha Gokulgandhi


PROGRAMMING WITH JAVA (202044502)

OUTPUT:

12302080501057 Teesha Gokulgandhi

You might also like