目前,我想在预订添加到数据库后更新我的房间可用性,但我现在面临的问题是,我不知道如何在预订后更新房间数量。
我需要的解决方案是,当我输入房间数量并添加时,房间数据库将减去房间数量。
public boolean updateRoomQuantity(String roomID, int amountOfRoomLeft){
String sql = String.format("update room set roomAvailability =%d where roomID = '%s'", amountOfRoomLeft, roomID);
try {
stmt = conn.createStatement();
stmt.executeUpdate(sql);
return true;
} catch (SQLException ex) {
Logger.getLogger(RoomDA.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
return false;
}
}
public boolean addRecord(String booID, String booDate, String booTime, String roomID, double roomPrice, int duration, String memberID,
String receptionistID, String checkinStatus, double totalRoomPrice, double totalPrice) {
String sql = String.format("insert into booking values('%s','%s','%s','%s',%.2f,%d,'%s','%s','%s',%.2f,%.2f)", booID, booDate, booTime, roomID, roomPrice, duration, memberID, receptionistID, checkinStatus, totalRoomPrice, totalPrice);
try {
stmt = conn.createStatement();
stmt.executeUpdate(sql);
return true;
} catch (SQLException ex) {
Logger.getLogger(BookingDA.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
return false;
}
}
private void addBooking() {
double total = 0.0;
int roomQuantity= Integer.parseInt(jtfRoomQuantity.getText().trim());
double price = Double.parseDouble(jtfRoomPrice.getText().trim());
total = (roomQuantity* price);
jtfTotalPrice.setText(String.valueOf(total));
jtfTotalRoomPrice.setText(String.valueOf(total));
if (!emptyFields()) {
if (jcbAutoFillDate.isSelected()) {
jbtFillInDate.doClick();
}
String booID = jtfBookingID.getText();
String booDate = jtfBookingDate.getText();
String booTime = jtfBookingTime.getText();
String roomID = null;
Room room = roomDA.getRecordByName(jtfAutoCompleteRoom.getText());
double roomPrice = Double.parseDouble(jtfRoomPrice.getText());
String memberID = null;
Member member = memberDA.getRecordByName(jtfAutoCompleteMember.getText());
String receptionistID = receptionistDA.getRecordByName(jcbAvailableReceptionist.getSelectedItem().toString()).getReceptionistID();
String checkinStatus = jcbStatus.getSelectedItem().toString();
double totalRoomPrice = Double.parseDouble(jtfTotalRoomPrice.getText());
double totalPrice = Double.parseDouble(jtfTotalPrice.getText());
memberID = member.getMemberID();
roomID = room.getRoomID();
if (bookingDA.addRecord(booID, booDate, booTime, roomID, roomPrice, duration, memberID, receptionistID, checkinStatus, totalRoomPrice, totalPrice)) {
JOptionPane.showMessageDialog(null, "Successfully added");
refreshTableContent();
autoResizeTable();
reset();
}
}
}
答案 0 :(得分:0)
使用roomAvailability = roomAvailability -1
public boolean updateRoomQuantity(String roomID, int amountOfRoomLeft){
String sql = String.format("update room set roomAvailability =roomAvailability-1 where roomID = '%s'", roomID);
try {
stmt = conn.createStatement();
stmt.executeUpdate(sql);
return true;
} catch (SQLException ex) {
Logger.getLogger(RoomDA.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
return false;
}
}
在添加新记录之前检查可用性是否小于0.如果greter大于零则分配一个房间。否则分配另一个房间。
答案 1 :(得分:0)
在您的预订DA中,您正在插入新的预订,因此您还有更新剩余房间的房间DA。
roomDA.updateRoomQuantity(roomId, amountOfRoomLeft);
roomId存在于您的预订中,唯一剩下的未知变量是amountOfRoomLeft。因此,您应该编写一个获取amounttOfRoomLeft
的新方法类似的东西:
public int getRoomQuantity(String roomID){
String sql = String.format("select roomAvailability from room where roomID = '%s'", roomID);
try {
stmt = conn.createStatement();
ResultSet resultset = stmt.execute(sql);
resultset.next();
return resultset.getInt(1);
} catch (SQLException ex) {
Logger.getLogger(RoomDA.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
你也应该自己做作业:))