?? housebookanduse.java
字號:
if (e.getActionCommand().equals("修改記錄")) {
buttonsSet();
autoResearch.setEnabled(false);
id = common();
if (id == null) {
return;
}
modify.setText("確認修改");
return;
}
if (e.getActionCommand().equals("確認修改")) {
int hGradeIndex = hGradeBox.getSelectedIndex();
if (hGradeIndex == -1) {
JOptionPane.showMessageDialog(null, "客房級別不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String hGrade = (String) hGradeBox.getSelectedItem();
String hNum = hNumTF.getText();
int numOfH;
try {
numOfH = Integer.valueOf(hNum);
if (numOfH <= 0) {
JOptionPane.showMessageDialog(null, "訂房數量必須為正整數!",
"提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "訂房數量必須為正整數!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String checkIn = dateGet.getDate(checkInTF.getText());
if (checkIn == null) {
JOptionPane.showMessageDialog(null,
"入住日期輸入有誤,必須為如2007-09-08的格式!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String checkOut = dateGet.getDate(checkOutTF.getText());
if (checkOut == null) {
JOptionPane.showMessageDialog(null,
"退房日期輸入有誤,必須為如2007-09-08的格式!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
int index = stateBox.getSelectedIndex();
if (index == -1) {
JOptionPane.showMessageDialog(null, "狀態不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String state = (String) stateBox.getSelectedItem();
String update = null;
DBConnection con = new DBConnection();
if (numOfH == num && hGrade.equals(hG)) {
update = "update HUseBook set dateIn='" + checkIn
+ "',dateOut='" + checkOut + "',consume=0,state='"
+ state + "' where ID=" + Integer.valueOf(id);
con.addSql(update);
try {
con.doDML();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
} else {
String addStr = null;
if (hNo.contains(",")) {
StringTokenizer st = new StringTokenizer(hNo, ",");
while (st.hasMoreTokens()) {
String noGet = st.nextToken();
if (noGet != null) {
addStr = "update House set state='是' where HouseNo='"
+ noGet + "'";
con.addSql(addStr);
}
}
} else {
addStr = "update House set state='是' where HouseNo='"
+ hNo + "'";
con.addSql(addStr);
}
String indicate = research();
if (indicate == null) {
return;
}
String reAutoHNo = autoHNoTF.getText();
update = "update HUseBook set bookNum=" + numOfH + ",hNo='"
+ reAutoHNo + "',dateIn='" + checkIn
+ "',dateOut='" + checkOut + "',consume=0,state='"
+ state + "' where ID=" + Integer.valueOf(id);
con.addSql(update);
if (reAutoHNo.contains(",")) {
StringTokenizer st = new StringTokenizer(reAutoHNo, ",");
while (st.hasMoreTokens()) {
String noGet = st.nextToken();
if (noGet != null) {
addStr = "update House set state='否' where HouseNo='"
+ noGet + "'";
con.addSql(addStr);
}
}
} else {
addStr = "update House set state='否' where HouseNo='"
+ reAutoHNo + "'";
con.addSql(addStr);
}
try {
con.doDML();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
String returnValue = doIt();
if (returnValue == null) {
return;
}
buttonsSet();
if (!autoResearch.isEnabled()) {
autoResearch.setEnabled(true);
}
modify.setText("修改記錄");
}
}
if (e.getSource() == delete) {
if (modify.getText().equals("確認修改")) {
modify.setText("修改記錄");
}
buttonsSet();
if (!autoResearch.isEnabled()) {
autoResearch.setEnabled(true);
}
try {
id = common();
if (id == null) {
return;
}
int confirm = JOptionPane.showConfirmDialog(null, "您真的確認刪除嗎?",
"", JOptionPane.YES_NO_OPTION);
if (confirm == -1 || confirm == JOptionPane.NO_OPTION) {
return;
}
DBConnection con = new DBConnection();
String query = "select hNo from HUseBook where ID=" + id;
CachedRowSet crs = con.getResultSet(query);
String houseNo = null;
if (crs.next()) {
houseNo = crs.getString(1);
}
String delete = "delete from HUseBook where ID=" + id;
con.addSql(delete);
String update = null;
String today = dateGet.getDate(format
.valueConverted(new Date()));
if (!houseNo.contains(",")) {
update = "update House set state='是',beginUseable='"
+ today + "' where HouseNo='" + houseNo + "'";
con.addSql(update);
} else {
StringTokenizer st = new StringTokenizer(houseNo, ",");
while (st.hasMoreTokens()) {
String sHNo = st.nextToken();
if (sHNo != null && !sHNo.equals("")) {
update = "update House set state='是',beginUseable='"
+ today + "' where HouseNo='" + sHNo + "'";
con.addSql(update);
}
}
}
con.doDML();
String returnValue = doIt();
if (returnValue == null) {
return;
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
buttonsSet();// 重新把按鈕置為可用狀態
}
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == cGradeBox) {
if (e.getStateChange() == ItemEvent.SELECTED) {
int index = cGradeBox.getSelectedIndex();
if (index != -1) {
String cGrade = (String) cGradeBox.getSelectedItem();
String query = "select firstPayPercent from CGrade where grade='"
+ cGrade + "'";
CachedRowSet crs = null;
try {
DBConnection con = new DBConnection();
crs = con.getResultSet(query);
if (crs.next()) {
typeTF.setText(String.valueOf(crs.getFloat(1)));
typeTF.setEditable(false);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
}
}
}
private String research() {
try {
int houseGradeIndex = hGradeBox.getSelectedIndex();
if (houseGradeIndex == -1) {
JOptionPane.showMessageDialog(null, "客房級別不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
String hGrade = (String) hGradeBox.getSelectedItem();
int numOfHouse;
try {
numOfHouse = Integer.valueOf(hNumTF.getText());
if (numOfHouse <= 0) {
JOptionPane.showMessageDialog(null, "訂房數量必須為正整數!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "訂房數量必須為正整數!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
DBConnection con = new DBConnection();
String query = "select count(*) from House where hGrade='" + hGrade
+ "' and state='是'";
CachedRowSet crs = con.getResultSet(query);
int count = 0;
while (crs.next()) {
count = crs.getInt(1);
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "目前沒有該級別的可用房間!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
if (count != 0 && count < numOfHouse) {
JOptionPane.showMessageDialog(null, "可供客房數量不夠,目前該級別的可用房間數為:"
+ count + "!", "提示", JOptionPane.INFORMATION_MESSAGE);
return null;
}
crs = null;
StringBuffer sb = new StringBuffer();
query = "select top " + numOfHouse
+ " HouseNo, beginUseable from House where hGrade='"
+ hGrade + "' and state='是' order by beginUseable asc";
crs = con.getResultSet(query);
count = 0;
if (numOfHouse == 1) {
while (crs.next()) {
sb.append(crs.getString(1));
}
} else {
while (crs.next()) {
count++;
if (count == numOfHouse) {
sb.append(crs.getString(1));
} else {
sb.append(crs.getString(1) + ",");
}
}
}
String sbStr = sb.toString();
autoHNoTF.setText(sbStr);
crs = null;
query = "select Sprice from HGrade where grade='" + hGrade + "'";
crs = con.getResultSet(query);
String checkIn = dateGet.getDate(checkInTF.getText());
if (checkIn == null) {
JOptionPane.showMessageDialog(null, "入住日期輸入有誤!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
String checkOut = dateGet.getDate(checkOutTF.getText());
if (checkOut == null) {
JOptionPane.showMessageDialog(null, "退房日期輸入有誤!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
String inAfterAdded = null;
int dayNum = 1;
for (;; dayNum++) {
inAfterAdded = addDay.addDays(checkIn, dayNum);
if (inAfterAdded.equals(checkOut)) {
break;
}
}
float money1 = 0;
while (crs.next()) {
money1 = crs.getFloat(1) * numOfHouse * dayNum;
}
crs = null;
int cGradeIndex = cGradeBox.getSelectedIndex();
if (cGradeIndex == -1) {
JOptionPane.showMessageDialog(null, "客戶級別不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
String type = typeTF.getText().trim();
if (type.equals("")) {
JOptionPane.showMessageDialog(null, "預付款比例不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
float typeValue = Float.valueOf(type);
query = "select standard from CGrade where id="
+ cGrade_ids[cGradeIndex];
crs = con.getResultSet(query);
float money2 = 0;
while (crs.next()) {
money2 = money1 * crs.getFloat(1);
}
float total = money2 * typeValue;
firstPayTF.setText(String.valueOf(total));
firstPayTF.setEditable(false);
nameTF.setEditable(false);
typeTF.setEditable(false);
autoHNoTF.setEditable(false);
checkInTF.setEditable(false);
checkOutTF.setEditable(false);
hNumTF.setEditable(false);
autoResearch.setEnabled(false);
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return "success";
}
private String common() {
String id = (String) JOptionPane.showInputDialog(null,
"請選擇要刪除或修改的記錄ID!", "", JOptionPane.INFORMATION_MESSAGE, null,
hUseAndBookID, hUseAndBookID[0]);
if (id == null) {
return null;
}
try {
DBConnection con = new DBConnection();
String query = "select cName,bookNum,hNo,hGrade,dateIn,dateOut,state,grade,firstPayPercent "
+ "from HUseBook,Client,CGrade where HUseBook.ID="
+ id
+ " and rendererid=Client.id and Client.cGradeID=CGrade.ID ";
CachedRowSet crs = con.getResultSet(query);
if (crs.next()) {
nameTF.setText(crs.getString(1));
num = crs.getInt(2);
hNo = crs.getString(3);
String hgrade = crs.getString(4);
String in = crs.getString(5);
String out = crs.getString(6);
String state = crs.getString(7);
String cGrade = crs.getString(8);
float firstPay = crs.getFloat(9);
checkInTF.setText(in);
checkOutTF.setText(out);
stateBox.setSelectedItem(state);
cGradeBox.setSelectedItem(cGrade);
typeTF.setText(String.valueOf(firstPay));
nameTF.setEditable(false);
hNumTF.setText(String.valueOf(num));
autoHNoTF.setText(hNo);
hGradeBox.setSelectedItem(hgrade);
typeTF.setEditable(false);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return id;
}
private void buttonsSet() {
firstPayTF.setEditable(false);
nameTF.setEditable(true);
typeTF.setEditable(true);
autoHNoTF.setEditable(true);
checkInTF.setEditable(true);
checkOutTF.setEditable(true);
hNumTF.setEditable(true);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -