-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotelReservationSystem.java
More file actions
113 lines (96 loc) · 4.77 KB
/
HotelReservationSystem.java
File metadata and controls
113 lines (96 loc) · 4.77 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
package HotelDatabase;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import javax.swing.*;
public class HotelReservationSystem extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel CustomerNameLabel, roomTypeLabel, arrivalLabel, departureLabel;
private JTextField nameField, arrivalField, departureField;
private JComboBox<String> roomTypeComboBox;
private JButton submitButton;
private Hotel hotel;
public HotelReservationSystem() {
setTitle("ABC Hotel Booking System");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
hotel = new Hotel(10, 5, 3); // Example: 10 regular rooms, 5 deluxe rooms, 3 junior suites
CustomerNameLabel = new JLabel("Customer Name:");
nameField = new JTextField(20);
roomTypeLabel = new JLabel("Room Type:");
String[] roomTypes = {"Regular Room", "Deluxe Room", "Junior Suite"};
roomTypeComboBox = new JComboBox<>(roomTypes);
arrivalLabel = new JLabel("Arrival Date:");
arrivalField = new JTextField(10);
departureLabel = new JLabel("Departure Date:");
departureField = new JTextField(10);
submitButton = new JButton("Submit");
submitButton.addActionListener(e -> submitBooking());
JPanel panel = new JPanel();
panel.add(CustomerNameLabel);
panel.add(nameField);
panel.add(roomTypeLabel);
panel.add(roomTypeComboBox);
panel.add(arrivalLabel);
panel.add(arrivalField);
panel.add(departureLabel);
panel.add(departureField);
panel.add(submitButton);
add(panel);
setVisible(true);
}
private void submitBooking() {
String customerName = nameField.getText();
String roomType = (String) roomTypeComboBox.getSelectedItem();
String arrivalDate = arrivalField.getText();
String departureDate = departureField.getText();
// Validate input
if (customerName.isEmpty() || roomType.isEmpty() || arrivalDate.isEmpty() || departureDate.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please fill in all fields.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
// Parse dates in yyyy-MM-dd
LocalDate arrival = LocalDate.parse(arrivalDate);
LocalDate departure = LocalDate.parse(departureDate);
long numberOfNights = ChronoUnit.DAYS.between(arrival, departure);
// Attempt to make a reservation
Room room = hotel.makeReservation(roomType, arrivalDate, departureDate);
if (room != null) {
double totalCharge = room.calculateTotalCharge((int) numberOfNights);
JOptionPane.showMessageDialog(this, "Reservation successful!\n" +
"Customer Name: " + customerName + "\n" +
"Room Type: " + roomType + "\n" +
"Arrival Date: " + arrivalDate + "\n" +
"Departure Date: " + departureDate + "\n" +
"Room Number: " + room.getRoomNumber() + "\n" +
"Total Charge: $" + totalCharge);
} else {
JOptionPane.showMessageDialog(this, "No rooms available for the selected dates.", "Error", JOptionPane.ERROR_MESSAGE);
}
// Here you would typically check room availability and store the booking
// For demonstration purposes, let's just print the booking details
System.out.println("Booking submitted:");
System.out.println("Customer Name: " + customerName);
System.out.println("Room Type: " + roomType);
System.out.println("Arrival Date: " + arrivalDate);
System.out.println("Departure Date: " + departureDate);
// Optionally, clear the fields after submission
nameField.setText("");
roomTypeComboBox.setSelectedIndex(0);
arrivalField.setText("");
departureField.setText("");
}
public static void main(String[] args) {
Hotel hotel = new Hotel(10, 5, 3); // Create a hotel with 10 regular rooms, 5 deluxe rooms, and 3 junior suites
// Example: Check if a Regular Room is available
Room availableRoom = hotel.makeReservation("Regular Room", "2024-05-15", "2024-05-20");
if (availableRoom != null) {
System.out.println("Reservation successful for a Regular Room.");
} else {
System.out.println("No Regular Rooms available for the selected dates.");
}
}
}