?? cashierservice.java
字號(hào):
package com.csbook.restaurant;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author pengtao
* @version 1.0
*/
public class CashierService extends JInternalFrame {
JPanel panel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
GridLayout gridLayout1 = new GridLayout(3,4);
JLabel jLabel1 = new JLabel();
JComboBox roomNo = new JComboBox();
JLabel jLabel2 = new JLabel();
JTextField customer = new JTextField();
JLabel jLabel3 = new JLabel();
JTextField roomType = new JTextField();
JLabel jLabel4 = new JLabel();
JTextField roomCapacity = new JTextField();
JLabel jLabel5 = new JLabel();
JTextField roomDept = new JTextField();
JPanel jPanel2 = new JPanel();
FlowLayout flowLayout1 = new FlowLayout();
JButton cancel = new JButton();
JButton settleAccounts = new JButton();
JButton doOrder = new JButton();
JPanel jPanel3 = new JPanel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
JTextField cusTel = new JTextField();
String operator="";
public CashierService(String title,boolean resizable,boolean closable,boolean maximizable,boolean iconifiable,String operator) {
super(title,resizable,closable,maximizable,iconifiable);
this.operator=operator;
try {
jbInit();
pack();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
panel1.setLayout(borderLayout1);
jPanel1.setLayout(gridLayout1);
jLabel1.setText("房臺(tái)編號(hào):");
jLabel2.setText("房臺(tái)類型:");
jLabel3.setText("所屬部門:");
jLabel4.setText("聯(lián)系電話:");
jLabel5.setText("顧客姓名:");
jPanel2.setLayout(flowLayout1);
cancel.setText("關(guān)閉窗口");
cancel.addActionListener(new CashierService_cancel_actionAdapter(this));
settleAccounts.setText("結(jié)賬");
settleAccounts.addActionListener(new CashierService_settleAccounts_actionAdapter(this));
doOrder.setText("點(diǎn)菜");
doOrder.addActionListener(new CashierService_doOrder_actionAdapter(this));
jLabel6.setText("前臺(tái)服務(wù)");
roomDept.setText("");
customer.setText("");
roomCapacity.setText("");
roomType.setText("");
jLabel7.setText("容納人數(shù):");
roomNo.addActionListener(new CashierService_roomNo_actionAdapter(this));
getContentPane().add(panel1);
panel1.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jLabel1, null);
jPanel1.add(roomNo, null);
jPanel1.add(jLabel3, null);
jPanel1.add(roomDept, null);
jPanel1.add(jLabel2, null);
jPanel1.add(roomType, null);
jPanel1.add(jLabel7, null);
jPanel1.add(roomCapacity, null);
jPanel1.add(jLabel5, null);
jPanel1.add(customer, null);
jPanel1.add(jLabel4, null);
jPanel1.add(cusTel, null);
panel1.add(jPanel2, BorderLayout.SOUTH);
jPanel2.add(doOrder, null);
jPanel2.add(settleAccounts, null);
jPanel2.add(cancel, null);
panel1.add(jPanel3, BorderLayout.NORTH);
jPanel3.add(jLabel6, null);
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
this.prepareShow();
}
private void prepareShow(){
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Restaurant;user=user;password=user");
ps=conn.prepareStatement("select id from room");
rs=ps.executeQuery();
while(rs.next()){
roomNo.addItem(rs.getString("id"));
}
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if(rs!=null)try{rs.close();}catch(SQLException ignore){}
if(ps!=null)try{ps.close();}catch(SQLException ignore){}
if(conn!=null)try{conn.close();}catch(SQLException ignore){}
}
}
void roomNo_actionPerformed(ActionEvent e) {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
String roomId=roomNo.getSelectedItem().toString();
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Restaurant;user=user;password=user");
ps=conn.prepareStatement("select room.deptName,room.type,room.capacity,roomBook.customer,roomBook.tel,roomBook.bookDate from room,roomBook where roomBook.roomNo=room.id and room.id=? and datediff(Day,roomBook.bookDate,getDate())=0");
ps.setString(1,roomId);
rs=ps.executeQuery();
if(rs.next()){
roomDept.setText(rs.getString("deptName"));
roomType.setText(rs.getString("type"));
roomCapacity.setText(rs.getString("capacity"));
customer.setText(rs.getString("customer"));
cusTel.setText(rs.getString("tel"));
}
}
catch(SQLException ex){
ex.printStackTrace();
}
finally{
if(rs!=null)try{rs.close();}catch(SQLException ignore){}
if(ps!=null)try{ps.close();}catch(SQLException ignore){}
if(conn!=null)try{conn.close();}catch(SQLException ignore){}
}
}
void doOrder_actionPerformed(ActionEvent e) {
String roomId=roomNo.getSelectedItem().toString();
boolean occupied=this.roomOccupied(roomId);
if(!occupied)
{
JOptionPane.showMessageDialog(this, "該房臺(tái)沒有客人!", "提示",
JOptionPane.PLAIN_MESSAGE);
return;
}
MenuInfo mInfo= new MenuInfo(null,"菜單列表",true,roomNo.getSelectedItem().toString(),customer.getText(),cusTel.getText(),this.operator);
//將窗口置中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = mInfo.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
mInfo.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
mInfo.setVisible(true);
}
void cancel_actionPerformed(ActionEvent e) {
this.dispose();
}
void settleAccounts_actionPerformed(ActionEvent e) {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
String roomId=roomNo.getSelectedItem().toString();
//檢查指定房臺(tái)是否有客人
boolean occupied=this.roomOccupied(roomId);
if(!occupied)
{
JOptionPane.showMessageDialog(this, "該房臺(tái)沒有客人!", "提示",
JOptionPane.PLAIN_MESSAGE);
return;
}
try{
//統(tǒng)計(jì)消費(fèi)信息
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Restaurant;user=user;password=user");
ps=conn.prepareStatement("select sales.customer,sales.food,food.retailPrice,sales.amount,sales.discount,food,unit from food,sales where sales.food=food.name and sales.room=? and sales.paid=0");
ps.setString(1,roomId);
rs=ps.executeQuery();
String message="";
float total=0;
if(rs.next()){
message=rs.getString("customer")+"在本店消費(fèi)"+rs.getInt("amount")+rs.getString("unit")+rs.getString("food");
total+=rs.getFloat("retailPrice")*rs.getInt("amount")*rs.getFloat("discount");
}
while(rs.next()){
message+=","+rs.getInt("amount")+rs.getString("unit")+rs.getString("food");
total+=rs.getFloat("retailPrice")*rs.getInt("amount")*rs.getFloat("discount");
}
message+="\n合計(jì):"+total+"元";
JOptionPane.showMessageDialog(this,message,"結(jié)賬",JOptionPane.PLAIN_MESSAGE);
//更新訂單狀態(tài)
ps=conn.prepareStatement("update sales set paid=1 where room=?");
ps.setString(1,roomId);
ps.executeUpdate();
//刪去房臺(tái)預(yù)訂表中的相關(guān)信息
ps=conn.prepareStatement("delete from roomBook where roomNo=? and datediff(Day,bookDate,getDate())=0");
ps.setString(1,roomId);
ps.executeUpdate();
}
catch(SQLException ex){
ex.printStackTrace();
}
finally{
if(rs!=null)try{rs.close();}catch(SQLException ignore){}
if(ps!=null)try{ps.close();}catch(SQLException ignore){}
if(conn!=null)try{conn.close();}catch(SQLException ignore){}
}
}
private boolean roomOccupied(String roomId)
{
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
boolean occupied=false;
try{
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Restaurant;user=user;password=user");
ps=conn.prepareStatement("select bookDate from roomBook where roomNo=? and datediff(Day,bookDate,getDate())=0");
ps.setString(1,roomId);
rs=ps.executeQuery();
if(rs.next()){
occupied=true;
}
}
catch(SQLException ex){
ex.printStackTrace();
}
finally{
if(rs!=null)try{rs.close();}catch(SQLException ignore){}
if(ps!=null)try{ps.close();}catch(SQLException ignore){}
if(conn!=null)try{conn.close();}catch(SQLException ignore){}
}
return occupied;
}
}
class CashierService_roomNo_actionAdapter implements java.awt.event.ActionListener {
CashierService adaptee;
CashierService_roomNo_actionAdapter(CashierService adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.roomNo_actionPerformed(e);
}
}
class CashierService_doOrder_actionAdapter implements java.awt.event.ActionListener {
CashierService adaptee;
CashierService_doOrder_actionAdapter(CashierService adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.doOrder_actionPerformed(e);
}
}
class CashierService_cancel_actionAdapter implements java.awt.event.ActionListener {
CashierService adaptee;
CashierService_cancel_actionAdapter(CashierService adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.cancel_actionPerformed(e);
}
}
class CashierService_settleAccounts_actionAdapter implements java.awt.event.ActionListener {
CashierService adaptee;
CashierService_settleAccounts_actionAdapter(CashierService adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.settleAccounts_actionPerformed(e);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -