-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManageBookingsPage.java
More file actions
99 lines (86 loc) · 3.89 KB
/
Copy pathManageBookingsPage.java
File metadata and controls
99 lines (86 loc) · 3.89 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
// package TourGuide;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
public class ManageBookingsPage extends JPanel {
private String guideUsername;
private MainFrame mainFrame;
public ManageBookingsPage(MainFrame mainFrame, String guideUsername) {
this.mainFrame = mainFrame;
this.guideUsername = guideUsername;
setLayout(new BorderLayout());
JTextArea bookingsArea = new JTextArea(20, 40);
bookingsArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(bookingsArea);
// Load bookings for the guide from file and display them
ArrayList<String> bookings = loadBookingsForGuide(guideUsername);
for (String booking : bookings) {
bookingsArea.append(booking + "\n");
}
add(new JLabel("Manage Bookings"), BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
// Button to accept booking
JButton acceptButton = new JButton("Accept Booking");
acceptButton.addActionListener(e -> {
String selectedDate = JOptionPane.showInputDialog("Enter the date of the booking to accept:");
if (selectedDate != null && !selectedDate.isEmpty()) {
manageBooking(selectedDate, "accepted");
}
});
add(acceptButton, BorderLayout.SOUTH);
// Button to reject booking
JButton rejectButton = new JButton("Reject Booking");
rejectButton.addActionListener(e -> {
String selectedDate = JOptionPane.showInputDialog("Enter the date of the booking to reject:");
if (selectedDate != null && !selectedDate.isEmpty()) {
manageBooking(selectedDate, "rejected");
}
});
add(rejectButton, BorderLayout.SOUTH);
}
// Load bookings for a guide from the file
private ArrayList<String> loadBookingsForGuide(String guideUsername) {
ArrayList<String> bookings = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader("Bookings.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
String[] bookingInfo = line.split(",");
if (bookingInfo[0].equals(guideUsername)) {
bookings.add("Date: " + bookingInfo[1] + ", Time: " + bookingInfo[2]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return bookings;
}
// Accept or reject a booking based on the date
private void manageBooking(String date, String action) {
try (BufferedReader reader = new BufferedReader(new FileReader("Bookings.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("Bookings_temp.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
String[] bookingInfo = line.split(",");
if (bookingInfo[0].equals(guideUsername) && bookingInfo[1].equals(date)) {
writer.write(bookingInfo[0] + "," + bookingInfo[1] + "," + bookingInfo[2] + "," + action);
writer.newLine();
} else {
writer.write(line);
writer.newLine();
}
}
// Rename the temp file to replace the original file
File originalFile = new File("Bookings.txt");
File tempFile = new File("Bookings_temp.txt");
if (!originalFile.delete()) {
System.out.println("Could not delete original file.");
}
if (!tempFile.renameTo(originalFile)) {
System.out.println("Could not rename temporary file.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}