-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHostel.java
301 lines (229 loc) · 12.1 KB
/
Hostel.java
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
package rsa2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Hostel extends JFrame implements ActionListener {
JLabel l;
JButton confirmButton;
JPanel contentpane;
public Hostel() {
// Create a new JFrame window
JFrame frame = new JFrame("Gender Selection");
// Set the size of the window
frame.setSize(550, 375);
// Set the window to not be resizable
frame.setResizable(false);
// Center the window on the screen
frame.setLocationRelativeTo(null);
// Create a newJPanel to hold the radio buttons, label and button
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // Use BoxLayout with vertical alignment
// Create the label for the gender selection
JLabel genderLabel = new JLabel(" Gender: ");
genderLabel.setFont(new Font("Arial", Font.BOLD, 24)); // Set the font size to 24
genderLabel.setForeground(Color.RED);
// Create the radio buttons and add them to a ButtonGroup
JRadioButton boyButton = new JRadioButton("Boy");
JRadioButton girlButton = new JRadioButton("Girl");
boyButton.setFont(new Font("Arial", Font.BOLD, 60)); // Set the font size to 60
girlButton.setFont(new Font("Arial", Font.BOLD, 60)); // Set the font size to 60
ButtonGroup group = new ButtonGroup();
group.add(boyButton);
group.add(girlButton);
//Create the "Confirm" button
confirmButton = new JButton("Confirm");
confirmButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Open a new window or page here
}
});
confirmButton.setFont(new Font("Arial", Font.BOLD, 24)); // Set the font size to 24
confirmButton.setBackground(new Color(206, 183, 111, 255));
// Add the label, radio buttonsand button to the panel
panel.add(Box.createVerticalGlue()); // Add vertical glue to center the label
panel.add(genderLabel);
panel.add(Box.createVerticalStrut(30)); // Add vertical strut to create a new line
panel.add(boyButton);
panel.add(Box.createVerticalStrut(30)); // Add vertical strut to create a new line
panel.add(girlButton);
panel.add(Box.createVerticalStrut(30)); // Add vertical strut to create a new line
panel.add(confirmButton);
// Add the action listener for the confirm button
confirmButton.addActionListener(this);
// Create a new JPanel with FlowLayout to center the panel
JPanel centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
centerPanel.add(panel);
// Add the centered panel to the window
frame.add(centerPanel);
// Set the window to exit when closed
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Make the window visible
frame.setVisible(true);
}
//-----------------------------------------------------------------
public class MyPage extends JFrame {
private String name; // variable to store the user's name
public MyPage() {
// Set the size of the window
setSize(474, 316);
// Set the window to exit when closed
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create the name label and text field
JLabel nameLabel = new JLabel("Name:");
nameLabel.setFont(new Font("Arial", Font.BOLD, 25));
JTextField nameField = new JTextField(40);
// Create the ID label and text field
JLabel idLabel = new JLabel("ID:");
idLabel.setFont(new Font("Arial", Font.BOLD, 25));
JTextField idField = new JTextField(40);
// Create the exit button
JButton exitButton = new JButton("Exit");
exitButton.setBackground(new Color(206, 183, 111, 255));
exitButton.setFont(new Font("Helvetica", Font.BOLD, 17));
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Exit the application
System.exit(0);
}
});
// Create the next button
JButton nextButton = new JButton("Next");
nextButton.setBackground(new Color(206, 183, 111, 255));
nextButton.setFont(new Font("Helvetica", Font.BOLD, 17));
nextButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Save the name entered by the user
name = nameField.getText();
// Create a new JFrame for the next page
JFrame nextFrame = new JFrame("Hostel ");
// Set the size of the window
nextFrame.setSize(800, 600);
// Set the window to exit when closed
nextFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a label to display the user's name
JLabel nameLabel = new JLabel("Hello, " + name + "!");
nameLabel.setForeground(Color.RED);
nameLabel.setFont(new Font("tahoma", Font.BOLD, 20));
// Add the name label to the new frame
nextFrame.add(nameLabel, BorderLayout.NORTH);
// Create the data for the table
Object[][] data = {
{"101", "1500$", "Available"},
{"102", "1500$", "Available"},
{"103", "3000$", "Available"},
{"104", "1500$", "Available"},
{"105", "2100$", "Available"},
{"106", "2100$", "Available"},
{"107", "1500$", "Available"},
{"108", "2100$", "Available"},
{"109", "1500$", "Available"},
{"110", "3000$", "Available"},
{"111", "3000$", "Available"},
{"112", "3000$", "Available"}
};
// Create the column names for the table
Object[] columnNames = {"Room Number", "Monthly Price", "Availability"};
// Create the JTable with the data and column names
JTable table = new JTable(data, columnNames);
// Add the table to a JScrollPane
JScrollPane scrollPane = new JScrollPane(table);
// Set the preferred size of the JScrollPane
scrollPane.setPreferredSize(new Dimension(750, 500));
// Create a label for the "Number of room" text
JLabel numberOfRoomsLabel = new JLabel("Room Number:");
// Create a text field for the user to enter the number of rooms
JTextField numberOfRoomsField = new JTextField(10);
// Create a button for the user to click to book the rooms
JButton bookButton = new JButton("Book");
bookButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Get the room number entered by the user
String roomNumberStr = numberOfRoomsField.getText();
// Loop through the rows of the table to find the selected room
for (int i = 0; i < data.length; i++) {
if (data[i][0].equals(roomNumberStr)) {
// Update the availability status of the selected room in the table
data[i][2] = "Not Available";
break;
}
}
// Update the table model to reflect the changes
table.setModel(new javax.swing.table.DefaultTableModel(data, columnNames));
try {
int roomNumber = Integer.parseInt(roomNumberStr);
if (roomNumber >= 101 && roomNumber <= 112) {
JOptionPane.showMessageDialog(null, "Room " + roomNumber + " has been booked successfully!");
} else {
JOptionPane.showMessageDialog(null, "Room " + roomNumber + " doesn't exist!");
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter a valid seat number!");
}
// Display a message showing the room has been booked successfully with the room number
// TODO: Handle booking logic here
}
});
// Create a new panel for the "Number of room" components
JPanel bookingPanel = new JPanel();
// Add the "Number of room" label to the panel
bookingPanel.add(numberOfRoomsLabel);
// Add the text field to the panel
bookingPanel.add(numberOfRoomsField);
// Add the book button to the panel
bookingPanel.add(bookButton);
// Create a new panel to hold all the components
JPanel mainPanel = new JPanel(new BorderLayout());
// Add the JScrollPane to the center of the main panel
mainPanel.add(scrollPane, BorderLayout.CENTER);
// Add the booking panel to the south of the main panel
mainPanel.add(bookingPanel, BorderLayout.SOUTH);
nextFrame.add(mainPanel);
// Dispose the current frame after creating the new frame
dispose();
// Set the nextFrame to be visible
nextFrame.setVisible(true);
}
});
// Create a new JPanel for the components
JPanel panel = new JPanel();
// Add the ID label and text field to the panel
panel.add(nameLabel);
panel.add(nameField);
panel.add(idLabel);
panel.add(idField);
// Add some spacing between the components
panel.add(Box.createHorizontalStrut(10));
// Add the exit button to the panel
panel.add(exitButton);
// Add some spacing between the components
panel.add(Box.createHorizontalStrut(10));
// Add the next button to the panel
panel.add(nextButton);
// Add the panel to the frame
add(panel);
// Set the frame to be visible
setVisible(true);
}
// Getter method to retrieve the saved name
public String getName() {
return name;
}
}
@Override
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == confirmButton) {
// Hide the current window
JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(confirmButton);
frame.setVisible(false);
new MyPage();
}
}
//-------------------------------------------------------------------------------
public static void main(String[] args) {
new Hostel().setVisible(true);
}
}