I am trying to calculate sub-total in a separate textField from an event which involves user selecting number of rooms and number of days (based on check in and check out dates).
This textField (sub-total) will effectively multiply (no of days * no of rooms * room price) and will be updated when the user changes duration or number of rooms.
Please note my GUI is based on drag and drop.
private void checkDoubleActionPerformed(java.awt.event.ActionEvent evt) {
// User clicks "Check Availability" button after selecting number of Double Rooms required and duration of stay
String value2 = spinner2.getValue().toString(); //Getting number of room using spinner2
//.set a default current date to Check in. Can be changed by customer
cid_chooser2.getJCalendar().setMinSelectableDate(new Date());
cid_chooser2.setMinSelectableDate(new Date());
Date d1 = null; //initial value of check in date
Date d2 = null; // initial value of check out date
try {
d1 = cid_chooser2.getDate();
d2 = cod_chooser2.getDate();
long duration = d2.getTime() - d1.getTime(); //calculationg duration in days
long days = TimeUnit.MILLISECONDS.toDays(duration);
if (days > 0) {
JOptionPane pane = new JOptionPane("You selected " + value2 + " Double Rooms for: " + days,JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION);
JDialog dialog = pane.createDialog(null, "Customer Notification");
dialog.setSize(new Dimension(400, 200));
dialog.show();
}
else {JOptionPane.showMessageDialog(null, "Check out Date needs to be after Check in Date ");
}
}
catch (NullPointerException ex1) {
if (d1 == null && d2 == null) {
JOptionPane.showMessageDialog(null, "Please enter missing check in AND check out dates.");
}
else if (d2 == null) {
JOptionPane.showMessageDialog(null, "Please enter missing check out date."
+ "\nyour check out date should be at least a day after your check in date");
}
else if (d1 == null) {
JOptionPane.showMessageDialog(null, "Please enter missing check in date."
+ "\nyour check in date should be at least today");
}
}
}
////////////// separate JTextField to calculate sub-total////////////
private void subTotal2ActionPerformed(java.awt.event.ActionEvent evt) {
// sub-Total based on number of Double Rooms selected * duration (days) * unit price
}