⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cashierservice.java

📁 本例以一个酒店的进存销系统为例
💻 JAVA
字号:
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("房台编号:");
    jLabel2.setText("房台类型:");
    jLabel3.setText("所属部门:");
    jLabel4.setText("联系电话:");
    jLabel5.setText("顾客姓名:");
    jPanel2.setLayout(flowLayout1);
    cancel.setText("关闭窗口");
    cancel.addActionListener(new CashierService_cancel_actionAdapter(this));
    settleAccounts.setText("结账");
    settleAccounts.addActionListener(new CashierService_settleAccounts_actionAdapter(this));
    doOrder.setText("点菜");
    doOrder.addActionListener(new CashierService_doOrder_actionAdapter(this));
    jLabel6.setText("前台服务");
    roomDept.setText("");
    customer.setText("");
    roomCapacity.setText("");
    roomType.setText("");
    jLabel7.setText("容纳人数:");
    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, "该房台没有客人!", "提示",
                                       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();
     //检查指定房台是否有客人
     boolean occupied=this.roomOccupied(roomId);
     if(!occupied)
      {
        JOptionPane.showMessageDialog(this, "该房台没有客人!", "提示",
                                      JOptionPane.PLAIN_MESSAGE);
        return;
      }
     try{
       //统计消费信息
       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")+"在本店消费"+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合计:"+total+"元";
       JOptionPane.showMessageDialog(this,message,"结账",JOptionPane.PLAIN_MESSAGE);
       //更新订单状态
       ps=conn.prepareStatement("update sales set paid=1 where room=?");
       ps.setString(1,roomId);
       ps.executeUpdate();
       //删去房台预订表中的相关信息
       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);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -