-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotelReservationSystem.java
More file actions
300 lines (248 loc) · 12.2 KB
/
Copy pathHotelReservationSystem.java
File metadata and controls
300 lines (248 loc) · 12.2 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Name Shayan ADil Khan
// id : CA/S1/9549
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
public class HotelReservationSystem {
private static final Map<String, Room> rooms = new HashMap<>();
private static final Map<String, Booking> bookings = new HashMap<>();
public static void main(String[] args) {
// Sample room data
rooms.put("101", new Room("101", "Single", 100.0, true));
rooms.put("102", new Room("102", "Double", 150.0, true));
rooms.put("103", new Room("103", "Suite", 250.0, false));
rooms.put("104", new Room("104", "Single", 100.0, true));
rooms.put("105", new Room("105", "Double", 150.0, true));
rooms.put("106", new Room("106", "Suite", 250.0, true));
rooms.put("107", new Room("107", "Single", 100.0, true));
rooms.put("108", new Room("108", "Double", 150.0, true));
rooms.put("109", new Room("109", "Suite", 250.0, true));
rooms.put("110", new Room("110", "Single", 100.0, true));
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Hotel Reservation System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 700); // Increased size of the frame
frame.add(new MainPanel());
frame.setVisible(true);
});
}
static class MainPanel extends JPanel {
private CardLayout cardLayout;
private JPanel cardPanel;
private JTextArea detailsArea;
private JTextField searchRoomField;
private JTextField nameField;
private JTextField roomField;
private JTextField paymentField;
public MainPanel() {
setLayout(new BorderLayout());
setBackground(new Color(144, 238, 144)); // Light green background color
// Adding the label with your name and ID
JLabel headerLabel = new JLabel("Shayan Adil Khan ID: CA/S1/9549", SwingConstants.CENTER);
headerLabel.setFont(new Font("Arial", Font.BOLD, 20));
headerLabel.setForeground(Color.BLACK);
add(headerLabel, BorderLayout.NORTH);
cardLayout = new CardLayout();
cardPanel = new JPanel(cardLayout);
cardPanel.add(createSearchPanel(), "Search");
cardPanel.add(createReservationPanel(), "Reserve");
cardPanel.add(createBookingDetailsPanel(), "Details");
add(cardPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(new Color(144, 238, 144)); // Light green for button panel
JButton searchButton = createStyledButton("Search Rooms");
JButton reserveButton = createStyledButton("Make Reservation");
JButton detailsButton = createStyledButton("Booking Details");
searchButton.addActionListener(e -> cardLayout.show(cardPanel, "Search"));
reserveButton.addActionListener(e -> cardLayout.show(cardPanel, "Reserve"));
detailsButton.addActionListener(e -> cardLayout.show(cardPanel, "Details"));
buttonPanel.add(searchButton);
buttonPanel.add(reserveButton);
buttonPanel.add(detailsButton);
add(buttonPanel, BorderLayout.SOUTH); // Changed to SOUTH to avoid overlapping with the header label
}
private JButton createStyledButton(String text) {
JButton button = new JButton(text);
button.setBackground(Color.DARK_GRAY);
button.setForeground(Color.WHITE);
button.setFocusPainted(false);
button.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));
button.setPreferredSize(new Dimension(150, 40));
button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
// Add a hover effect
button.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseEntered(java.awt.event.MouseEvent evt) {
button.setBackground(Color.GRAY);
}
@Override
public void mouseExited(java.awt.event.MouseEvent evt) {
button.setBackground(Color.DARK_GRAY);
}
});
return button;
}
private JPanel createSearchPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
searchRoomField = new JTextField(20); // Increased length of the search field
searchRoomField.setFont(new Font("Arial", Font.BOLD, 16)); // Bold and increased font size
JLabel searchLabel = new JLabel("Enter Room Number:");
searchLabel.setFont(new Font("Arial", Font.BOLD, 16)); // Bold and increased font size
JButton searchButton = createStyledButton("Search");
searchButton.addActionListener(new SearchButtonListener());
JPanel inputPanel = new JPanel();
inputPanel.add(searchLabel); // Label for input
inputPanel.add(searchRoomField); // Adding text field to input panel
inputPanel.add(searchButton); // Adding button to input panel
detailsArea = new JTextArea(15, 50);
detailsArea.setEditable(false);
detailsArea.setFont(new Font("Arial", Font.PLAIN, 16)); // Increased font size for details
panel.add(inputPanel, BorderLayout.NORTH); // Add input panel to the top of the search panel
panel.add(new JScrollPane(detailsArea), BorderLayout.CENTER); // Add details area to the center
return panel;
}
private JPanel createReservationPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 2));
JLabel nameLabel = new JLabel("Name:");
nameLabel.setFont(new Font("Arial", Font.BOLD, 16)); // Bold and increased font size
panel.add(nameLabel);
nameField = new JTextField();
nameField.setFont(new Font("Arial", Font.BOLD, 16)); // Bold and increased font size
panel.add(nameField);
JLabel roomLabel = new JLabel("Room Number:");
roomLabel.setFont(new Font("Arial", Font.BOLD, 16)); // Bold and increased font size
panel.add(roomLabel);
roomField = new JTextField();
roomField.setFont(new Font("Arial", Font.BOLD, 16)); // Bold and increased font size
panel.add(roomField);
JLabel paymentLabel = new JLabel("Payment Amount:");
paymentLabel.setFont(new Font("Arial", Font.BOLD, 16)); // Bold and increased font size
panel.add(paymentLabel);
paymentField = new JTextField();
paymentField.setFont(new Font("Arial", Font.BOLD, 16)); // Bold and increased font size
panel.add(paymentField);
JButton reserveButton = createStyledButton("Reserve");
reserveButton.addActionListener(new ReserveButtonListener());
panel.add(reserveButton);
return panel;
}
private JPanel createBookingDetailsPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JTextArea bookingDetailsArea = new JTextArea(15, 50);
bookingDetailsArea.setEditable(false);
bookingDetailsArea.setFont(new Font("Arial", Font.PLAIN, 16)); // Increased font size for booking details
panel.add(new JScrollPane(bookingDetailsArea), BorderLayout.CENTER);
JButton refreshButton = createStyledButton("Refresh");
refreshButton.addActionListener(e -> {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Booking> entry : bookings.entrySet()) {
sb.append("Booking ID: ").append(entry.getKey()).append("\n")
.append("Name: ").append(entry.getValue().getName()).append("\n")
.append("Room Number: ").append(entry.getValue().getRoomNumber()).append("\n")
.append("Amount Paid: $").append(entry.getValue().getAmountPaid()).append("\n")
.append("--------\n");
}
bookingDetailsArea.setText(sb.toString());
});
panel.add(refreshButton, BorderLayout.SOUTH);
return panel;
}
private class SearchButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String roomNumber = searchRoomField.getText();
Room room = rooms.get(roomNumber);
if (room != null) {
detailsArea.setText("Room Number: " + room.getNumber() + "\n" +
"Category: " + room.getCategory() + "\n" +
"Price: $" + room.getPrice() + "\n" +
"Available: " + (room.isAvailable() ? "Yes" : "No"));
} else {
detailsArea.setText("Room not found.");
}
}
}
private class ReserveButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String roomNumber = roomField.getText();
String paymentAmount = paymentField.getText();
Room room = rooms.get(roomNumber);
if (room != null) {
if (room.isAvailable()) {
double amount;
try {
amount = Double.parseDouble(paymentAmount);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Invalid payment amount.");
return;
}
bookings.put(roomNumber, new Booking(name, roomNumber, amount));
room.setAvailable(false);
JOptionPane.showMessageDialog(null, "Reservation successful.");
nameField.setText("");
roomField.setText("");
paymentField.setText("");
} else {
JOptionPane.showMessageDialog(null, "Room is not available.");
}
} else {
JOptionPane.showMessageDialog(null, "Room not found.");
}
}
}
}
static class Room {
private String number;
private String category;
private double price;
private boolean available;
public Room(String number, String category, double price, boolean available) {
this.number = number;
this.category = category;
this.price = price;
this.available = available;
}
public String getNumber() {
return number;
}
public String getCategory() {
return category;
}
public double getPrice() {
return price;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
}
static class Booking {
private String name;
private String roomNumber;
private double amountPaid;
public Booking(String name, String roomNumber, double amountPaid) {
this.name = name;
this.roomNumber = roomNumber;
this.amountPaid = amountPaid;
}
public String getName() {
return name;
}
public String getRoomNumber() {
return roomNumber;
}
public double getAmountPaid() {
return amountPaid;
}
}
}