?? housebookanduse.java
字號(hào):
package file1;
/*
* 功能描述:酒店客房的預(yù)定與入住登記以及客房分配入口
* Author:黃順武
* Time:---
* Last Modified:2007-12-15
* Modified Content:數(shù)據(jù)庫(kù)連接類(lèi)DBConnection
* Modify Reason:數(shù)據(jù)庫(kù)連接類(lèi)DBConnection 的內(nèi)部結(jié)構(gòu)設(shè)計(jì)得到優(yōu)化
*/
import java.sql.*;
import sun.jdbc.rowset.*;
import java.util.Date;
import java.util.StringTokenizer;
import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class HouseBookAndUse extends JPanel implements ActionListener,
ItemListener {
private JLabel cGrade = new JLabel("客戶級(jí)別:", SwingConstants.CENTER);
private JLabel name = new JLabel("客戶名字:", SwingConstants.CENTER);
private JLabel hGrade = new JLabel("客房級(jí)別:", SwingConstants.CENTER);
private JLabel bookNum = new JLabel("訂房數(shù)量:", SwingConstants.CENTER);
private JLabel checkIn = new JLabel("入住日期:", SwingConstants.CENTER);
private JLabel checkOut = new JLabel("退房日期:", SwingConstants.CENTER);
private JLabel type = new JLabel("預(yù)付比例:", SwingConstants.CENTER);
private JLabel state = new JLabel("記錄狀態(tài):", SwingConstants.CENTER);
private JButton autoResearch = new JButton("自動(dòng)分房");
private JLabel firstPay = new JLabel("預(yù)付金額:", SwingConstants.CENTER);
private JComboBox cGradeBox = new JComboBox();
private JTextField nameTF = new JTextField(12);
private JComboBox hGradeBox = new JComboBox();
private JTextField hNumTF = new JTextField(10);
private JTextField checkInTF = new JTextField(10);
private JTextField checkOutTF = new JTextField(10);
private JTextField typeTF = new JTextField(10);
private JComboBox stateBox = new JComboBox(new String[] { "是", "否" });
private JTextField autoHNoTF = new JTextField(10);
private JTextField firstPayTF = new JTextField(10);
private JPanel p1 = new JPanel();
private JTable recordTable;
private JScrollPane recScrollPane;
private String[] title = { "ID", "客戶名字", "客戶級(jí)別", "數(shù)量", "客房號(hào)", "入住日期",
"退房日期", "酒水消費(fèi)金額", "狀態(tài)" };
private int titleNum = 0;
private String tableData[][] = null;
private JPanel p3 = new JPanel();
private JButton add = new JButton("增加記錄");
private JButton modify = new JButton("修改記錄");
private JButton delete = new JButton("刪除記錄");
private float[] hPrice;
private String[] hUseAndBookID;// 記錄訂房記錄的ID
private String[] cGrade_ids = null;// 所有客戶級(jí)別的id
private String id = null;
private GetDate dateGet;
private AddSomeDays addDay;
private Formatter format = new Formatter();
private String hG = null;// 修改記錄時(shí)記錄修改前的客戶級(jí)別
private int num;// 修改記錄時(shí)記錄修改前的訂房數(shù)量
private String hNo = null;// 修改記錄時(shí)的客房號(hào)
public HouseBookAndUse() {
autoResearch.setBorder(null);
autoResearch.setBackground(Color.LIGHT_GRAY);
nameTF.setBorder(null);
hNumTF.setBorder(null);
checkInTF.setBorder(null);
checkOutTF.setBorder(null);
typeTF.setBorder(null);
autoHNoTF.setBorder(null);
firstPayTF.setBorder(null);
String index = getTitles();
if (index.equals("failture")) {
return;
}
titleNum = title.length;
dateGet = new GetDate();
addDay = new AddSomeDays();
p1.setLayout(new GridLayout(2, 10, 2, 10));
p1.add(cGrade);
p1.add(cGradeBox);
p1.add(name);
p1.add(nameTF);
p1.add(hGrade);
p1.add(hGradeBox);
p1.add(bookNum);
p1.add(hNumTF);
p1.add(autoResearch);
p1.add(autoHNoTF);
p1.add(checkIn);
p1.add(checkInTF);
p1.add(checkOut);
p1.add(checkOutTF);
p1.add(type);
p1.add(typeTF);
p1.add(state);
p1.add(stateBox);
p1.add(firstPay);
p1.add(firstPayTF);
p3.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
add.setBorder(null);
add.setBackground(Color.LIGHT_GRAY);
modify.setBorder(null);
modify.setBackground(Color.LIGHT_GRAY);
delete.setBorder(null);
delete.setBackground(Color.LIGHT_GRAY);
p3.add(add);
p3.add(modify);
p3.add(delete);
this.setLayout(new BorderLayout(0, 5));
String returnValue = doIt();
if (returnValue == null) {
return;
}
cGradeBox.addItemListener(this);
autoResearch.addActionListener(this);
modify.addActionListener(this);
add.addActionListener(this);
delete.addActionListener(this);
}
private String getTitles() {
String sql = "select id,grade from CGrade";
CachedRowSet crs = null;
int count = 0;
try {
DBConnection con = new DBConnection();
crs = con.getResultSet(sql);
while (crs.next()) {
count++;
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "數(shù)據(jù)庫(kù)中沒(méi)客戶級(jí)別記錄,請(qǐng)先添加級(jí)別!",
"提示", JOptionPane.INFORMATION_MESSAGE);
return "failture";
}
crs.beforeFirst();
cGrade_ids = new String[count];
count = 0;
while (crs.next()) {
cGrade_ids[count++] = String.valueOf(crs.getInt(1));
cGradeBox.addItem(crs.getString(2));
}
cGradeBox.setSelectedIndex(-1);
crs = null;
sql = "select* from HGrade";
crs = con.getResultSet(sql);
count = 0;
while (crs.next()) {
count++;
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "數(shù)據(jù)庫(kù)中沒(méi)客房級(jí)別記錄,請(qǐng)先添加客房級(jí)別!",
"提示", JOptionPane.INFORMATION_MESSAGE);
return "failture";
}
hPrice = new float[count];
crs.beforeFirst();
count = 0;
while (crs.next()) {
hGradeBox.addItem(crs.getString(1));
hPrice[count] = crs.getFloat(2);
count++;
}
hGradeBox.setSelectedIndex(-1);
} catch (SQLException sqle) {
sqle.printStackTrace();
return "failture";
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return "failture";
}
return "success";
}
private String doIt() {
try {
String sql = "select HUseBook.ID,cName,grade,bookNum,hNo,dateIn,dateOut,consume,state"
+ " from HUseBook,Client,CGrade where rendererID=Client.ID and cGradeID=CGrade.ID";
DBConnection con = new DBConnection();
CachedRowSet crs = con.getResultSet(sql);
int count = 0;
while (crs.next()) {
count++;
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "數(shù)據(jù)庫(kù)中沒(méi)有入住記錄!", "提示",
JOptionPane.INFORMATION_MESSAGE);
modify.setEnabled(false);
delete.setEnabled(false);
} else {
modify.setEnabled(true);
delete.setEnabled(true);
}
tableData = new String[count][titleNum];
hUseAndBookID = new String[count];
crs.beforeFirst();
count = 0;
int row = 0;
while (crs.next()) {
hUseAndBookID[count] = String.valueOf(crs.getInt(1));
tableData[row][0] = String.valueOf(crs.getInt(1));
tableData[row][1] = crs.getString(2);
tableData[row][2] = crs.getString(3);
tableData[row][3] = String.valueOf(crs.getInt(4));
tableData[row][4] = crs.getString(5);
tableData[row][5] = crs.getString(6);
tableData[row][6] = crs.getString(7);
tableData[row][7] = String.valueOf(crs.getFloat(8));
tableData[row][8] = crs.getString(9);
row++;
count++;
}
recordTable = new JTable(tableData, title);
recScrollPane = new JScrollPane(recordTable);
this.add(recScrollPane, BorderLayout.CENTER);
this.add(p1, BorderLayout.NORTH);
this.add(recScrollPane, BorderLayout.CENTER);
this.add(p3, BorderLayout.SOUTH);
this.validate();
} catch (SQLException sqle) {
sqle.printStackTrace();
return null;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return null;
}
return "success";
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == autoResearch) {
String index = research();
if (index == null) {
return;
}
}
if (e.getSource() == add) {
if (modify.getText().equals("確認(rèn)修改")) {
modify.setText("修改記錄");
}
int cGradeIndex = cGradeBox.getSelectedIndex();
if (cGradeIndex == -1) {
JOptionPane.showMessageDialog(null, "客戶級(jí)別不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String name = nameTF.getText();
if (name.equals("")) {
JOptionPane.showMessageDialog(null, "名字不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
int houseGradeIndex = hGradeBox.getSelectedIndex();
if (houseGradeIndex == -1) {
JOptionPane.showMessageDialog(null, "客房級(jí)別不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
int numOfHouse;
try {
numOfHouse = Integer.valueOf(hNumTF.getText());
if (numOfHouse <= 0) {
JOptionPane.showMessageDialog(null, "訂房數(shù)量必須為正整數(shù)!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "訂房數(shù)量必須為正整數(shù)!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String type = typeTF.getText();
if (type.equals("")) {
JOptionPane.showMessageDialog(null, "預(yù)付款比例不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String checkIn = dateGet.getDate(checkInTF.getText());
String checkOut = dateGet.getDate(checkOutTF.getText());
int stateIndex = stateBox.getSelectedIndex();
if (stateIndex == -1 || stateIndex == 1) {
JOptionPane.showMessageDialog(null, "狀態(tài)不能為空且必須選擇'是'!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String state = (String) stateBox.getSelectedItem();
String bookedHNo = autoHNoTF.getText();
if (bookedHNo.equals("")) {
JOptionPane.showMessageDialog(null, "客房號(hào)不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if (autoResearch.isEnabled()) {
JOptionPane.showMessageDialog(null, "請(qǐng)先點(diǎn)擊自動(dòng)分房按鈕分配客房!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
try {
DBConnection con = new DBConnection();
String queryStr = null;
String grade = null;
CachedRowSet crs = null;
if (bookedHNo.indexOf(",") == -1) {
queryStr = "select hGrade from House where HouseNo='"
+ bookedHNo + "'";
crs = con.getResultSet(queryStr);
if (crs.next()) {
grade = crs.getString(1);
}
} else {
StringTokenizer st = new StringTokenizer(bookedHNo, ",");
String sHNO = null;
int base = 0;
while (st.hasMoreTokens()) {
sHNO = st.nextToken();
if (base == 0) {
break;
}
}
queryStr = "select hGrade from House where HouseNo='"
+ sHNO + "'";
crs = con.getResultSet(queryStr);
if (crs.next()) {
grade = crs.getString(1);
}
}
crs = null;
String query = "select* from Client where cName='" + name + "'";
crs = con.getResultSet(query);
if (!crs.next()) {
int confirm = JOptionPane.showConfirmDialog(null,
"該客戶是新客戶嗎?", "", JOptionPane.YES_NO_OPTION);
if (confirm == -1) {
return;
}
if (confirm == JOptionPane.YES_OPTION) {
String insert = "insert into Client values("
+ cGrade_ids[cGradeIndex] + ",'" + name
+ "','')";
DBConnection conn = new DBConnection();
conn.addSql(insert);
conn.doDML();
}
}
crs = null;
crs = con.getResultSet("select* from Client");
int renderid = -1;
while (crs.next()) {
if (crs.isLast()) {
renderid = crs.getInt(1);
}
}
String insert = "insert into HUseBook values(" + renderid + ","
+ numOfHouse + ",'" + bookedHNo + "','" + grade + "','"
+ checkIn + "','" + checkOut + "',0,'" + state + "')";
con.addSql(insert);
if (!bookedHNo.contains(",")) {
String update = "update House set state='否',beginUseable='"
+ addDay.addDays(checkOut, 1) + "' where HouseNo='"
+ bookedHNo + "'";
con.addSql(update);
} else {
StringTokenizer st = new StringTokenizer(bookedHNo, ",");
while (st.hasMoreTokens()) {
String hNo = st.nextToken();
if (hNo != null) {
String update = "update House set state='否',beginUseable='"
+ addDay.addDays(checkOut, 1)
+ "' where HouseNo='" + hNo + "'";
con.addSql(update);
}
}
}
con.doDML();
String returnValue = doIt();
if (returnValue == null) {
return;
}
buttonsSet();
hNumTF.setEditable(true);
autoResearch.setEnabled(true);
modify.setEnabled(true);
delete.setEnabled(true);
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
if (e.getSource() == modify) {
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -