/
Hotel.java
115 lines (97 loc) · 4.44 KB
/
Hotel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.util.*;
public class Hotel {
// Array to track room availability status
private static int[] roomCount = new int[101];
// List to store booked rooms, each element is an array [roomNumber, nightsBooked]
private static List<int[]> bookings = new ArrayList<>();
public static void main(String[] args) {
// Scanner for getting the user input
Scanner scanner = new Scanner(System.in);
// Main program loop
while (true) {
// Display main menu options
System.out.println("----------- Hotel Reservation System -----------");
System.out.println("0 - Exit");
System.out.println("1 - Make a Reservation");
System.out.println("2 - View Booked Rooms");
System.out.print("Enter your choice: ");
// Menu choice
int choice = scanner.nextInt();
// Switch for user choice
switch (choice) {
case 0:
// Exit the program
System.out.println("Exiting the program.");
scanner.close();
System.exit(0);
break;
case 1:
// Call reservation method to handle room booking
reservation(scanner);
break;
case 2:
// Prompt user for room number to search
System.out.print("Enter the room number you want to search: ");
int roomNumberToSearch = scanner.nextInt();
// Call booked method to view details for a specific room
booked(roomNumberToSearch);
break;
default:
// Invalid choice
System.out.println("Invalid choice. Try again.");
}
}
}
// Method for handling room reservation
public static void reservation(Scanner scanner) {
// Prompt user for room number
System.out.print("Enter the room number (1-100) you would like to book: ");
int roomNumber = scanner.nextInt();
// Check if the room number is valid
if (roomNumber >= 1 && roomNumber <= 100) {
// Check if the room is available
if (roomCount[roomNumber] == 0) {
// Mark the room as booked
roomCount[roomNumber] = roomNumber;
// Prompt user for the number of nights
System.out.print("For how many nights would you like to book the room? (Room rate is EUR 100 per night): ");
int roomNights = scanner.nextInt();
// Display booking confirmation
System.out.println("You have chosen to book Room " + roomNumber + " for " + roomNights +
" nights. The total bill for this booking will be " + roomNights * 100 + " EUR");
System.out.println("Room " + roomNumber + " has been successfully booked!");
// Record the booking in the list
int[] roomsBooked = { roomNumber, roomNights };
bookings.add(roomsBooked);
} else {
// Room is already booked
System.out.println("Room " + roomNumber + " is already booked. Choose another one.");
}
} else {
// Invalid room number
System.out.println("Invalid room number. Enter a number between 1 and 100.");
}
}
// Method for viewing details for a specific room
public static void booked(int roomNumberToSearch) {
// Display section heading
System.out.println("----------- Booked Room Details -----------");
boolean roomFound = false;
// Loop through the list of bookings
for (int[] booking : bookings) {
// Check if the current booking matches the room number to search
if (booking[0] == roomNumberToSearch) {
// Display details for the found room
System.out.println("Room Number: " + booking[0]);
System.out.println("Number of nights booked for: " + booking[1]);
System.out.println("Total Cost: " + booking[1] * 100 + " EUR");
roomFound = true;
break;
}
}
// If room not found
if (!roomFound) {
System.out.println("Room " + roomNumberToSearch + " not found or not booked yet.");
}
}
}