3.14饮料自动售货机

实现步骤

  1. 创建VendingMachine

  2. 实现饮料选择逻辑

实现代码(Java Swing)

java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class VendingMachineGUI extends JFrame {
    private JButton orangeJuiceButton;
    private JButton beerButton;
    private JButton coin5Button;
    private JButton coin10Button;
    private JTextArea displayArea;
    private int currentCoin = 0;

    public VendingMachineGUI() {
        setTitle("饮料自动售货机");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout(10, 10));

        // 投币面板
        JPanel coinPanel = new JPanel(new GridLayout(1, 2, 5, 5));
        coin5Button = new JButton("投入5角");
        coin10Button = new JButton("投入1元");
        coinPanel.add(coin5Button);
        coinPanel.add(coin10Button);

        // 饮料选择面板
        JPanel drinkPanel = new JPanel(new GridLayout(1, 2, 5, 5));
        orangeJuiceButton = new JButton("橙汁");
        beerButton = new JButton("啤酒");
        drinkPanel.add(orangeJuiceButton);
        drinkPanel.add(beerButton);

        // 显示区域
        displayArea = new JTextArea();
        displayArea.setEditable(false);
        displayArea.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        JScrollPane scrollPane = new JScrollPane(displayArea);

        // 状态标签
        JLabel stateLabel = new JLabel("当前投币: 0角", JLabel.CENTER);
        stateLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));

        // 添加组件
        add(coinPanel, BorderLayout.NORTH);
        add(drinkPanel, BorderLayout.CENTER);
        add(scrollPane, BorderLayout.SOUTH);
        add(stateLabel, BorderLayout.WEST);

        // 添加事件监听器
        coin5Button.addActionListener(e -> {
            currentCoin += 5;
            updateStateLabel(stateLabel);
            displayArea.append("已投入5角钱\n");
        });

        coin10Button.addActionListener(e -> {
            currentCoin += 10;
            updateStateLabel(stateLabel);
            displayArea.append("已投入1元钱\n");
        });

        orangeJuiceButton.addActionListener(e -> selectDrink("橙汁"));
        beerButton.addActionListener(e -> selectDrink("啤酒"));

        setVisible(true);
    }

    private void updateStateLabel(JLabel label) {
        label.setText("当前投币: " + currentCoin + "角");
    }

    private void selectDrink(String drink) {
        if (currentCoin < 5) {
            displayArea.append("投币不足,请至少投入5角钱\n");
            return;
        }

        if (currentCoin == 5) {
            displayArea.append("已选择" + drink + ",请取用\n");
            currentCoin = 0;
        } else {
            displayArea.append("已选择" + drink + ",请取用并找回5角钱\n");
            currentCoin = 0;
        }
        
        // 显示饮料图片
        showDrinkImage(drink);
    }

    private void showDrinkImage(String drink) {
        JFrame imageFrame = new JFrame(drink);
        imageFrame.setSize(200, 300);
        
        JLabel imageLabel = new JLabel();
        imageLabel.setHorizontalAlignment(JLabel.CENTER);
        
        if (drink.equals("橙汁")) {
            imageLabel.setIcon(new ImageIcon("orange_juice.png")); // 替换为实际图片路径
            imageLabel.setText("🍊 橙汁"); // 如果没有图片,使用emoji
        } else {
            imageLabel.setIcon(new ImageIcon("beer.png")); // 替换为实际图片路径
            imageLabel.setText("🍺 啤酒"); // 如果没有图片,使用emoji
        }
        
        imageFrame.add(imageLabel);
        imageFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new VendingMachineGUI());
    }
}

运行结果示例

饮料自动售货机界面

  1. 点击"投入5角"或"投入1元"按钮投币

  2. 当前投币金额显示在左侧

  3. 选择"橙汁"或"啤酒"按钮

  4. 结果显示在下方文本区域

  5. 弹出窗口显示所选饮料的图片或emoji表示

因果图测试用例

  1. 投入5角,选择橙汁 → 出橙汁

  2. 投入5角,选择啤酒 → 出啤酒

  3. 投入1元,选择橙汁 → 出橙汁+退5角

  4. 投入1元,选择啤酒 → 出啤酒+退5角

  5. 投入其他金额 → 提示错误

  6. 选择其他选项 → 提示错误

posted @ 2025-03-27 09:29    阅读(17)  评论(0)    收藏  举报