-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateBookingView.java
195 lines (146 loc) · 5.78 KB
/
CreateBookingView.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
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import Basic.Facility;
import Basic.Member;
import Basic.TimeSlot;
public class CreateBookingView extends JFrame{
private Admin admin;
//private AdminController controller;
private JPanel panel;
private JButton submitButton, cancelButton;
private JLabel label0, label1;
private JTextField input0, input1;
private AdminView controller;
private DataPool dp;
private JTextField chosenDate;
private JTextField noOfPax;
private JLabel note;
JComboBox facilityList;
JComboBox slotList;
public CreateBookingView(AdminView controller, int idToBook){
//
dp = new DataPool();
ArrayList<TimeSlot> listT = dp.getSlotList();
ArrayList<Facility> listF = dp.getFacilityList();
setBounds(100, 100, 450, 300);
this.controller=controller;
panel = new JPanel();
panel.setBorder(new EmptyBorder(5, 5, 5, 5));
panel.setLayout(null);
//panel.setLayout(null);
JLabel lblDateOfAppointment = new JLabel("Date of Appointment");
lblDateOfAppointment.setBounds(6, 31, 142, 16);
panel.add(lblDateOfAppointment);
chosenDate = new JTextField();
chosenDate.setBounds(193, 26, 151, 26);
panel.add(chosenDate);
chosenDate.setColumns(10);
JLabel lblFacility = new JLabel("Facility");
lblFacility.setBounds(6, 56, 61, 16);
panel.add(lblFacility);
facilityList = new JComboBox();
int selectedIndex = 0;
for(Facility f: listF){
facilityList.addItem(f);
}
for(Facility f: listF){
if (idToBook != f.getId()) {
selectedIndex++;
break;
}
}
if (idToBook != 0){
facilityList.setSelectedIndex(selectedIndex);
}
facilityList.setBounds(193, 52, 151, 27);
facilityList.addActionListener (new RefreshPriceOnDemand ());
panel.add(facilityList);
JLabel lblSlot = new JLabel("Slot");
lblSlot.setBounds(6, 84, 61, 16);
panel.add(lblSlot);
JLabel lblNumberOfPax = new JLabel("Number of Pax");
lblNumberOfPax.setBounds(6, 124, 142, 16);
panel.add(lblNumberOfPax);
noOfPax = new JTextField();
noOfPax.setBounds(193, 119, 151, 26);
panel.add(noOfPax);
noOfPax.setColumns(10);
JButton btnSubmit = new JButton("Submit");
btnSubmit.setBounds(193, 157, 117, 29);
btnSubmit.addActionListener(new SubmitButtonListener());
panel.add(btnSubmit);
slotList = new JComboBox();
for(TimeSlot t: listT){
slotList.addItem(t);
}
slotList.setBounds(193, 80, 151, 27);
slotList.addActionListener (new RefreshPriceOnDemand ());
panel.add(slotList);
//panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createTitledBorder("Create new booking"));
//panel.setLayout(new GridLayout(3,2));
panel.setBackground(Color.white);
JButton btnCancel = new JButton("Cancel");
btnCancel.setBounds(81, 158, 100, 26);
btnCancel.addActionListener(new ButtonListener());
panel.add(btnCancel);
getContentPane().add(panel);
note = new JLabel("Price will be different during peak hours");
note.setBounds(10, 210, 208, 14);
panel.add(note);
}
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
controller.mainMenu();
}
}
public class RefreshPriceOnDemand implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
Facility selectedF = (Facility) facilityList.getSelectedItem();
TimeSlot selectedT = (TimeSlot) slotList.getSelectedItem();
String date = chosenDate.getText();
String isPeak= "";
if (getRemainingCapacityAtSlot(date, selectedT.getID(), selectedF.getId()) >= selectedF.getCapacity()) isPeak = "- (Peak Hours)";
note.setText("Price per table/room is:"+ calculatePriceBasedOnDemand(selectedF, selectedT, date)+ isPeak);
}
}
private class SubmitButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
// try {
//runAddTransactionSQL(int member, int facility, int timeSlot, int paxCount)
Facility selectedF = (Facility) facilityList.getSelectedItem();
TimeSlot selectedT = (TimeSlot) slotList.getSelectedItem();
String date = chosenDate.getText();
int unitprice = calculatePriceBasedOnDemand(selectedF, selectedT, date);
//Check if booking pax count is greater than capacity left.
if (Integer.parseInt( noOfPax.getText()) > (selectedF.getCapacity() - getRemainingCapacityAtSlot(date, selectedT.getID(), selectedF.getId()))){
JOptionPane.showMessageDialog(panel, "Please enter pax count from 1 to "+(selectedF.getCapacity() - getRemainingCapacityAtSlot(date, selectedT.getID(), selectedF.getId())),"Information", JOptionPane.PLAIN_MESSAGE);
} else{
int price = Integer.parseInt( noOfPax.getText()) * unitprice ;
controller.createBooking(chosenDate.getText(), selectedF.getId(), selectedT.getID(), Integer.parseInt(noOfPax.getText()), price );
JOptionPane.showMessageDialog(panel, "Transaction recorded","Information", JOptionPane.PLAIN_MESSAGE);
}
// } catch (CreateMemberException e1) {
// JOptionPane.showMessageDialog(panel, e1.getMessage(),"Information", JOptionPane.PLAIN_MESSAGE);
// }
}
}
public void setFacilityID(int idToBook) {
// TODO Auto-generated method stub
}
public int getRemainingCapacityAtSlot(String date, int timeSlot, int facilityID){
int i = dp.getTotalBookingCountAt(date, timeSlot, facilityID);
return i;
}
public int calculatePriceBasedOnDemand(Facility selectedF, TimeSlot selectedT, String date){
//Peak: Number of total pax count >= half of Facility capacity
if (getRemainingCapacityAtSlot(date, selectedT.getID(), selectedF.getId()) >= selectedF.getCapacity()) return selectedF.getPeakPrice();
else
return selectedF.getNonPeakPrice();
}
}