-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab_8.java
More file actions
192 lines (165 loc) · 5.6 KB
/
lab_8.java
File metadata and controls
192 lines (165 loc) · 5.6 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import java.util.*;
class Guest {
private String name;
private int age;
public Guest(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
class Room {
private int roomNumber;
private boolean isOccupied;
public Room(int roomNumber) {
this.roomNumber = roomNumber;
this.isOccupied = false;
}
public int getRoomNumber() {
return roomNumber;
}
public boolean isOccupied() {
return isOccupied;
}
public void occupy() {
isOccupied = true;
}
public void vacate() {
isOccupied = false;
}
}
class Reservation {
private int reservationId;
private Guest guest;
private int roomNumber;
public Reservation(int reservationId, Guest guest, int roomNumber) {
this.reservationId = reservationId;
this.guest = guest;
this.roomNumber = roomNumber;
}
public int getReservationId() {
return reservationId;
}
public Guest getGuest() {
return guest;
}
public int getRoomNumber() {
return roomNumber;
}
}
class Hotel {
private String name;
private Map<Integer, Room> rooms;
private List<Reservation> reservations;
public Hotel(String name) {
this.name = name;
this.rooms = new HashMap<>();
this.reservations = new ArrayList<>();
}
public void addRoom(int roomNumber) {
Room room = new Room(roomNumber);
rooms.put(roomNumber, room);
}
public void displayAvailableRooms() {
System.out.println("Available Rooms in " + name + ":");
for (Room room : rooms.values()) {
if (!room.isOccupied()) {
System.out.println("Room " + room.getRoomNumber());
}
}
}
public void bookRoom(int roomNumber, Guest guest) {
Room room = rooms.get(roomNumber);
if (room != null && !room.isOccupied()) {
room.occupy();
Reservation reservation = new Reservation(reservations.size() + 1, guest, roomNumber);
reservations.add(reservation);
System.out.println("Room " + roomNumber + " booked successfully for " + guest.getName());
} else {
System.out.println("Room " + roomNumber + " is not available for booking.");
}
}
public void vacateRoom(int roomNumber) {
Room room = rooms.get(roomNumber);
if (room != null && room.isOccupied()) {
room.vacate();
Reservation reservationToRemove = null;
for (Reservation reservation : reservations) {
if (reservation.getRoomNumber() == roomNumber) {
reservationToRemove = reservation;
break;
}
}
if (reservationToRemove != null) {
reservations.remove(reservationToRemove);
System.out.println("Room " + roomNumber + " vacated successfully.");
}
} else {
System.out.println("Room " + roomNumber + " is not occupied.");
}
}
public void displayGuestList() {
System.out.println("Guest List in " + name + ":");
for (Reservation reservation : reservations) {
System.out.println("Reservation ID: " + reservation.getReservationId() +
", Guest: " + reservation.getGuest().getName() +
", Room: " + reservation.getRoomNumber());
}
}
}
public class lab_8 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Create a hotel
Hotel myHotel = new Hotel("Grand Hotel");
// Add some rooms to the hotel
myHotel.addRoom(101);
myHotel.addRoom(102);
myHotel.addRoom(103);
// User interaction
int choice;
do {
System.out.println("\n1. Display available rooms");
System.out.println("2. Book a room");
System.out.println("3. Vacate a room");
System.out.println("4. Display guest list");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
myHotel.displayAvailableRooms();
break;
case 2:
scanner.nextLine(); // Consume the newline character
System.out.print("Enter guest name: ");
String guestName = scanner.nextLine();
System.out.print("Enter guest age: ");
int guestAge = scanner.nextInt();
System.out.print("Enter the room number to book: ");
int roomToBook = scanner.nextInt();
Guest guest = new Guest(guestName, guestAge);
myHotel.bookRoom(roomToBook, guest);
break;
case 3:
System.out.print("Enter the room number to vacate: ");
int roomToVacate = scanner.nextInt();
myHotel.vacateRoom(roomToVacate);
break;
case 4:
myHotel.displayGuestList();
break;
case 5:
System.out.println("Exiting the program. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please enter a valid option.");
}
} while (choice != 5);
}
}