[英]How to Update Room Quantity?
目前,我想在預訂添加到數據庫后更新我的房間可用性,但我現在面臨的問題是,我不知道如何在預訂后更新房間數量。
我需要的解決方案是,當我輸入房間數量並添加時,房間數據庫將減去房間數量。
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();
}
}
}
使用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大於零則分配一個房間。 否則分配另一個房間。
在您的預訂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); } }
你也應該自己做你的作業:))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.