-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotelManagementSystem.java
More file actions
106 lines (91 loc) · 3.08 KB
/
HotelManagementSystem.java
File metadata and controls
106 lines (91 loc) · 3.08 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
import java.util.concurrent.*;
import lab9.HotelManagementSystem;
// Class representing a guest in the hotel
class Guest extends Thread {
private String name;
public Guest(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println(name + " has checked into the hotel.");
// Simulate making requests
makeRequest("Towel");
makeRequest("Breakfast");
System.out.println(name + " has completed their stay and checked out.");
}
private void makeRequest(String request) {
// Simulate time to make a request
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + " is requesting " + request);
HotelManagementSystem.placeRequest(request);
}
}
// Class representing the hotel staff
class HotelStaff extends Thread {
private BlockingQueue<String> requests;
private volatile boolean isRunning = true;
public HotelStaff(BlockingQueue<String> requests) {
this.requests = requests;
}
public void stopProcessing() {
isRunning = false;
// Interrupt the thread to break out of blocking operation
interrupt();
}
@Override
public void run() {
while (isRunning) {
try {
String request = requests.take();
System.out.println("Hotel staff is fulfilling request: " + request);
// Simulate time to fulfill the request
Thread.sleep(3000);
System.out.println("Hotel staff has fulfilled the request: " + request);
} catch (InterruptedException e) {
// Handle interruption, e.g., exit the loop
System.out.println("Hotel staff thread interrupted. Exiting.");
isRunning = false;
}
}
}
}
// Class representing the hotel management system
public class HotelManagementSystem {
private static BlockingQueue<String> requests = new LinkedBlockingQueue<>();
private static HotelStaff hotelStaff;
public static void placeRequest(String request) {
try {
requests.put(request);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
hotelStaff = new HotelStaff(requests);
hotelStaff.start();
// Simulate multiple guests
for (int i = 1; i <= 3; i++) {
Guest guest = new Guest("Guest " + i);
guest.start();
// Simulate time between guests
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Simulate time for guests to complete their requests
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Stop hotel staff processing after guests have completed their requests
hotelStaff.stopProcessing();
}
}