0% found this document useful (0 votes)
6 views4 pages

Import Java

The document defines a HotelManagementSystem with Hotel and Room classes to manage a hotel. The Hotel class tracks an array of rooms and allows checking guests in and out of rooms by room number. It also displays available rooms. The Room class represents individual rooms with attributes like number, guest, and occupancy status. The main method initializes sample rooms and a hotel, then displays a menu for check in, check out, viewing available rooms, and exiting.
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)
6 views4 pages

Import Java

The document defines a HotelManagementSystem with Hotel and Room classes to manage a hotel. The Hotel class tracks an array of rooms and allows checking guests in and out of rooms by room number. It also displays available rooms. The Room class represents individual rooms with attributes like number, guest, and occupancy status. The main method initializes sample rooms and a hotel, then displays a menu for check in, check out, viewing available rooms, and exiting.
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

import [Link].

Scanner;
// Hotel class represents the hotel and its operations
class Hotel {
private Room[] rooms;
public Hotel(String hotelName, Room[] rooms) {
[Link] = rooms;
}
// Display available rooms in the hotel
public void displayAvailableRooms() {
[Link]("Available Rooms:");
for (Room room : rooms) {
if (![Link]()) {
[Link](room);
}
}
}
// Check-in a guest to a room
public void checkIn() {
Scanner scanner = new Scanner([Link]);
[Link]("Enter room number: ");
int roomNumber = [Link]();
// Find the room by its number
Room room = findRoom(roomNumber);
if (room != null) {
if ([Link]()) {
[Link]("Room " + roomNumber + " is already
occupied.");
} else {
[Link]("Enter guest name: ");
String guestName = [Link]();
[Link](guestName);
[Link](true);
[Link]("Guest " + guestName + " checked into room
" + roomNumber + ".");
}
} else {
[Link]("Room " + roomNumber + " does not exist.");
}
}
// Check-out a guest from a room
public void checkOut() {
Scanner scanner = new Scanner([Link]);
[Link]("Enter room number: ");
int roomNumber = [Link]();
// Find the room by its number
Room room = findRoom(roomNumber);
if (room != null) {
if ([Link]()) {
String guestName = [Link]();
[Link]("");
[Link](false);
[Link]("Guest " + guestName + " checked out of
room " + roomNumber + ".");
} else {
[Link]("Room " + roomNumber + " is not
occupied.");
}
} else {
[Link]("Room " + roomNumber + " does not exist.");
}
}
// Find a room by its number
private Room findRoom(int roomNumber) {
for (Room room : rooms) {
if ([Link]() == roomNumber) {
return room;
}
}
return null;
}
}
// Room class represents a hotel room
class Room {
private int roomNumber;
private String guestName;
private boolean occupied;
public Room(int roomNumber) {
[Link] = roomNumber;
[Link] = "";
[Link] = false;
}
public int getRoomNumber() {
return roomNumber;
}
public String getGuestName() {
return guestName;
}
public void setGuestName(String guestName) {
[Link] = guestName;
}
public boolean isOccupied() {
return occupied;
}
public void setOccupied(boolean occupied) {
[Link] = occupied;
}
@Override
public String toString() {
return "Room " + roomNumber + ": " + (occupied ? "Occupied by " +
guestName : "Available");
}
}
// HotelManagementSystem class contains the main method to run the program
public class HotelManagementSystem {
public static void main(String[] args) {
// Create an array of rooms
Room[] rooms = {
new Room(101),
new Room(102),
new Room(103),
new Room(201),
new Room(202),
new Room(203),
};
// Create a hotel object
Hotel hotel = new Hotel("Grand Hotel", rooms);
Scanner scanner = new Scanner([Link]);
// Display menu and handle user input
while (true) {
[Link]("\n--- Hotel Management System by DataFlair ---
");
[Link]("1. Check-in");
[Link]("2. Check-out");
[Link]("3. View available rooms");
[Link]("4. Exit");
[Link]("Enter your choice: ");
int choice = [Link]();
switch (choice) {
case 1:
[Link]();
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 4:
[Link]("Exiting the system...");
[Link](0);
break;
default:
[Link]("Invalid choice. Please try again.");
}
}
}
}

Common questions

Powered by AI

Encapsulation in the Room class is demonstrated by private access control on its fields (roomNumber, guestName, occupied). Methods such as setGuestName() and setOccupied() provide controlled access, ensuring that internal data is modified only through defined interfaces, which helps maintain data integrity and abstraction. This design prevents direct modifications of the internal state from outside the class, safeguarding against potential inconsistencies or unauthorized access .

The Room class includes several helper methods: getRoomNumber(), getGuestName(), setGuestName(String), isOccupied(), setOccupied(boolean), and toString(). These methods encapsulate the properties of a Room, allowing access and modification of the room number, guest name, and occupancy status while maintaining data integrity. The toString() method provides a readable representation of the room's current status, helping in the management of guest information and room availability .

To check in a guest, the program first prompts the user to enter a room number. It then uses the findRoom() method to locate the corresponding Room object. If the room is found and is not occupied, the user is prompted to enter the guest's name, which is then stored in the Room object using setGuestName(), and the room's occupancy status is set to true using setOccupied(). A confirmation message is displayed once the operation is successful. If the room is already occupied or does not exist, appropriate error messages are shown .

Using Scanner carries risks such as resource leaks if not properly closed and exceptions from invalid inputs like mismatched data types or end-of-input errors. These can be mitigated by closing the Scanner instance properly, using try-catch blocks to handle exceptions, and implementing input validation checks to ensure that user inputs are correct and of expected format before processing them .

Potential improvements could include better input validation to prevent invalid room numbers or non-integer inputs, implementing a graphical user interface for enhanced user experience, and adding functionality to handle special requests or preferences for guests. Additionally, expanding the system to support online check-ins or integrating with third-party booking systems could provide a more comprehensive solution .

The program could benefit from implementing the Singleton pattern for the Scanner object to avoid creating multiple unnecessary instances. Additionally, the MVC (Model-View-Controller) pattern could be applied where Room and Hotel represent the model, user commands act as the controller, and results displayed on the console serve as the view. This separation can improve code organization and maintainability by clearly delineating responsibilities .

The HotelManagementSystem class serves as the main entry point of the program. It initializes an array of Room objects and creates a Hotel object with these rooms. Through a loop, it manages user interactions, invoking methods on the Hotel object based on user input to perform check-ins, check-outs, and display available rooms. This class integrates the functionalities of Room and Hotel classes, facilitating a user-friendly hotel management experience .

Error handling during check-ins and check-outs is implemented through condition checks and user feedback. For check-ins, the program displays an error if a non-existent room is entered or if the room is already occupied. For check-outs, it checks if the room exists and if it is actually occupied. If any conditions are not met, informative messages are presented to the user to rectify the input. This approach, while basic, ensures that incorrect operations are prevented without crashing the program .

The program determines if a room is occupied by checking the boolean variable 'occupied' within each Room object. The isOccupied() method returns the value of this variable. If it is true, the room is occupied; otherwise, it is available. This is verified in methods like displayAvailableRooms() and checkIn(), where the occupancy status is checked before performing actions .

The primary responsibilities of the Hotel class include managing room availability, handling guest check-ins and check-outs, and interfacing with the user to provide a list of available rooms. It maintains an array of Room objects and includes methods to display available rooms, check guests into and out of rooms, and find a specific room by its number .

You might also like