-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
197 lines (167 loc) · 8.25 KB
/
Copy pathMain.java
File metadata and controls
197 lines (167 loc) · 8.25 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;
import net.sourceforge.jdatepicker.impl.DateComponentFormatter;
public class Main {
private static AppointmentManager manager = new AppointmentManager();
public static void main(String[] args) {
JFrame frame = new JFrame("Manage Your Appointments");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(650, 450);
frame.setLayout(new BorderLayout(15, 15));
// Input section
JPanel inputPanel = new JPanel(new GridLayout(5, 2, 10, 10));
JLabel typeLabel = new JLabel("Type:");
String[] appointmentTypes = {"One-time", "Daily", "Monthly"};
JComboBox<String> typeDropdown = new JComboBox<>(appointmentTypes);
JLabel startLabel = new JLabel("Start Date:");
JDatePickerImpl startPicker = createDatePicker();
JLabel endLabel = new JLabel("End Date:");
JDatePickerImpl endPicker = createDatePicker();
JLabel descLabel = new JLabel("Description:");
JTextField descField = new JTextField();
inputPanel.add(typeLabel);
inputPanel.add(typeDropdown);
inputPanel.add(startLabel);
inputPanel.add(startPicker);
inputPanel.add(endLabel);
inputPanel.add(endPicker);
inputPanel.add(descLabel);
inputPanel.add(descField);
// Buttons
JPanel buttonPanel = new JPanel(new GridLayout(1, 4, 10, 10));
JButton addButton = new JButton("Add");
JButton deleteButton = new JButton("Delete");
JButton checkButton = new JButton("Check");
JButton updateButton = new JButton("Update");
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
buttonPanel.add(checkButton);
buttonPanel.add(updateButton);
// Display
JPanel displayPanel = new JPanel(new BorderLayout());
DefaultListModel<String> listModel = new DefaultListModel<>();
JList<String> appointmentList = new JList<>(listModel);
JScrollPane scrollPane = new JScrollPane(appointmentList);
JLabel statusLabel = new JLabel("", SwingConstants.CENTER);
displayPanel.add(scrollPane, BorderLayout.CENTER);
displayPanel.add(statusLabel, BorderLayout.SOUTH);
frame.add(inputPanel, BorderLayout.NORTH);
frame.add(buttonPanel, BorderLayout.CENTER);
frame.add(displayPanel, BorderLayout.SOUTH);
// Button actions
addButton.addActionListener(e -> {
try {
String type = (String) typeDropdown.getSelectedItem();
LocalDate start = convertToDate(startPicker);
LocalDate end = convertToDate(endPicker);
String description = descField.getText();
Appointment appointment;
if ("One-time".equals(type)) {
appointment = new OnetimeAppointment(start, description);
} else if ("Daily".equals(type)) {
appointment = new DailyAppointment(start, end, description);
} else {
appointment = new MonthlyAppointment(start, end, description);
}
manager.add(appointment);
updateAppointmentList(listModel);
statusLabel.setText("Added successfully.");
} catch (Exception ex) {
statusLabel.setText("Error: " + ex.getMessage());
}
});
deleteButton.addActionListener(e -> {
int selected = appointmentList.getSelectedIndex();
if (selected >= 0) {
Appointment toDelete = manager.getSortedAppointments()
.get(selected);
manager.delete(toDelete);
updateAppointmentList(listModel);
statusLabel.setText("Deleted.");
} else {
statusLabel.setText("Select an appointment to delete.");
}
});
checkButton.addActionListener(e -> {
try {
String input = JOptionPane.showInputDialog("Enter date (yyyy-mm-dd):");
LocalDate date = LocalDate.parse(input);
Appointment[] results = manager.getAppointmentsOn(date, null);
if (results.length > 0) {
statusLabel.setText("Found: " + results[0].getDescription());
} else {
statusLabel.setText("No appointments found.");
}
} catch (Exception ex) {
statusLabel.setText("Invalid date.");
}
});
updateButton.addActionListener(e -> {
int selected = appointmentList.getSelectedIndex();
if (selected >= 0) {
Appointment current = manager.getSortedAppointments()
.get(selected);
String newDescription = (String) JOptionPane.showInputDialog(null, "New description:", "Update", JOptionPane.QUESTION_MESSAGE, null, null, current.getDescription());
// String newDescription = JOptionPane.showInputDialog("New description:");
if (newDescription == null || newDescription.isEmpty()) {
statusLabel.setText("Update canceled.");
return;
}
String newStartInput = (String) JOptionPane.showInputDialog(null, "New start date (yyyy-mm-dd):", "Update", JOptionPane.QUESTION_MESSAGE, null, null, current.getStartDate().toString());
if (newStartInput == null || newStartInput.isEmpty()) {
statusLabel.setText("Update canceled.");
return;
}
LocalDate newStart = newStartInput == null ? current.getStartDate() : LocalDate.parse(newStartInput);
String newEndInput = (String) JOptionPane.showInputDialog(null, "New end date (yyyy-mm-dd):", "Update", JOptionPane.QUESTION_MESSAGE, null, null, current.getEndDate().toString());
if (newEndInput == null || newEndInput.isEmpty()) {
statusLabel.setText("Update canceled.");
return;
}
LocalDate newEnd = newEndInput == null ? current.getEndDate() : LocalDate.parse(newEndInput);
String type = (String) typeDropdown.getSelectedItem();
Appointment updated;
if ("One-time".equals(type)) {
updated = new OnetimeAppointment(newStart, newDescription);
} else if ("Daily".equals(type)) {
updated = new DailyAppointment(newStart, newEnd, newDescription);
} else {
updated = new MonthlyAppointment(newStart, newEnd, newDescription);
}
manager.update(current, updated);
updateAppointmentList(listModel);
statusLabel.setText("Updated.");
} else {
statusLabel.setText("Select an appointment to update.");
}
});
frame.setVisible(true);
}
private static void updateAppointmentList(DefaultListModel<String> listModel) {
listModel.clear();
List<Appointment> sortedAppointments = manager.getSortedAppointments();
for (Appointment appointment : sortedAppointments) {
listModel.addElement(appointment.toString());
}
}
private static JDatePickerImpl createDatePicker() {
UtilDateModel model = new UtilDateModel();
JDatePanelImpl panel = new JDatePanelImpl(model);
return new JDatePickerImpl(panel, new DateComponentFormatter());
}
private static LocalDate convertToDate(JDatePickerImpl picker) {
java.util.Date date = (java.util.Date) picker.getModel().getValue();
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
}
// TODO: handle invalid update input - handle crash
// TODO: sort appointments in order, or description
// TODO: display appointments on a certain date, or display all: create buttons or ....