?? checkout.java
字號:
package file1;
/*
* 功能描述:客戶結帳入口,處理客戶的結帳
* @Author:黃順武
* Create Time:---
* Last Modified:2007-12-15
* Modify Reason:數據庫連接類DBConnection 的內部結構設計得到優化
*/
import java.sql.*;
import sun.jdbc.rowset.*;
import java.util.Date;
import java.util.StringTokenizer;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class CheckOut extends JPanel implements ActionListener {
private JLabel cName = new JLabel("客戶名稱:");
private JComboBox nameBox = new JComboBox();
private JButton query = new JButton(" 查詢 ");
private JButton checkOut = new JButton(" 結帳 ");
private JLabel moneyLB = new JLabel("租金:");
private JTextField moneyTF = new JTextField(8);
private JLabel consumeLB = new JLabel("消費金額: ");
private JTextField consumeTF = new JTextField(8);
private JLabel preLB = new JLabel("優惠標準:");
private JTextField preTF = new JTextField(10);
private JLabel firstPayLB = new JLabel("預付金額:");
private JTextField firstPayTF = new JTextField(8);
private JLabel remained = new JLabel("本次應付總金額:");
private JTextField remainedTF = new JTextField(8);
private JPanel p1 = new JPanel();
private JPanel p2 = new JPanel();
private JPanel p3 = new JPanel();
private JTable resultTable = null;
private String[] title = { "客戶名稱", "訂房數量", "所訂客房號", "客房級別", "入住日期",
"原定退房日期", "實際退房時間", "酒水消費金額", "記錄目前狀態" };
private int titleNum = 0;
private String[][] data = null;
private JScrollPane resultPane = new JScrollPane();
private DBConnection con = null;
private GetDate dateGet = null;
private Formatter format = null;
private String[] IDS = null;
private Date d = new Date();
private String value = null;
private String todayStr = null;
private AddSomeDays addDay = null;
private String[] client_ids = null;// 存儲用戶的所有ID
public CheckOut() {
cName.setBackground(Color.LIGHT_GRAY);
cName.setBorder(null);
titleNum = title.length;
String index = getNameOfClients();
if (index == null) {
return;
}
dateGet = new GetDate();
format = new Formatter();
addDay = new AddSomeDays();
query.setBackground(Color.LIGHT_GRAY);
query.setBorder(null);
query.setEnabled(true);
checkOut.setEnabled(false);
checkOut.setBackground(Color.LIGHT_GRAY);
checkOut.setBorder(null);
moneyTF.setBackground(Color.LIGHT_GRAY);
moneyTF.setBorder(null);
moneyTF.setEditable(false);
consumeTF.setBackground(Color.LIGHT_GRAY);
consumeTF.setBorder(null);
consumeTF.setEditable(false);
preTF.setBackground(Color.LIGHT_GRAY);
preTF.setBorder(null);
preTF.setEditable(false);
firstPayTF.setBackground(Color.LIGHT_GRAY);
firstPayTF.setBorder(null);
firstPayTF.setEditable(false);
remainedTF.setBackground(Color.LIGHT_GRAY);
remainedTF.setBorder(null);
remainedTF.setEditable(false);
remained.setForeground(Color.red);
p1.setLayout(new FlowLayout(FlowLayout.CENTER, 35, 0));
p1.add(cName);
p1.add(nameBox);
p1.add(query);
p1.add(remained);
p1.add(remainedTF);
p1.add(checkOut);
p2.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0));
p2.add(moneyLB);
p2.add(moneyTF);
p2.add(consumeLB);
p2.add(consumeTF);
p2.add(preLB);
p2.add(preTF);
p2.add(firstPayLB);
p2.add(firstPayTF);
p3.setLayout(new BorderLayout(0, 20));
p3.add(p1, BorderLayout.NORTH);
p3.add(p2, BorderLayout.CENTER);
this.setLayout(new BorderLayout(0, 20));
this.add(p3, BorderLayout.NORTH);
query.addActionListener(this);
checkOut.addActionListener(this);
}
private String getNameOfClients() {
try {
con = new DBConnection();
String queryStr = "select distinct rendererID,cName from HUseBook,Client where state='是' and rendererID=Client.id";
CachedRowSet crs = con.getResultSet(queryStr);
int count = 0;
while (crs.next()) {
count++;
}
crs.beforeFirst();
if (count == 0) {
JOptionPane.showMessageDialog(null, "沒有未結帳的客戶!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
client_ids = new String[count];
count = 0;
while (crs.next()) {
client_ids[count++] = String.valueOf(crs.getInt(1));
nameBox.addItem(crs.getString(2));
}
nameBox.setSelectedIndex(-1);
} catch (SQLException sqle) {
sqle.printStackTrace();
return null;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return "success";
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == query) {
int name_index = nameBox.getSelectedIndex();
if (name_index == -1) {
JOptionPane.showMessageDialog(null, "請選擇客戶名稱!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String queryStr = "select HUseBook.ID,cName,bookNum,hNo,hGrade,dateIn,dateOut,consume,state from HUseBook,Client where rendererID="
+ client_ids[name_index] + " and state='是' and rendererID=Client.ID";
CachedRowSet crs = null;
try {
crs = con.getResultSet(queryStr);
int row = 0;
while (crs.next()) {
row++;
}
if (row == 0) {
JOptionPane.showMessageDialog(null, "該客戶沒有訂房記錄!請您確認!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
IDS = new String[row];
data = new String[row][titleNum];
crs.beforeFirst();
row = 0;
String today = format.valueConverted(d);
while (crs.next()) {
IDS[row] = String.valueOf(crs.getInt(1));
data[row][0] = crs.getString(2);
data[row][1] = String.valueOf(crs.getInt(3));
data[row][2] = crs.getString(4);
data[row][3] = crs.getString(5);
data[row][4] = crs.getString(6);
data[row][5] = crs.getString(7);
data[row][6] = today;
data[row][7] = String.valueOf(crs.getFloat(8));
data[row][8] = crs.getString(9);
row++;
}
resultTable = new JTable(data, title);
resultPane = new JScrollPane(resultTable);
this.add(resultPane, BorderLayout.CENTER);
this.validate();
crs = null;
row = resultTable.getModel().getRowCount();
float firstPayPercent = 0;
float firstPay = 0;
float standard = 0;
float moneyOfBooked = 0;// 訂房的錢的數目
float moneyOfConsume = 0;
String nameFirst = (String) nameBox.getSelectedItem();
if (nameFirst == null) {
JOptionPane.showMessageDialog(null, "請選擇客戶名稱!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
queryStr = "select standard,firstPayPercent from CGrade,Client where CGrade.id=cGradeID and Client.ID="
+ client_ids[name_index];
crs = con.getResultSet(queryStr);
while (crs.next()) {
standard = crs.getFloat(1);
firstPayPercent = crs.getFloat(2);
}
crs = null;
String in = null;
String inAfterAdded = null;
String checkOutStr = null;
value = format.valueConverted(d);
todayStr = dateGet.getDate(value);
for (int count = 0; count < row; count++) {
// demand modify here
in = (String) resultTable.getModel().getValueAt(count, 5);
checkOutStr = (String) resultTable.getModel().getValueAt(
count, 6);
int dayNumActual = 1;
for (;; dayNumActual++) {
inAfterAdded = addDay.addDays(in, dayNumActual);
if (inAfterAdded.equals(todayStr)) {
break;
}
}
dayNumActual++;// 加一后才符合邏輯
int dayNumIni = 1;
for (;; dayNumIni++) {
inAfterAdded = addDay.addDays(in, dayNumIni);
if (inAfterAdded.equals(checkOutStr)) {
break;
}
}
dayNumIni++;// 加一后才符合邏輯
String numStr = (String) resultTable.getModel().getValueAt(
count, 1);
int num = Integer.valueOf(numStr);
String hGrade = (String) resultTable.getModel().getValueAt(
count, 3);
String consumeStr = (String) resultTable.getModel()
.getValueAt(count, 7);
float consume = Float.valueOf(consumeStr);
queryStr = "select Sprice from HGrade where grade='"
+ hGrade + "'";
crs = con.getResultSet(queryStr);
float price = 0;
if (crs.next()) {
price = crs.getFloat(1);
}
moneyOfConsume += consume;
moneyOfBooked += (num * price * dayNumActual * standard);
firstPay += (num * price * dayNumIni * standard * firstPayPercent);
}
float moneyStillToPay = moneyOfBooked + moneyOfConsume
- firstPay;
moneyTF.setText(String.valueOf(moneyOfBooked));
consumeTF.setText(String.valueOf(moneyOfConsume));
preTF.setText(String.valueOf(standard));
firstPayTF.setText(String.valueOf(firstPay));
remainedTF.setText(String.valueOf(moneyStillToPay));
if (row != 0) {
checkOut.setEnabled(true);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
if (ae.getSource() == checkOut) {
String nameFirst = (String) nameBox.getSelectedItem();
if (nameFirst == null) {
JOptionPane.showMessageDialog(null, "請選擇客戶名稱!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String nameSecond = resultTable.getModel().getValueAt(0, 0)
.toString().trim();
if (!nameFirst.equals(nameSecond)) {
JOptionPane.showMessageDialog(null, "請您先點擊查詢按鈕!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
for (String id : IDS) {
String queryS = "update HUseBook set state='否',dateOut='"
+ todayStr + "' where ID=" + id;
con.addSql(queryS);
try {
con.doDML();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
int row = resultTable.getModel().getRowCount();
String hno = null;
for (int count = 0; count < row; count++) {
hno = (String) resultTable.getModel().getValueAt(count, 2);
String update = null;
String today = dateGet.getDate(format.valueConverted(d));
if (!hno.contains(",")) {
update = "update House set state='是',beginUseable='"
+ addDay.addDays(today, 1) + "' where HouseNo='"
+ hno + "'";
con.addSql(update);
try {
con.doDML();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
} else {
StringTokenizer st = new StringTokenizer(hno, ",");
while (st.hasMoreTokens()) {
String sHNo = st.nextToken();
if (sHNo != null && !sHNo.equals("")) {
update = "update House set state='是',beginUseable='"
+ addDay.addDays(today, 1)
+ "' where HouseNo='" + sHNo + "'";
con.addSql(update);
try {
con.doDML();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
}
}
}
for (int base = 0; base < row; base++) {
data[base][8] = "否";
}
resultPane.updateUI();
checkOut.setEnabled(false);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -