?? checkoutpanel.java
字號:
import javax.swing.*;
import java.awt.*;
import java.sql.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.util.ArrayList;
public class CheckOutPanel extends JPanel
{
public CheckOutPanel(MainFrame parent)
{
formater = java.text.NumberFormat.getNumberInstance();
formater.setMaximumFractionDigits(2);
labels[MainFrame.NAME] = new JLabel("姓 名:");
labels[MainFrame.ID] = new JLabel("證件號:");
labels[MainFrame.HOUSE_NUM] = new JLabel("房 號:");
labels[MainFrame.TYPE] = new JLabel("類 別:");
labels[MainFrame.TIME] = new JLabel("時 間:");
labels[MainFrame.MONEY] = new JLabel("住宿費:");
labels[MainFrame.TIME].setToolTipText("入住的時間");
labels[MainFrame.NAME].setToolTipText("可以在此輸入名字來搜索");
labels[MainFrame.ID].setToolTipText("可以在此輸入ID號來搜索");
labels[MainFrame.HOUSE_NUM].setToolTipText("可以在此輸入房號來搜索");
labels[MainFrame.TYPE].setToolTipText("入住房間的類別");
labels[MainFrame.MONEY].setToolTipText("用戶到目前為止應付的金額");
for (int i = 0; i < labels.length; i++)
labels[i].setFont(new Font("Serif", Font.BOLD, 16));
this.parent = parent;
for (int i = 0; i < checkOutTextFields.length; i++)
checkOutTextFields[i] = new JTextField(20);
checkOutTextFields[MainFrame.TIME].setEditable(false);
checkOutTextFields[MainFrame.MONEY].setEditable(false);
checkOutTextFields[MainFrame.TYPE].setEditable(false);
checkOutButton.setEnabled(false);
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
constraints.weightx = 0;
constraints.weighty = 0;
add(this, labels[MainFrame.NAME], constraints, 0, 0, 1, 1);
add(this, labels[MainFrame.ID], constraints, 0, 1, 1, 1);
add(this, labels[MainFrame.HOUSE_NUM], constraints, 0, 2, 1, 1);
add(this, labels[MainFrame.TYPE], constraints, 0, 3, 1, 1);
add(this, labels[MainFrame.TIME], constraints, 0, 4, 1, 1);
add(this, labels[MainFrame.MONEY], constraints, 0, 5, 1, 1);
constraints.anchor = GridBagConstraints.WEST;
add(this, checkOutTextFields[MainFrame.NAME], constraints, 1, 0, 3, 1);
add(this, checkOutTextFields[MainFrame.ID], constraints, 1, 1, 3, 1);
add(this, checkOutTextFields[MainFrame.HOUSE_NUM], constraints, 1, 2, 3, 1);
add(this, checkOutTextFields[MainFrame.TYPE], constraints, 1, 3, 3, 1);
add(this, checkOutTextFields[MainFrame.TIME], constraints, 1, 4, 3, 1);
add(this, checkOutTextFields[MainFrame.MONEY], constraints, 1, 5, 3, 1);
final JButton serchButton = new JButton("搜索");
serchButton.setToolTipText("搜索符合的記錄");
add(this, serchButton, constraints, 0, 6, 1, 1);
checkOutButton.setToolTipText("結賬并清除相應的記錄");
add(this, checkOutButton, constraints, 1, 6, 1, 1);
prevousButton.setToolTipText("上一條記錄");
nextButton.setToolTipText("下一條記錄");
nextButton.setEnabled(false);
prevousButton.setEnabled(false);
constraints.anchor = GridBagConstraints.EAST;
add(this, prevousButton, constraints, 2, 6, 1, 1);
add(this, nextButton, constraints, 3, 6, 1, 1);
serchButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
StringBuffer sqlStringBuf = new StringBuffer("select 姓名, 身份證號, 客戶.房號, 入住日期, 類別 from 客戶, 酒店房 where 客戶.房號 = 酒店房.房號 and 酒店房.狀態 = '租出'");
if(!checkOutTextFields[MainFrame.NAME].getText().equals(""))
sqlStringBuf.append(" and 姓名 like '"+ checkOutTextFields[MainFrame.NAME].getText().replace('*', '%').replace('?', '_') + "'");
if(!checkOutTextFields[MainFrame.ID].getText().equals(""))
sqlStringBuf.append(" and 身份證號 like '"+ checkOutTextFields[MainFrame.ID].getText().replace('*', '%').replace('?', '_') + "'");
if(!checkOutTextFields[MainFrame.HOUSE_NUM].getText().equals(""))
sqlStringBuf.append(" and 客戶.房號 like '"+ checkOutTextFields[MainFrame.HOUSE_NUM].getText().replace('*', '%').replace('?', '_') + "'");
System.out.println(sqlStringBuf.toString());
ResultSet rs = getParentStatement().executeQuery(sqlStringBuf.toString());
if(array.size() != 0)
array.clear();
while(rs.next())
{
String [] record = new String[5];
record[0] = rs.getString("姓名");
record[1] = rs.getString("身份證號");
record[2] = rs.getString("房號");
record[3] = rs.getString("類別");
record[4] = rs.getDate("入住日期").toString();
array.add(record);
}
rs.close();
rs = getParentStatement().executeQuery("select * from 酒店房類別");
if(!priceTable.isEmpty())
priceTable.clear();
while(rs.next())
{
priceTable.put(rs.getString("酒店類別"), new Double(rs.getDouble("價格")));
}
for(int i = 0;i<checkOutTextFields.length;i++)
checkOutTextFields[i].setText("");
prevousButton.setEnabled(false);
if(array.size() < 2)
nextButton.setEnabled(false);
else
nextButton.setEnabled(true);
currentIndex = 0;
if(array.size() > 0)
{
showRecord();
checkOutButton.setEnabled(true);
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
});
checkOutButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
getParentStatement().addBatch("delete from 客戶 where 身份證號 = '" +
((String[]) array.get(currentIndex))[MainFrame.ID] +"'");
getParentStatement().addBatch("update 酒店房 set 狀態 = '空閑' where 房號 = '" +
((String[]) array.get(currentIndex))[MainFrame.HOUSE_NUM] + "'");
getParentStatement().executeBatch();
updateCheckInPreorderPanel();
for(int i = 0;i<checkOutTextFields.length;i++)
checkOutTextFields[i].setText("");
prevousButton.setEnabled(false);
nextButton.setEnabled(false);
checkOutButton.setEnabled(false);
showMessage("手續辦理完畢");
}
catch(SQLException e)
{
e.printStackTrace();
showMessage("數據庫更新失敗!\n" + e.toString());
}
}
});
prevousButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
currentIndex--;
showRecord();
nextButton.setEnabled(true);
if(currentIndex == 0)
prevousButton.setEnabled(false);
}
});
nextButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
currentIndex++;
showRecord();
prevousButton.setEnabled(true);
if(currentIndex >= array.size() - 1)
nextButton.setEnabled(false);
}
});
}
private void updateCheckInPreorderPanel()
{
parent.CheckInPreorderPanelInstance.updatehouseNumComboBox();
}
private void showRecord()
{
System.out.println(currentIndex + "currentIndex");
for(int i = 0;i<5;i++){
checkOutTextFields[i].setText(((String[])array.get(currentIndex))[i]);
System.out.print(((String[])array.get(currentIndex))[i] + "\n");}
java.sql.Date date = Date.valueOf(checkOutTextFields[MainFrame.TIME].getText());
double price = ((Double)priceTable.get(((String[])array.get(currentIndex))[MainFrame.TYPE])).doubleValue();
int day = (int)((new java.sql.Date(new java.util.Date().getTime()).getTime() - date.getTime()) / ONE_DAY);
StringBuffer outStrBuffer = new StringBuffer(formater.format(price * day == 0 ? 0.5 * price : price * day) + " 元 (");
if(day == 0)
outStrBuffer.append("不足一天,算半天。");
outStrBuffer.append(formater.format(price) + "元/天)");
checkOutTextFields[MainFrame.MONEY].setText(outStrBuffer.toString());
}
private void showMessage(String message)
{
parent.showMessage(message);
}
private void add(Container parent, Component c,
GridBagConstraints constraints, int x,
int y, int w, int h)
{
constraints.gridx = x;
constraints.gridy = y;
constraints.gridwidth = w;
constraints.gridheight = h;
parent.add(c, constraints);
}
private Statement getParentStatement()
{
return parent.stat;
}
private java.util.Hashtable priceTable = new java.util.Hashtable();
private JButton prevousButton = new JButton("上一條");
private JButton nextButton = new JButton("下一條");
private final JButton checkOutButton = new JButton("結賬");
private ArrayList array = new ArrayList();
private final MainFrame parent;
private int currentIndex = 0;
private JLabel[] labels = new JLabel[6];;
private JTextField [] checkOutTextFields = new JTextField[6];
private java.text.NumberFormat formater;
public static long ONE_DAY = 86400000;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -