JAVA模拟自动售货机

来源:互联网 发布:dk智力训练手册淘宝 编辑:程序博客网 时间:2024/05/01 19:51

版本说明

  1. 2015-07-02 第一版。

  2. 2015-07-04 修复余额变更错误问题(变量使用错误造成),增加修改商品数量功能,调整视图结构,修改Unicode为中文,增加功能提示。

下面的代码为最新版本代码。

import java.awt.BorderLayout;import java.awt.Color;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Image;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.SwingUtilities;import javax.swing.border.CompoundBorder;import javax.swing.border.LineBorder;import javax.swing.border.TitledBorder;import javax.swing.JTabbedPane;import java.awt.FlowLayout;/** * 自动售货机 功能说明: * <ol> * <li>点击图片或图片下方的购买复选框进行商品的选择或取消选择,商品数量小于等于0时无法选择;</li> * <li>点击“投入一元”或“投入五元”按钮模拟投入钱操作;</li> * <li>点击“购买”按钮完成购买,当投入钱数少于购买商品需要的钱数时购买不成功,当系统零钱不够找零时购买不成功;</li> * <li>点击“退款”按钮完成退款;</li> * <li>点击“查看”按钮可以查看所有商品的剩余数量,显示五秒后会关闭显示;</li> * <li>点击“登录”按钮弹出登录管理员需要的密码输入框,输入ADMIN后可以进入管理员页面,管理员页面分为修改余额和修改商品两个标签页,修改余额标签页可以查看系统总余额,系统一元数量和五元数量,同时可以手动更新一元和五元的数量,完成修改余额操作;修改商品标签页可以查看商品总数量和各个商品的分数量,同时可以手动修改每个商品的数量,完成修改商品数量操作。</li> * </ol> * <p> * <h4>注意在运行之前修改图片路径</h4> *  * @author th */public class AutoSaleMachine extends JFrame {    private static final long serialVersionUID = 6576104659382463412L;    private static final String ADMIN = "ADMIN"; // 管理员密码    // 初始化所有商品的名字    private static final String NAME_COKE = "可口可乐";    private static final String NAME_FANTA = "芬达";    private static final String NAME_RTEA = "红茶";    private static final String NAME_GTEA = "绿茶";    // 初始化所有商品的价格    private static final int PRICE_COKE = 3;    private static final int PRICE_FANTA = 2;    private static final int PRICE_RTEA = 4;    private static final int PRICE_GTEA = 5;    // 初始化所有商品的ID,没有多少用    private static final int ID_COKE = 1;    private static final int ID_FANTA = 2;    private static final int ID_RTEA = 3;    private static final int ID_GTEA = 4;    // 初始化所有商品数量    private static int COUNT_COKE = 10;    private static int COUNT_FANTA = 1;    private static int COUNT_RTEA = 30;    private static int COUNT_GTEA = 2;    private String img_coke = "photos/coke.jpg";    private String img_fanta = "photos/fanta.jpg";    private String img_rtea = "photos/rtea.jpg";    private String img_gtea = "photos/gtea.jpg";    private int cokeCount;    private int fantaCount;    private int rteaCount;    private int gteaCount;    private int userMoneySum; // 用户投入的钱数    private int userOneCount; // 用户一元计数    private int userFiveCount; // 用户五元计数    private int sysMoneySum; // 系统的总钱数    private int sysOneCount = 1; // 系统一元计数    private int sysFiveCount = 20; // 系统五元计数    private JTextField txt_UserMoney;    private JButton btn_UserFive;    private JButton btn_UserOne;    private GoodPanel gp_coke;    private GoodPanel gp_fanta;    private GoodPanel gp_rtea;    private GoodPanel gp_gtea;    private JButton btn_Show;    private JButton btn_Buy;    private JButton btn_Cancle;    private JButton btn_Admin;    private JPanel panel_Admin;    private JLabel lbl_SysMoneySum;    private JTextField txt_SysSum;    private JLabel lbl_SysOneSum;    private JTextField txt_SysOne;    private JLabel lbl_SysFiveSum;    private JTextField txt_SysFive;    private JButton btn_Save;    private JButton btn_Logout;    public AutoSaleMachine() {        setTitle("自动售货机");        GridBagLayout gridBagLayout = new GridBagLayout();        gridBagLayout.columnWidths = new int[] { 0, 0 };        gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0 };        gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };        gridBagLayout.rowWeights = new double[] { 1.0, 0.0, 0.0,                Double.MIN_VALUE };        getContentPane().setLayout(gridBagLayout);        JPanel panel_Good = new JPanel();        GridBagConstraints gbc_panel_Good = new GridBagConstraints();        gbc_panel_Good.insets = new Insets(0, 0, 5, 0);        gbc_panel_Good.fill = GridBagConstraints.BOTH;        gbc_panel_Good.gridx = 0;        gbc_panel_Good.gridy = 0;        getContentPane().add(panel_Good, gbc_panel_Good);        GridBagLayout gbl_panel_Good = new GridBagLayout();        gbl_panel_Good.columnWidths = new int[] { 230, 230, 0 };        gbl_panel_Good.rowHeights = new int[] { 0, 0, 0 };        gbl_panel_Good.columnWeights = new double[] { 0.5, 0.5,                Double.MIN_VALUE };        gbl_panel_Good.rowWeights = new double[] { 0.5, 0.5, Double.MIN_VALUE };        panel_Good.setLayout(gbl_panel_Good);        gp_coke = new GoodPanel(img_coke, PRICE_COKE, NAME_COKE, ID_COKE,                COUNT_COKE);        GridBagConstraints gbc_gp_coke = new GridBagConstraints();        gbc_gp_coke.insets = new Insets(5, 5, 5, 5);        gbc_gp_coke.fill = GridBagConstraints.BOTH;        gbc_gp_coke.gridx = 0;        gbc_gp_coke.gridy = 0;        panel_Good.add(gp_coke, gbc_gp_coke);        gp_fanta = new GoodPanel(img_fanta, PRICE_FANTA, NAME_FANTA, ID_FANTA,                COUNT_FANTA);        GridBagConstraints gbc_gp_fanta = new GridBagConstraints();        gbc_gp_fanta.insets = new Insets(5, 5, 5, 5);        gbc_gp_fanta.fill = GridBagConstraints.BOTH;        gbc_gp_fanta.gridx = 1;        gbc_gp_fanta.gridy = 0;        panel_Good.add(gp_fanta, gbc_gp_fanta);        gp_rtea = new GoodPanel(img_rtea, PRICE_RTEA, NAME_RTEA, ID_RTEA,                COUNT_RTEA);        GridBagConstraints gbc_gp_rtea = new GridBagConstraints();        gbc_gp_rtea.insets = new Insets(5, 5, 5, 5);        gbc_gp_rtea.fill = GridBagConstraints.BOTH;        gbc_gp_rtea.gridx = 0;        gbc_gp_rtea.gridy = 1;        panel_Good.add(gp_rtea, gbc_gp_rtea);        gp_gtea = new GoodPanel(img_gtea, PRICE_GTEA, NAME_GTEA, ID_GTEA,                COUNT_GTEA);        GridBagConstraints gbc_gp_gtea = new GridBagConstraints();        gbc_gp_gtea.insets = new Insets(5, 5, 5, 5);        gbc_gp_gtea.fill = GridBagConstraints.BOTH;        gbc_gp_gtea.gridx = 1;        gbc_gp_gtea.gridy = 1;        panel_Good.add(gp_gtea, gbc_gp_gtea);        JPanel panel_Btn = new JPanel();        GridBagConstraints gbc_panel_Btn = new GridBagConstraints();        gbc_panel_Btn.insets = new Insets(0, 0, 5, 0);        gbc_panel_Btn.fill = GridBagConstraints.HORIZONTAL;        gbc_panel_Btn.gridx = 0;        gbc_panel_Btn.gridy = 1;        getContentPane().add(panel_Btn, gbc_panel_Btn);        GridBagLayout gbl_panel_Btn = new GridBagLayout();        gbl_panel_Btn.columnWidths = new int[] { 0, 0, 0, 0, 0 };        gbl_panel_Btn.rowHeights = new int[] { 0, 0, 0 };        gbl_panel_Btn.columnWeights = new double[] { 0.25, 0.25, 0.25, 0.25,                Double.MIN_VALUE };        gbl_panel_Btn.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };        panel_Btn.setLayout(gbl_panel_Btn);        JLabel lbl_UserMoney = new JLabel("投入金额(元):");        GridBagConstraints gbc_lbl_UserMoney = new GridBagConstraints();        gbc_lbl_UserMoney.insets = new Insets(5, 5, 5, 5);        gbc_lbl_UserMoney.gridx = 0;        gbc_lbl_UserMoney.gridy = 0;        panel_Btn.add(lbl_UserMoney, gbc_lbl_UserMoney);        txt_UserMoney = new JTextField();        txt_UserMoney.setEditable(false);        GridBagConstraints gbc_txt_UserMoney = new GridBagConstraints();        gbc_txt_UserMoney.insets = new Insets(5, 0, 5, 5);        gbc_txt_UserMoney.fill = GridBagConstraints.HORIZONTAL;        gbc_txt_UserMoney.gridx = 1;        gbc_txt_UserMoney.gridy = 0;        panel_Btn.add(txt_UserMoney, gbc_txt_UserMoney);        txt_UserMoney.setColumns(10);        btn_UserOne = new JButton("投入一元");        GridBagConstraints gbc_btn_UserOne = new GridBagConstraints();        gbc_btn_UserOne.insets = new Insets(5, 0, 5, 5);        gbc_btn_UserOne.gridx = 2;        gbc_btn_UserOne.gridy = 0;        panel_Btn.add(btn_UserOne, gbc_btn_UserOne);        btn_UserOne.addActionListener(handler);        btn_Show = new JButton("查看");        btn_Show.setToolTipText("查看自动售货机中的商品数量,显示五秒后关闭");        GridBagConstraints gbc_btn_Show = new GridBagConstraints();        gbc_btn_Show.insets = new Insets(0, 0, 5, 5);        gbc_btn_Show.gridx = 0;        gbc_btn_Show.gridy = 1;        panel_Btn.add(btn_Show, gbc_btn_Show);        btn_Show.addActionListener(handler);        btn_Buy = new JButton("购买");        GridBagConstraints gbc_btn_Buy = new GridBagConstraints();        gbc_btn_Buy.insets = new Insets(0, 0, 5, 5);        gbc_btn_Buy.gridx = 1;        gbc_btn_Buy.gridy = 1;        panel_Btn.add(btn_Buy, gbc_btn_Buy);        btn_Buy.addActionListener(handler);        btn_UserFive = new JButton("投入五元");        GridBagConstraints gbc_btn_UserFive = new GridBagConstraints();        gbc_btn_UserFive.insets = new Insets(5, 0, 5, 0);        gbc_btn_UserFive.gridx = 3;        gbc_btn_UserFive.gridy = 0;        panel_Btn.add(btn_UserFive, gbc_btn_UserFive);        btn_UserFive.addActionListener(handler);        btn_Cancle = new JButton("取消");        GridBagConstraints gbc_btn_Cancle = new GridBagConstraints();        gbc_btn_Cancle.insets = new Insets(0, 0, 5, 5);        gbc_btn_Cancle.gridx = 2;        gbc_btn_Cancle.gridy = 1;        panel_Btn.add(btn_Cancle, gbc_btn_Cancle);        btn_Cancle.addActionListener(handler);        btn_Admin = new JButton("登录");        btn_Admin.setToolTipText("需要登录密码,登录后可以修改系统余额和商品数量。");        GridBagConstraints gbc_btn_Admin = new GridBagConstraints();        gbc_btn_Admin.insets = new Insets(0, 0, 5, 0);        gbc_btn_Admin.gridx = 3;        gbc_btn_Admin.gridy = 1;        panel_Btn.add(btn_Admin, gbc_btn_Admin);        btn_Admin.addActionListener(handler);        panel_Admin = new JPanel();        panel_Admin.setBorder(new TitledBorder(null, "管理员界面",                TitledBorder.LEADING, TitledBorder.TOP, null, null));        GridBagConstraints gbc_panel = new GridBagConstraints();        gbc_panel.fill = GridBagConstraints.BOTH;        gbc_panel.gridx = 0;        gbc_panel.gridy = 2;        getContentPane().add(panel_Admin, gbc_panel);        panel_Admin.setLayout(new BorderLayout(0, 0));        tabbedPane = new JTabbedPane(JTabbedPane.TOP);        panel_Admin.add(tabbedPane, BorderLayout.NORTH);        panel_AdminMoney = new JPanel();        tabbedPane.addTab("修改余额", null, panel_AdminMoney, null);        GridBagLayout gbl_panel_AdminMoney = new GridBagLayout();        gbl_panel_AdminMoney.columnWidths = new int[] { 0, 0, 0, 0, 0 };        gbl_panel_AdminMoney.rowHeights = new int[] { 0, 0, 0, 0 };        gbl_panel_AdminMoney.columnWeights = new double[] { 0.0, 0.5, 0.0, 0.5,                Double.MIN_VALUE };        gbl_panel_AdminMoney.rowWeights = new double[] { 0.0, 0.0, 0.0,                Double.MIN_VALUE };        panel_AdminMoney.setLayout(gbl_panel_AdminMoney);        lbl_SysMoneySum = new JLabel("系统总余额:");        GridBagConstraints gbc_lbl_SysMoneySum = new GridBagConstraints();        gbc_lbl_SysMoneySum.insets = new Insets(0, 0, 5, 5);        gbc_lbl_SysMoneySum.anchor = GridBagConstraints.EAST;        gbc_lbl_SysMoneySum.gridx = 0;        gbc_lbl_SysMoneySum.gridy = 0;        panel_AdminMoney.add(lbl_SysMoneySum, gbc_lbl_SysMoneySum);        txt_SysSum = new JTextField();        txt_SysSum.setEditable(false);        GridBagConstraints gbc_txt_SysSum = new GridBagConstraints();        gbc_txt_SysSum.insets = new Insets(0, 0, 5, 5);        gbc_txt_SysSum.fill = GridBagConstraints.HORIZONTAL;        gbc_txt_SysSum.gridx = 1;        gbc_txt_SysSum.gridy = 0;        panel_AdminMoney.add(txt_SysSum, gbc_txt_SysSum);        txt_SysSum.setColumns(10);        lbl_SysOneSum = new JLabel("系统一元数量:");        GridBagConstraints gbc_lbl_SysOneSum = new GridBagConstraints();        gbc_lbl_SysOneSum.anchor = GridBagConstraints.EAST;        gbc_lbl_SysOneSum.insets = new Insets(0, 5, 5, 5);        gbc_lbl_SysOneSum.gridx = 0;        gbc_lbl_SysOneSum.gridy = 1;        panel_AdminMoney.add(lbl_SysOneSum, gbc_lbl_SysOneSum);        txt_SysOne = new JTextField();        GridBagConstraints gbc_txt_SysOne = new GridBagConstraints();        gbc_txt_SysOne.insets = new Insets(0, 0, 5, 5);        gbc_txt_SysOne.fill = GridBagConstraints.HORIZONTAL;        gbc_txt_SysOne.gridx = 1;        gbc_txt_SysOne.gridy = 1;        txt_SysOne.setToolTipText("可以输入修改后的数量,点击保存按钮完成系统中一元数量的修改。");        panel_AdminMoney.add(txt_SysOne, gbc_txt_SysOne);        txt_SysOne.setColumns(10);        lbl_SysFiveSum = new JLabel("系统五元数量:");        GridBagConstraints gbc_lbl_SysFiveSum = new GridBagConstraints();        gbc_lbl_SysFiveSum.anchor = GridBagConstraints.EAST;        gbc_lbl_SysFiveSum.insets = new Insets(0, 5, 0, 5);        gbc_lbl_SysFiveSum.gridx = 0;        gbc_lbl_SysFiveSum.gridy = 2;        panel_AdminMoney.add(lbl_SysFiveSum, gbc_lbl_SysFiveSum);        txt_SysFive = new JTextField();        GridBagConstraints gbc_txt_SysFive = new GridBagConstraints();        gbc_txt_SysFive.insets = new Insets(0, 0, 0, 5);        gbc_txt_SysFive.fill = GridBagConstraints.HORIZONTAL;        gbc_txt_SysFive.gridx = 1;        gbc_txt_SysFive.gridy = 2;        txt_SysFive.setToolTipText("可以输入修改后的数量,点击保存按钮完成系统中一元数量的修改。");        panel_AdminMoney.add(txt_SysFive, gbc_txt_SysFive);        txt_SysFive.setColumns(10);        panel_AdminGood = new JPanel();        tabbedPane.addTab("\u4FEE\u6539\u5546\u54C1", null, panel_AdminGood,                null);        GridBagLayout gbl_panel_AdminGood = new GridBagLayout();        gbl_panel_AdminGood.columnWidths = new int[] { 0, 0, 0, 0, 0 };        gbl_panel_AdminGood.rowHeights = new int[] { 0, 0, 0, 0 };        gbl_panel_AdminGood.columnWeights = new double[] { 0.0, 0.5, 0.0, 0.5,                Double.MIN_VALUE };        gbl_panel_AdminGood.rowWeights = new double[] { 0.0, 0.0, 0.0,                Double.MIN_VALUE };        panel_AdminGood.setLayout(gbl_panel_AdminGood);        lbl_SysGood = new JLabel("系统总商品数量:");        GridBagConstraints gbc_lbl_SysGood = new GridBagConstraints();        gbc_lbl_SysGood.anchor = GridBagConstraints.EAST;        gbc_lbl_SysGood.insets = new Insets(0, 0, 5, 5);        gbc_lbl_SysGood.gridx = 0;        gbc_lbl_SysGood.gridy = 0;        panel_AdminGood.add(lbl_SysGood, gbc_lbl_SysGood);        txt_SysGood = new JTextField();        txt_SysGood.setEditable(false);        GridBagConstraints gbc_txt_SysGood = new GridBagConstraints();        gbc_txt_SysGood.insets = new Insets(0, 0, 5, 5);        gbc_txt_SysGood.fill = GridBagConstraints.HORIZONTAL;        gbc_txt_SysGood.gridx = 1;        gbc_txt_SysGood.gridy = 0;        panel_AdminGood.add(txt_SysGood, gbc_txt_SysGood);        txt_SysGood.setColumns(10);        lbl_SysCoke = new JLabel("可口可乐数量:");        GridBagConstraints gbc_lbl_SysCoke = new GridBagConstraints();        gbc_lbl_SysCoke.anchor = GridBagConstraints.EAST;        gbc_lbl_SysCoke.insets = new Insets(0, 5, 5, 0);        gbc_lbl_SysCoke.gridx = 0;        gbc_lbl_SysCoke.gridy = 1;        panel_AdminGood.add(lbl_SysCoke, gbc_lbl_SysCoke);        txt_CountCoke = new JTextField();        GridBagConstraints gbc_txt_CountCoke = new GridBagConstraints();        gbc_txt_CountCoke.insets = new Insets(0, 0, 5, 5);        gbc_txt_CountCoke.fill = GridBagConstraints.HORIZONTAL;        gbc_txt_CountCoke.gridx = 1;        gbc_txt_CountCoke.gridy = 1;        panel_AdminGood.add(txt_CountCoke, gbc_txt_CountCoke);        txt_CountCoke.setColumns(10);        txt_CountCoke.setToolTipText("可以输入修改后的数量,点击保存按钮完成系统中可口可乐数量的修改。");        lbl_SysFanta = new JLabel("芬达数量:");        GridBagConstraints gbc_lbl_SysFanta = new GridBagConstraints();        gbc_lbl_SysFanta.anchor = GridBagConstraints.EAST;        gbc_lbl_SysFanta.insets = new Insets(0, 5, 5, 0);        gbc_lbl_SysFanta.gridx = 2;        gbc_lbl_SysFanta.gridy = 1;        panel_AdminGood.add(lbl_SysFanta, gbc_lbl_SysFanta);        txt_CountFanta = new JTextField();        GridBagConstraints gbc_txt_CountFanta = new GridBagConstraints();        gbc_txt_CountFanta.insets = new Insets(0, 0, 5, 0);        gbc_txt_CountFanta.fill = GridBagConstraints.HORIZONTAL;        gbc_txt_CountFanta.gridx = 3;        gbc_txt_CountFanta.gridy = 1;        panel_AdminGood.add(txt_CountFanta, gbc_txt_CountFanta);        txt_CountFanta.setColumns(10);        txt_CountFanta.setToolTipText("可以输入修改后的数量,点击保存按钮完成系统中芬达数量的修改。");        lbl_SysRtea = new JLabel("红茶数量:");        GridBagConstraints gbc_lbl_SysRtea = new GridBagConstraints();        gbc_lbl_SysRtea.anchor = GridBagConstraints.EAST;        gbc_lbl_SysRtea.insets = new Insets(0, 5, 5, 0);        gbc_lbl_SysRtea.gridx = 0;        gbc_lbl_SysRtea.gridy = 2;        panel_AdminGood.add(lbl_SysRtea, gbc_lbl_SysRtea);        txt_CountRtea = new JTextField();        GridBagConstraints gbc_txt_CountRtea = new GridBagConstraints();        gbc_txt_CountRtea.insets = new Insets(0, 0, 5, 5);        gbc_txt_CountRtea.fill = GridBagConstraints.HORIZONTAL;        gbc_txt_CountRtea.gridx = 1;        gbc_txt_CountRtea.gridy = 2;        panel_AdminGood.add(txt_CountRtea, gbc_txt_CountRtea);        txt_CountRtea.setColumns(10);        txt_CountRtea.setToolTipText("可以输入修改后的数量,点击保存按钮完成系统中红茶数量的修改。");        lbl_SysGtea = new JLabel("绿茶数量:");        GridBagConstraints gbc_lbl_SysGtea = new GridBagConstraints();        gbc_lbl_SysGtea.anchor = GridBagConstraints.EAST;        gbc_lbl_SysGtea.insets = new Insets(0, 5, 5, 0);        gbc_lbl_SysGtea.gridx = 2;        gbc_lbl_SysGtea.gridy = 2;        panel_AdminGood.add(lbl_SysGtea, gbc_lbl_SysGtea);        txt_CountGtea = new JTextField();        GridBagConstraints gbc_txt_CountGtea = new GridBagConstraints();        gbc_txt_CountGtea.insets = new Insets(0, 0, 5, 0);        gbc_txt_CountGtea.fill = GridBagConstraints.HORIZONTAL;        gbc_txt_CountGtea.gridx = 3;        gbc_txt_CountGtea.gridy = 2;        panel_AdminGood.add(txt_CountGtea, gbc_txt_CountGtea);        txt_CountGtea.setColumns(10);        txt_CountGtea.setToolTipText("可以输入修改后的数量,点击保存按钮完成系统中绿茶数量的修改。");        panel_BtnAdmin = new JPanel();        FlowLayout fl_panel_BtnAdmin = (FlowLayout) panel_BtnAdmin.getLayout();        fl_panel_BtnAdmin.setHgap(35);        btn_Save = new JButton("保存");        panel_BtnAdmin.add(btn_Save);        btn_Save.addActionListener(handler);        btn_Logout = new JButton("退出");        panel_BtnAdmin.add(btn_Logout);        panel_Admin.add(panel_BtnAdmin, BorderLayout.SOUTH);        btn_Logout.addActionListener(handler);        panel_Admin.setVisible(false);    }    /**     * 响应按钮事件     */    private ActionListener handler = new ActionListener() {        @Override        public void actionPerformed(ActionEvent e) {            if (e.getSource() == btn_UserOne) {                userOneCount++;                updateUserMoney();            } else if (e.getSource() == btn_UserFive) {                userFiveCount++;                updateUserMoney();            } else if (e.getSource() == btn_Buy) {                // 购买                // 获取购买的商品总价,如果总价大于输入的钱数,提示用户继续投入钱                int needSum = getGoodSum();                if (needSum == 0) {                    JOptionPane.showMessageDialog(null,                            "购买失败,您没有选择任何商品,请选择需要的商品。");                    return;                } else if (needSum > userMoneySum) {                    JOptionPane.showMessageDialog(null, "投入的金额小于需要购买商品的总价,请再投入"                            + (needSum - userMoneySum) + "元或取消一些商品。");                    reinitGoodCount();                    return;                } else {                    // 计算需要找零的钱数                    int remain = userMoneySum - needSum;                    int remainFiveCount = remain / 5;                    int remainOneCount = remain % 5;                    // 修改系统钱数和计数                    int sysRemain = sysOneCount + userOneCount - remainOneCount;                    if (sysRemain < 0) {                        JOptionPane.showMessageDialog(null,                                "抱歉,系统中没有足够的零钱,请修改够买商品或退款后使用零钱购买。");                        return;                    }                    sysOneCount = sysRemain;                    sysFiveCount = sysFiveCount + userFiveCount                            - remainFiveCount;                    sysMoneySum = sysOneCount + sysFiveCount * 5;                    // 修改商品的总数                    updateGoodCount();                    // 输出购买的商品和找零的钱数                    String result = getBuyResult(remain);                    JOptionPane.showMessageDialog(null, result);                    // 设置初始状态                    reinit();                    pnlAdminUpdate();                }            } else if (e.getSource() == btn_Cancle) {                // 退款                updateUserMoney();                if (userMoneySum > 0) {                    String remainResult = getRemianResult();                    JOptionPane.showMessageDialog(null, remainResult);                    reinit();                } else {                    JOptionPane.showMessageDialog(null, "您投入的钱数为0,无法进行退款操作。");                }            } else if (e.getSource() == btn_Show) {                // 显示剩余商品数目                showGoodCount();            } else if (e.getSource() == btn_Admin) {                String input = JOptionPane.showInputDialog("请输入管理员密码,区分大小写。");                if (ADMIN.equals(input)) {                    // 登录成功,显示管理员面板                    pnlAdminUpdate();                    panel_Admin.setVisible(true);                    repaint();                } else {                    JOptionPane.showMessageDialog(null, "您输入的密码错误,无法进入管理员页面。");                }            } else if (e.getSource() == btn_Save) {                String oneStr = txt_SysOne.getText();                int one = Integer.parseInt(oneStr);                String fiveStr = txt_SysFive.getText();                int five = Integer.parseInt(fiveStr);                sysOneCount = one;                sysFiveCount = five;                sysMoneySum = sysOneCount + sysFiveCount * 5;                txt_SysSum.setText(sysMoneySum + "");                String cokeStr = txt_CountCoke.getText();                COUNT_COKE = Integer.parseInt(cokeStr);                String fantaStr = txt_CountFanta.getText();                COUNT_FANTA = Integer.parseInt(fantaStr);                String rteaStr = txt_CountRtea.getText();                COUNT_RTEA = Integer.parseInt(rteaStr);                String gteaStr = txt_CountGtea.getText();                COUNT_GTEA = Integer.parseInt(gteaStr);                int sysGoodSum = COUNT_COKE + COUNT_FANTA + COUNT_RTEA                        + COUNT_GTEA;                pnlAdminUpdate();                JOptionPane.showMessageDialog(null, "修改成功,系统中的总余额为:"                        + sysMoneySum + "元;总商品数量为:" + sysGoodSum + "。");            } else if (e.getSource() == btn_Logout) {                panel_Admin.setVisible(false);                repaint();            }        }    };    private JLabel lbl_SysGood;    private JTextField txt_SysGood;    private JLabel lbl_SysCoke;    private JTextField txt_CountCoke;    private JLabel lbl_SysFanta;    private JTextField txt_CountFanta;    private JLabel lbl_SysRtea;    private JTextField txt_CountRtea;    private JLabel lbl_SysGtea;    private JTextField txt_CountGtea;    private JTabbedPane tabbedPane;    private JPanel panel_AdminMoney;    private JPanel panel_AdminGood;    private JPanel panel_BtnAdmin;    /**     * 更新管理员面板信息     */    private void pnlAdminUpdate() {        txt_SysOne.setText(sysOneCount + "");        txt_SysFive.setText(sysFiveCount + "");        txt_SysSum.setText((sysOneCount + sysFiveCount * 5) + "");        txt_CountCoke.setText(COUNT_COKE + "");        txt_CountFanta.setText(COUNT_FANTA + "");        txt_CountRtea.setText(COUNT_RTEA + "");        txt_CountGtea.setText(COUNT_GTEA + "");        txt_SysGood                .setText((COUNT_COKE + COUNT_FANTA + COUNT_RTEA + COUNT_GTEA)                        + "");        gp_coke.setGoodCount(COUNT_COKE);        gp_fanta.setGoodCount(COUNT_FANTA);        gp_rtea.setGoodCount(COUNT_RTEA);        gp_gtea.setGoodCount(COUNT_GTEA);    }    /**     * 显示商品数量     */    protected void showGoodCount() {        gp_coke.showRemain();        gp_fanta.showRemain();        gp_rtea.showRemain();        gp_gtea.showRemain();    }    /**     * 获取退款结果     *      * @return     */    protected String getRemianResult() {        StringBuffer sb = new StringBuffer();        sb.append("退款成功。您取回");        if (userOneCount > 0) {            sb.append(userOneCount).append("个一元硬币;");        }        if (userFiveCount > 0) {            sb.append(userFiveCount).append("张五元纸币;");        }        return sb.toString();    }    /**     * 获取购买的商品描述     *      * @param remain     * @return     */    protected String getBuyResult(int remain) {        StringBuffer sb = new StringBuffer();        sb.append("购买成功。您购买了");        if (cokeCount > 0) {            sb.append(cokeCount).append("瓶").append(NAME_COKE).append(";");        }        if (fantaCount > 0) {            sb.append(fantaCount).append("瓶").append(NAME_FANTA).append(";");        }        if (rteaCount > 0) {            sb.append(rteaCount).append("瓶").append(NAME_RTEA).append(";");        }        if (gteaCount > 0) {            sb.append(gteaCount).append("瓶").append(NAME_GTEA).append(";");        }        if (remain >= 0) {            sb.append("并找您" + remain + "元。");        }        return sb.toString();    }    /**     * 更新商品数量     */    protected void updateGoodCount() {        if (cokeCount > 0) {            COUNT_COKE -= cokeCount;            gp_coke.setGoodCount(COUNT_COKE);            gp_coke.reInit();        }        if (fantaCount > 0) {            COUNT_FANTA -= fantaCount;            gp_fanta.setGoodCount(COUNT_FANTA);            gp_fanta.reInit();        }        if (rteaCount > 0) {            COUNT_RTEA -= rteaCount;            gp_rtea.setGoodCount(COUNT_RTEA);            gp_rtea.reInit();        }        if (gteaCount > 0) {            COUNT_GTEA -= gteaCount;            gp_gtea.setGoodCount(COUNT_GTEA);            gp_gtea.reInit();        }    }    /**     * 设置商品选中数为0,并初始化投入钱数等于0;     */    protected void reinit() {        reinitGoodCount();        userOneCount = 0;        userFiveCount = 0;        userMoneySum = 0;        txt_UserMoney.setText(0 + "");    }    /**     * 重新设置商品的选择数量为0     */    protected void reinitGoodCount() {        cokeCount = 0;        fantaCount = 0;        rteaCount = 0;        gteaCount = 0;    }    /**     * 获取购买商品需要的总钱数     *      * @return     */    protected int getGoodSum() {        int sum = 0;        if (gp_coke.isSelected()) {            cokeCount++;            sum += cokeCount * PRICE_COKE;        }        if (gp_fanta.isSelected()) {            fantaCount++;            sum += fantaCount * PRICE_FANTA;        }        if (gp_rtea.isSelected()) {            rteaCount++;            sum += rteaCount * PRICE_RTEA;        }        if (gp_gtea.isSelected()) {            gteaCount++;            sum += gteaCount * PRICE_GTEA;        }        return sum;    }    /**     * 更新用户投入的钱数     */    protected void updateUserMoney() {        userMoneySum = (userOneCount + userFiveCount * 5);        txt_UserMoney.setText(userMoneySum + "");    }    public static void main(String[] args) {        SwingUtilities.invokeLater(new Runnable() {            @Override            public void run() {                AutoSaleMachine asm = new AutoSaleMachine();                asm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                asm.setSize(500, 600);                asm.setResizable(false);                asm.setLocationRelativeTo(null);                asm.setVisible(true);            }        });    }}/** * @author th 商品面板,包含商品的图片和名称,以及是否选中的复选框 */class GoodPanel extends JPanel {    /**     *      */    private static final long serialVersionUID = 4364216395712746083L;    private static final String MONEY_UNIT = "元";    private static final String TIP = "单击图片可以选择购买或者取消购买";    private String picPath;    private String name;    private JTextField txt_Price;    private int price;    private JLabel lbl_Pic;    private JCheckBox cbox_Select;    private int goodId;    private int goodCount;    private JLabel lbl_Remain;    private JTextField txt_Remain;    GoodPanel(String picPath, int price, String name, int goodId, int goodCount) {        this.picPath = picPath;        this.price = price;        this.name = name;        this.goodId = goodId;        this.goodCount = goodCount;        this.setBorder(new CompoundBorder(null, new LineBorder(new Color(255,                0, 0), 2)));        this.setLayout(new BorderLayout(0, 0));        lbl_Pic = new JLabel();        this.add(lbl_Pic, BorderLayout.CENTER);        lbl_Pic.setToolTipText(TIP);        // 单击图片可以选择购买或者取消购买;        lbl_Pic.addMouseListener(mouseAdapter);        JPanel panel_Prop = new JPanel();        this.add(panel_Prop, BorderLayout.SOUTH);        JLabel lbl_Name = new JLabel(this.name);        panel_Prop.add(lbl_Name);        txt_Price = new JTextField();        txt_Price.setEditable(false);        panel_Prop.add(txt_Price);        txt_Price.setColumns(2);        txt_Price.setText(this.price + MONEY_UNIT);        cbox_Select = new JCheckBox("购买");        if (this.goodCount <= 0) {            cbox_Select.setSelected(false);        }        panel_Prop.add(cbox_Select);        lbl_Remain = new JLabel("剩余:");        panel_Prop.add(lbl_Remain);        txt_Remain = new JTextField();        txt_Remain.setEditable(false);        panel_Prop.add(txt_Remain);        txt_Remain.setColumns(2);        lbl_Remain.setVisible(false);        txt_Remain.setVisible(false);        initPic();    }    /**     * 图片的响应事件     */    MouseAdapter mouseAdapter = new MouseAdapter() {        @Override        public void mouseClicked(MouseEvent e) {            if (goodCount > 0)                cbox_Select.setSelected(!cbox_Select.isSelected());        }    };    /**     * 设置商品数量     *      * @param goodCount     */    public void setGoodCount(int goodCount) {        this.goodCount = goodCount;        if (this.goodCount <= 0) {            cbox_Select.setEnabled(false);        } else {            if (!cbox_Select.isEnabled()) {                cbox_Select.setEnabled(true);            }        }    }    /**     * 初始化显示的商品图片     */    private void initPic() {        if (this.picPath == null) {            return;        }        new Thread() {            @Override            public void run() {                int width = getWidth();                while (width == 0) {                    try {                        Thread.sleep(100);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    width = getWidth();                }                int height = getHeight();                File input = new File(picPath);                if (!input.exists()) {                    return;                }                try {                    BufferedImage image = ImageIO.read(input);                    ImageIcon bgIcon = new ImageIcon(image.getScaledInstance(                            width, height, Image.SCALE_DEFAULT));                    lbl_Pic.setIcon(bgIcon);                    repaint();                } catch (IOException e) {                    e.printStackTrace();                }            }        }.start();    }    /**     * 是否购买该商品     *      * @return     */    public boolean isSelected() {        return cbox_Select.isSelected();    }    /**     * 获取商品编号     *      * @return     */    public int getGoodId() {        return goodId;    }    /**     * 购买完毕后需要重置     */    public void reInit() {        cbox_Select.setSelected(false);    }    /**     * 显示商品余量     */    public void showRemain() {        new Thread() {            @Override            public void run() {                lbl_Remain.setVisible(true);                txt_Remain.setText(goodCount + "");                txt_Remain.setVisible(true);                repaint();                try {                    Thread.sleep(5000);                    lbl_Remain.setVisible(false);                    txt_Remain.setVisible(false);                    repaint();                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }.start();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613
  • 614
  • 615
  • 616
  • 617
  • 618
  • 619
  • 620
  • 621
  • 622
  • 623
  • 624
  • 625
  • 626
  • 627
  • 628
  • 629
  • 630
  • 631
  • 632
  • 633
  • 634
  • 635
  • 636
  • 637
  • 638
  • 639
  • 640
  • 641
  • 642
  • 643
  • 644
  • 645
  • 646
  • 647
  • 648
  • 649
  • 650
  • 651
  • 652
  • 653
  • 654
  • 655
  • 656
  • 657
  • 658
  • 659
  • 660
  • 661
  • 662
  • 663
  • 664
  • 665
  • 666
  • 667
  • 668
  • 669
  • 670
  • 671
  • 672
  • 673
  • 674
  • 675
  • 676
  • 677
  • 678
  • 679
  • 680
  • 681
  • 682
  • 683
  • 684
  • 685
  • 686
  • 687
  • 688
  • 689
  • 690
  • 691
  • 692
  • 693
  • 694
  • 695
  • 696
  • 697
  • 698
  • 699
  • 700
  • 701
  • 702
  • 703
  • 704
  • 705
  • 706
  • 707
  • 708
  • 709
  • 710
  • 711
  • 712
  • 713
  • 714
  • 715
  • 716
  • 717
  • 718
  • 719
  • 720
  • 721
  • 722
  • 723
  • 724
  • 725
  • 726
  • 727
  • 728
  • 729
  • 730
  • 731
  • 732
  • 733
  • 734
  • 735
  • 736
  • 737
  • 738
  • 739
  • 740
  • 741
  • 742
  • 743
  • 744
  • 745
  • 746
  • 747
  • 748
  • 749
  • 750
  • 751
  • 752
  • 753
  • 754
  • 755
  • 756
  • 757
  • 758
  • 759
  • 760
  • 761
  • 762
  • 763
  • 764
  • 765
  • 766
  • 767
  • 768
  • 769
  • 770
  • 771
  • 772
  • 773
  • 774
  • 775
  • 776
  • 777
  • 778
  • 779
  • 780
  • 781
  • 782
  • 783
  • 784
  • 785
  • 786
  • 787
  • 788
  • 789
  • 790
  • 791
  • 792
  • 793
  • 794
  • 795
  • 796
  • 797
  • 798
  • 799
  • 800
  • 801
  • 802
  • 803
  • 804
  • 805
  • 806
  • 807
  • 808
  • 809
  • 810
  • 811
  • 812
  • 813
  • 814
  • 815
  • 816
  • 817
  • 818
  • 819
  • 820
  • 821
  • 822
  • 823
  • 824
  • 825
  • 826
  • 827
  • 828
  • 829
  • 830
  • 831
  • 832
  • 833
  • 834
  • 835
  • 836
  • 837
  • 838
  • 839
  • 840
  • 841
  • 842
  • 843
  • 844
  • 845
  • 846
  • 847
  • 848
  • 849
  • 850
  • 851
  • 852
  • 853
  • 854
  • 855
  • 856
  • 857
  • 858
  • 859
  • 860
  • 861
  • 862
  • 863
  • 864
  • 865
  • 866
  • 867
  • 868
  • 869
  • 870
  • 871
  • 872
  • 873
  • 874
  • 875
  • 876
  • 877
  • 878
  • 879
  • 880
  • 881
  • 882
  • 883
  • 884
  • 885
  • 886
  • 887
  • 888
  • 889
  • 890
  • 891
  • 892
  • 893
  • 894
  • 895
  • 896
  • 897
  • 898
  • 899
  • 900
  • 901
  • 902
  • 903
  • 904
  • 905
  • 906
  • 907
  • 908
  • 909
  • 910
  • 911
  • 912
  • 913
  • 914
  • 915
  • 916
  • 917
  • 918
  • 919
  • 920
  • 921
  • 922
  • 923
  • 924
  • 925
  • 926
  • 927
  • 928
  • 929

功能截图:

启动界面:

这里写图片描述

购买成功:

这里写图片描述

退款:

这里写图片描述

查看货物数量:

这里写图片描述

登录管理员界面:

这里写图片描述

管理员界面:

这里写图片描述

修改系统Money数量:

这里写图片描述

修改系统商品数量:

这里写图片描述

原创粉丝点击