Repository navigation
Expand file tree
/
Copy pathCheckOut.java
More file actions
181 lines (152 loc) · 6.86 KB
/
Copy pathCheckOut.java
File metadata and controls
181 lines (152 loc) · 6.86 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
package Hotel.Management.System;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
public class CheckOut extends JFrame {
JComboBox<String> customerBox;
JButton checkoutButton, backButton;
JTable roomTable;
DefaultTableModel tableModel;
public CheckOut() {
setTitle("Hotel Check-Out");
setSize(850, 500);
setLocationRelativeTo(null);
setLayout(null);
getContentPane().setBackground(new Color(3, 45, 48));
// Label
JLabel label = new JLabel("Select Customer:");
label.setForeground(Color.WHITE);
label.setBounds(50, 30, 150, 25);
add(label);
// Customer ComboBox
customerBox = new JComboBox<>();
customerBox.setBounds(200, 30, 300, 25);
add(customerBox);
// Checkout button
checkoutButton = new JButton("Checkout");
checkoutButton.setBounds(520, 30, 120, 30);
checkoutButton.setForeground(Color.WHITE);
checkoutButton.setBackground(Color.BLACK);
checkoutButton.addActionListener(e -> checkoutCustomer());
add(checkoutButton);
// Back button
backButton = new JButton("Back");
backButton.setBounds(660, 30, 120, 30);
backButton.setForeground(Color.WHITE);
backButton.setBackground(Color.BLACK);
backButton.addActionListener(e -> setVisible(false));
add(backButton);
// Room table
tableModel = new DefaultTableModel();
roomTable = new JTable(tableModel);
roomTable.setRowHeight(25);
roomTable.setFont(new Font("Tahoma", Font.PLAIN, 14));
JScrollPane scrollPane = new JScrollPane(roomTable);
scrollPane.setBounds(50, 80, 720, 360);
add(scrollPane);
// Load data
loadCustomers();
loadRoomData();
setVisible(true);
}
private void loadCustomers() {
try {
customerBox.removeAllItems();
connection c = new connection();
ResultSet rs = c.getStatement().executeQuery("SELECT customer_id, name, room FROM customer");
while (rs.next()) {
int id = rs.getInt("customer_id");
String name = rs.getString("name");
String room = rs.getString("room");
customerBox.addItem(id + " - " + name + " (Room: " + room + ")");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void loadRoomData() {
try {
connection c = new connection();
ResultSet rs = c.getStatement().executeQuery(
"SELECT roomnumber AS 'Room Number', price AS 'Price', cleaningstatus AS 'Cleaning Status', availability AS 'Availability', bedtype AS 'Bed Type' FROM room"
);
tableModel.setRowCount(0);
tableModel.setColumnCount(0);
// Column names
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
for (int i = 1; i <= colCount; i++) {
tableModel.addColumn(rsmd.getColumnLabel(i));
}
// Rows
while (rs.next()) {
Object[] row = new Object[colCount];
for (int i = 0; i < colCount; i++) {
row[i] = rs.getObject(i + 1);
}
tableModel.addRow(row);
}
// Color availability
roomTable.getColumn("Availability").setCellRenderer(new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
String status = (String) value;
if ("Available".equalsIgnoreCase(status)) {
c.setForeground(Color.GREEN.darker());
} else {
c.setForeground(Color.RED);
}
return c;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private void checkoutCustomer() {
String selected = (String) customerBox.getSelectedItem();
if (selected == null) {
JOptionPane.showMessageDialog(null, "No customer selected");
return;
}
int customerId = Integer.parseInt(selected.split(" - ")[0]);
try {
connection c = new connection();
Statement stmt = c.getStatement();
// 1️⃣ Update checkout info & total amount first
String updateCustomer = "UPDATE customer c " +
"JOIN room r ON c.room = r.roomnumber " +
"SET checkouttime = NOW(), " +
"total_amount = (DATEDIFF(NOW(), STR_TO_DATE(c.checkintime, '%a %b %d %H:%i:%s IST %Y')) + 1) * r.price - CAST(c.deposit AS UNSIGNED), " +
"payment_status = 'Paid' " +
"WHERE c.customer_id = " + customerId;
stmt.executeUpdate(updateCustomer);
// 2️⃣ Move updated record to checkout_history (explicit column mapping)
String moveToHistory =
"INSERT INTO checkout_history (customer_id, name, room, checkintime, checkouttime, deposit, total_amount, payment_status) " +
"SELECT customer_id, name, room, checkintime, checkouttime, deposit, total_amount, payment_status " +
"FROM customer WHERE customer_id = " + customerId;
stmt.executeUpdate(moveToHistory);
// 3️⃣ Update room status
String updateRoom = "UPDATE room SET cleaningstatus='Dirty', availability='Available' " +
"WHERE roomnumber = (SELECT room FROM customer WHERE customer_id = " + customerId + ")";
stmt.executeUpdate(updateRoom);
// 4️⃣ Delete from active customer table
stmt.executeUpdate("DELETE FROM customer WHERE customer_id = " + customerId);
JOptionPane.showMessageDialog(null, "Checkout completed successfully!");
// Refresh table and customer list
loadRoomData();
loadCustomers();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Checkout failed: " + e.getMessage());
}
}
public static void main(String[] args) {
new CheckOut();
}
}