1. 程式人生 > >JAVA模擬自動售貨機

JAVA模擬自動售貨機

版本說明

  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 {