-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoreCreditPanel.java
More file actions
260 lines (206 loc) · 9.64 KB
/
StoreCreditPanel.java
File metadata and controls
260 lines (206 loc) · 9.64 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
public class StoreCreditPanel extends JPanel {
private int width = 100;
private int height = 30;
private JTextField usrInput;
private String username;
private MovieTheatreApp app;
private Color Red = new Color(139, 0, 0);
private Color Yellow = new Color(255, 248, 191);
private Color Orange = new Color(244, 138, 104);
private final Color pastelGreen = new Color(152, 251, 152); // Light pastel green
private JPanel creditPanel; // Right side panel
private TicketController ticketC;
private String email;
private ArrayList<StoreCredit> credits;
private JButton useButton;
private JButton currentSelectButton;
private StoreCredit selectedCredit;
private JButton enterButton;
private JPanel sidePanel;
private JPanel usePanel;
private JButton submitButton;
public StoreCreditPanel(MovieTheatreApp app, UserDatabaseManager userDBM, TicketController ticketC) {
this.app = app;
this.ticketC = ticketC;
JLabel usrLabel = new JLabel("Email:");
usrLabel.setForeground(Red);
usrInput = new JTextField(15);
usrInput.setPreferredSize(new Dimension(width, height));
submitButton = new JButton("Submit");
submitButton.setForeground(Red);
submitButton.setEnabled(false);
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(validateCredit(selectedCredit) && selectedCredit != null) { // idk man:(((
String message = "Sucessfully purchased ticket with selected store credit. Remaining amount for selected store credit: "
+ (selectedCredit.getAmount() - 12.50);
JOptionPane.showMessageDialog(app, message);
ticketC.changeCredit(selectedCredit, (selectedCredit.getAmount() - 12.5));
displayCredits();
ticketC.changeSeatAvailability(app.getSelectedSeat(), false);
// NEED TO UPDATE STORE CREDIT IN DB
}
else {
JOptionPane.showMessageDialog(app, "Invalid payment. Ticket costs $12.50 in store credit");
}
// check if enough credit to but the ticket 12.50
// display JOPtion panel that it successfully purchased
// subtract that credit from their credit !!!!! and put back into db
}
});
enterButton = new JButton("Enter");
enterButton.setForeground(Red);
enterButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Toggle the visibility of the creditPanel after submitting email
sidePanel.setVisible(true); // Show the right-side panel
// Handle the submitted email (you may want to add more logic here)
email = usrInput.getText();
displayCredits();
System.out.println("Email entered: " + email);
}
});
JButton backButton = new JButton("Back");
backButton.setForeground(Red);
backButton.addActionListener(e -> {
System.out.println("Going back to guest panel");
app.switchToPayment();
});
setLayout(new BorderLayout());
// Header panel with title
JPanel headerPanel = new JPanel();
JLabel title = new JLabel("Enter email associated with store credit");
title.setForeground(Red);
headerPanel.add(title);
headerPanel.setBackground(Yellow);
this.add(headerPanel, BorderLayout.NORTH);
// Submit and back buttons at the bottom
JPanel submitPanel = new JPanel();
submitPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
submitPanel.add(backButton);
submitPanel.add(submitButton);
this.add(submitPanel, BorderLayout.PAGE_END);
submitPanel.setBackground(Orange);
// Client info panel (email field)
JPanel clientPanel = new JPanel();
clientPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
clientPanel.setBackground(Yellow);
int row = 0;
addComponent(clientPanel, usrLabel, 0, row, gbc); // FOR THE EMAILLLL
addComponent(clientPanel, usrInput, 1, row, gbc);
addComponent(clientPanel, enterButton, 2, row, gbc);
this.add(clientPanel, BorderLayout.CENTER);
// useButton = new JButton("Use");
// useButton.setForeground(Red);
// useButton.setEnabled(false);
//
// usePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
// usePanel.setBackground(Color.WHITE);
// usePanel.add(useButton);
creditPanel = new JPanel();
creditPanel.setLayout(new GridLayout(6, 2, 5, 5));
creditPanel.setPreferredSize(new Dimension(400, this.getHeight())); // Set fixed size for the panel
creditPanel.setBackground(Color.WHITE);
creditPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
sidePanel = new JPanel(new BorderLayout());
sidePanel.add(creditPanel, BorderLayout.CENTER);
//sidePanel.add(usePanel, BorderLayout.PAGE_END);
sidePanel.setBackground(Color.WHITE);
// useButton = new JButton("Use");
// useButton.setForeground(Red);
// creditPanel.add(useButton, BorderLayout.PAGE_END);
// Initially hide the creditPanel
sidePanel.setVisible(false);
// Add creditPanel to the right of the main panel
this.add(sidePanel, BorderLayout.EAST);
}
public void displayCredits() {
creditPanel.removeAll(); // Clear existing components
// Retrieve credits for the current registered user
this.credits = ticketC.getCreditsFromEmail(email);
// Update the layout of the creditPanel based on the number of credits
creditPanel.setLayout(new GridLayout(credits.size() + 1, 1, 5, 5)); // Stack components vertically
if (credits == null || credits.isEmpty()) {
//useButton.setEnabled(false);
JLabel noCreditsLabel = new JLabel("No available store credit.");
noCreditsLabel.setForeground(Red);
noCreditsLabel.setHorizontalAlignment(SwingConstants.CENTER);
noCreditsLabel.setVerticalAlignment(SwingConstants.CENTER);
creditPanel.add(noCreditsLabel);
} else {
// Loop through saved credits and create a button for each
for (StoreCredit credit : credits) {
JLabel cardNumLabel = new JLabel(credit.toString());
cardNumLabel.setForeground(Red);
JButton selectButton = new JButton("Select");
selectButton.setForeground(Red);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonPanel.setBackground(Color.WHITE);
buttonPanel.add(cardNumLabel);
buttonPanel.add(selectButton);
buttonPanel.setBackground(Color.WHITE);
// Button action to select a credit
selectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentSelectButton == selectButton) {
updateButtonColor(selectButton, true); // Deselect
currentSelectButton = null;
selectedCredit = null;
submitButton.setEnabled(false); // Disable Use button if no credit selected
} else {
updateButtonColor(selectButton, false); // Select
currentSelectButton = selectButton;
selectedCredit = credit; // Set selected credit
submitButton.setEnabled(true); // Enable Use button
}
}
});
// Add the label and the select button for each credit
creditPanel.add(buttonPanel);
}
}
// Call revalidate and repaint on the creditPanel to refresh its layout
creditPanel.revalidate(); // Ensure layout updates after adding components
creditPanel.repaint(); // Ensure repaint of the panel
}
// Helper method for adding components to clientPanel
private void addComponent(JPanel panel, Component component, int gridx, int gridy, GridBagConstraints gbc) {
gbc.gridx = gridx;
gbc.gridy = gridy;
panel.add(component, gbc);
}
public void updateButtonColor(JButton button, boolean unclicked) {
if (unclicked) {
button.setBackground(UIManager.getColor("Button.background"));
} else {
button.setBackground(pastelGreen);
}
}
public void autofill() {
if ((app.getCurrentUser() == 1) && (app.getRU() != null)) {
usrInput.setText(app.getRU().getEmail());
}
}
private boolean validateCredit(StoreCredit credit) {
boolean valid = true;
if (credit.getAmount() < 12.50) {
valid = false;
}
return valid;
}
}