Import Java
Import Java
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 .