-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotel.java
More file actions
336 lines (280 loc) · 13.9 KB
/
Copy pathHotel.java
File metadata and controls
336 lines (280 loc) · 13.9 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*import Library */
import com.toedter.calendar.JDateChooser;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Hotel extends JFrame {
Connection con = Connect.getConnection();
private int currentUserId;
public int getCurrentUserId() {
return currentUserId;
}
/*
function Hotel avis Interface
*/
public Hotel() {
setTitle("Avis_Hotel");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu homeMenu = new JMenu("Home");
JMenuItem connectItem = new JMenuItem("Connect to Database");
JMenuItem exitItem = new JMenuItem("Exit");
JMenuItem dashboard = new JMenuItem("Dashboard");
connectItem.addActionListener(e -> showLoginDialog());
dashboard.addActionListener(e -> {
new Dashboard(this);
});
exitItem.addActionListener(e -> System.exit(0));
menuBar.add(homeMenu);
menuBar.add(connectItem);
menuBar.add(dashboard);
menuBar.add(exitItem);
setJMenuBar(menuBar);
setLayout(new BorderLayout());
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(6, 2, 10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
JLabel rateLabel = new JLabel("Rate :");
JComboBox<Integer> rateComboBox = new JComboBox<>(new Integer[]{1, 2, 3, 4, 5});
JLabel timeLabel = new JLabel("Date :");
JDateChooser jDateChooser1 = new JDateChooser();
JLabel typeLabel = new JLabel("Type of Trip:");
JPanel checkboxPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JCheckBox typeCheckBox1 = new JCheckBox("SOLO");
JCheckBox typeCheckBox2 = new JCheckBox("Couple");
JCheckBox typeCheckBox3 = new JCheckBox("Family");
JCheckBox typeCheckBox4 = new JCheckBox("Job");
JCheckBox typeCheckBox5 = new JCheckBox("Friends");
checkboxPanel.add(typeCheckBox1);
checkboxPanel.add(typeCheckBox2);
checkboxPanel.add(typeCheckBox3);
checkboxPanel.add(typeCheckBox4);
checkboxPanel.add(typeCheckBox5);
JLabel securiteLabel = new JLabel("Security");
JComboBox<Integer> securiteComboBox = new JComboBox<>(new Integer[]{1, 2, 3, 4, 5});
JLabel commentLabel = new JLabel("Your Comment!:");
JTextArea commentTextArea = new JTextArea();
JButton submitButton = new JButton("Submit");
submitButton.setFont(new Font("Arial", Font.BOLD, 14));
submitButton.setBackground(new Color(50, 150, 50));
submitButton.setForeground(Color.WHITE);
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
submitFeedback(rateComboBox, jDateChooser1, typeCheckBox1, typeCheckBox2, typeCheckBox3, typeCheckBox4, typeCheckBox5, securiteComboBox, commentTextArea);
}
});
JButton effaceButton = new JButton("Clear !");
effaceButton.setFont(new Font("Arial", Font.BOLD, 14));
effaceButton.setBackground(new Color(200, 50, 50));
effaceButton.setForeground(Color.WHITE);
effaceButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
effacerFeedback(rateComboBox, jDateChooser1, typeCheckBox1, typeCheckBox2, typeCheckBox3, typeCheckBox4, typeCheckBox5, securiteComboBox, commentTextArea);
}
});
mainPanel.add(rateLabel);
mainPanel.add(rateComboBox);
mainPanel.add(timeLabel);
mainPanel.add(jDateChooser1);
mainPanel.add(typeLabel);
mainPanel.add(checkboxPanel);
mainPanel.add(securiteLabel);
mainPanel.add(securiteComboBox);
mainPanel.add(commentLabel);
mainPanel.add(commentTextArea);
mainPanel.add(submitButton);
mainPanel.add(effaceButton);
add(mainPanel, BorderLayout.CENTER);
con = Connect.getConnection();
setVisible(true);
}
/*part submit if not logeed IN */
private void submitFeedback(JComboBox<Integer> rateComboBox, JDateChooser jDateChooser1, JCheckBox typeCheckBox1,
JCheckBox typeCheckBox2, JCheckBox typeCheckBox3, JCheckBox typeCheckBox4,
JCheckBox typeCheckBox5, JComboBox<Integer> securiteComboBox, JTextArea commentTextArea) {
if (currentUserId == 0) {
showLoginDialog(() -> {submitFeedbackAfterLogin(rateComboBox, jDateChooser1, typeCheckBox1, typeCheckBox2,
typeCheckBox3, typeCheckBox4, typeCheckBox5, securiteComboBox, commentTextArea);
});
} else {
submitFeedbackAfterLogin(rateComboBox, jDateChooser1, typeCheckBox1, typeCheckBox2,
typeCheckBox3, typeCheckBox4, typeCheckBox5, securiteComboBox, commentTextArea);
}
}
private void showLoginDialog(Runnable afterLoginCallback) {
LoginDialog loginDialog = new LoginDialog(this);
loginDialog.setVisible(true);
if (loginDialog.isConnectPressed()) {
try {
Connection con = Connect.getConnection();
if (con != null) {
String username = loginDialog.getUsername();
String password = loginDialog.getPassword();
String checkIfExistsQuery = "SELECT ID FROM citoyen WHERE username = ? AND password = ?";
try (PreparedStatement checkIfExistsStatement = con.prepareStatement(checkIfExistsQuery)) {
checkIfExistsStatement.setString(1, username);
checkIfExistsStatement.setString(2, password);
try (ResultSet rs = checkIfExistsStatement.executeQuery()) {
if (rs.next()) {
currentUserId = rs.getInt("ID");
System.out.println("Connected User ID: " + currentUserId);
} else {
// User doesn't exist, insert a new record
try (PreparedStatement st = con.prepareStatement("INSERT INTO citoyen(username, password) VALUES (?, ?)")) {
st.setString(1, username);
st.setString(2, password);
int rowsAffected = st.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Citoyen ajouté avec succès à la base de données.");
currentUserId = getLastInsertedId(con);
System.out.println("Connected User ID: " + currentUserId);
} else {
System.out.println("Échec de l'ajout du citoyen à la base de données.");
}
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
} else {
System.out.println("Échec de la connexion à la base de données.");
}
afterLoginCallback.run();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}
/*part submit normal */
private void submitFeedbackAfterLogin(JComboBox<Integer> rateComboBox, JDateChooser jDateChooser1, JCheckBox typeCheckBox1,
JCheckBox typeCheckBox2, JCheckBox typeCheckBox3, JCheckBox typeCheckBox4,
JCheckBox typeCheckBox5, JComboBox<Integer> securiteComboBox, JTextArea commentTextArea) {
Connection con = Connect.getConnection();
try {
if (con != null) {
PreparedStatement st = con.prepareStatement("INSERT INTO avis_citoyen(rate, Time, Type, security, comment,id_citoyen) VALUES (?, ?, ?, ?, ?,?)");
int rate = (int) rateComboBox.getSelectedItem();
java.util.Date utilDate = jDateChooser1.getDate();
java.sql.Date date = new java.sql.Date(utilDate.getTime());
StringBuilder typeBuilder = new StringBuilder();
if (typeCheckBox1.isSelected()) {
typeBuilder.append("SOLO ");
}
if (typeCheckBox2.isSelected()) {
typeBuilder.append("Couple ");
}
if (typeCheckBox3.isSelected()) {
typeBuilder.append("Family ");
}
if (typeCheckBox4.isSelected()) {
typeBuilder.append("Job ");
}
if (typeCheckBox5.isSelected()) {
typeBuilder.append("Friends");
}
String type = typeBuilder.toString().trim();
int securite = (int) securiteComboBox.getSelectedItem();
String comment = commentTextArea.getText();
st.setInt(1, rate);
st.setDate(2, date);
st.setString(3, type);
st.setInt(4, securite);
st.setString(5, comment);
st.setInt(6,currentUserId );
System.out.println("SQL Statement: " + st.toString());
int rowsAffected = st.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Avis ajouté avec succès à la base de données.");
effacerFeedback(securiteComboBox, jDateChooser1, typeCheckBox5, typeCheckBox5, typeCheckBox5, typeCheckBox5, typeCheckBox5, securiteComboBox, commentTextArea);
} else {
System.out.println("Échec de l'ajout d'Avis à la base de données.");
}
} else {
System.out.println("Échec a la connexion au base de données.");
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
/* clear input method on sumbit button or in clear button */
private void effacerFeedback(JComboBox<Integer> rateComboBox, JDateChooser jDateChooser1, JCheckBox typeCheckBox1,
JCheckBox typeCheckBox2, JCheckBox typeCheckBox3, JCheckBox typeCheckBox4,
JCheckBox typeCheckBox5, JComboBox<Integer> securiteComboBox, JTextArea commentTextArea) {
rateComboBox.setSelectedIndex(0);
jDateChooser1.setDate(null);
typeCheckBox1.setSelected(false);
typeCheckBox2.setSelected(false);
typeCheckBox3.setSelected(false);
typeCheckBox4.setSelected(false);
typeCheckBox5.setSelected(false);
securiteComboBox.setSelectedIndex(0);
commentTextArea.setText("");
}
private void showLoginDialog() {
LoginDialog loginDialog = new LoginDialog(this);
loginDialog.setVisible(true);
if (loginDialog.isConnectPressed()) {
try {
Connection con = Connect.getConnection();
if (con != null) {
String username = loginDialog.getUsername();
String password = loginDialog.getPassword();
String checkIfExistsQuery = "SELECT ID FROM citoyen WHERE username = ? AND password = ?";
try (PreparedStatement checkIfExistsStatement = con.prepareStatement(checkIfExistsQuery)) {
checkIfExistsStatement.setString(1, username);
checkIfExistsStatement.setString(2, password);
try (ResultSet rs = checkIfExistsStatement.executeQuery()) {
if (rs.next()) {
currentUserId = rs.getInt("ID");
System.out.println("Connected User ID: " + currentUserId);
} else {
// User doesn't exist, insert a new record
try (PreparedStatement st = con.prepareStatement("INSERT INTO citoyen(username, password) VALUES (?, ?)")) {
st.setString(1, username);
st.setString(2, password);
int rowsAffected = st.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Citoyen ajouté avec succès à la base de données.");
currentUserId = getLastInsertedId(con);
System.out.println("Connected User ID: " + currentUserId);
} else {
System.out.println("Échec de l'ajout du citoyen à la base de données.");
}
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
} else {
System.out.println("Échec de la connexion à la base de données.");
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}
private int getLastInsertedId(Connection con) throws SQLException {
int lastInsertedId = -1;
String query = "SELECT LAST_INSERT_ID() as last_id";
try (PreparedStatement statement = con.prepareStatement(query);
ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
lastInsertedId = resultSet.getInt("last_id");
}
}
return lastInsertedId;
}
}