找零钱最佳组合

假设商店货品价格(R) 都不大于100元(且为整数),若顾客付款(P)100元内,现有一个程序能在每位顾客付款后给出找零钱的最佳组合(找给顾客货币张数最少)。 假定此商店的货币面值只包括:50(N50)10(N10) 5(N5)1(N1) 四种。

   请结合等价类划分法和边界值分析法为上述程序设计 出相应的测试用例。

实验步骤:

① 设计程序

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

public class ChangeCalculator {

    public static void main(String[] args) {

        // 创建主窗口

        JFrame frame = new JFrame("找零计算器");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(400, 200);

        

        // 创建面板

        JPanel panel = new JPanel();

        frame.add(panel);

        placeComponents(panel);

        

        // 设置窗口可见

        frame.setVisible(true);

    }

 

    private static void placeComponents(JPanel panel) {

        panel.setLayout(null);

 

        // 创建标签和文本框

        JLabel priceLabel = new JLabel("请输入商品价格(1-100元):");

        priceLabel.setBounds(10, 20, 300, 25);

        panel.add(priceLabel);

 

        JTextField priceInput = new JTextField(20);

        priceInput.setBounds(10, 50, 165, 25);

        panel.add(priceInput);

 

        JLabel paymentLabel = new JLabel("请输入付款金额(1-100元):");

        paymentLabel.setBounds(10, 80, 300, 25);

        panel.add(paymentLabel);

 

        JTextField paymentInput = new JTextField(20);

        paymentInput.setBounds(10, 110, 165, 25);

        panel.add(paymentInput);

 

        // 创建按钮

        JButton btnCalculate = new JButton("计算找零");

        btnCalculate.setBounds(190, 50, 100, 25);

        panel.add(btnCalculate);

 

        JButton btnReset = new JButton("重置");

        btnReset.setBounds(190, 110, 100, 25);

        panel.add(btnReset);

 

        // 创建标签用于显示结果

        JLabel resultLabel = new JLabel();

        resultLabel.setBounds(10, 140, 370, 25);

        panel.add(resultLabel);

 

        // 添加按钮点击事件

        btnCalculate.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                try {

                    int R = Integer.parseInt(priceInput.getText());

                    int P = Integer.parseInt(paymentInput.getText());

                    String result = calculateChange(R, P);

                    resultLabel.setText(result);

                } catch (NumberFormatException ex) {

                    resultLabel.setText("无效输入:请输入整数");

                }

            }

        });

 

        btnReset.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                priceInput.setText("");

                paymentInput.setText("");

                resultLabel.setText("");

            }

        });

    }

 

    private static String calculateChange(int R, int P) {

        // 检查输入是否有效

        if (R <= 0 || P <= 0 || R > 100 || P > 100 || P < R) {

            return "无效输入:价格和付款必须为整数且在1-100元之间,付款金额不能小于价格";

        }

 

        // 计算找零金额

        int C = P - R;

        if (C == 0) {

            return "无需找零";

        }

 

        // 计算最佳找零组合

        int N50 = C / 50;

        C %= 50;

        int N10 = C / 10;

        C %= 10;

        int N5 = C / 5;

        C %= 5;

        int N1 = C;

 

        return String.format("找零组合:%d 张 50 元,%d 张 10 元,%d 张 5 元,%d 张 1 元", N50, N10, N5, N1);

    }

}

 

① 划分等价类,得到等价类表。等价类表格式如下:

输入条件

有效等价类

唯一标识

无效等价类

唯一标识

价格 RR

0<R≤1000<R≤100 且为整数

E1

R≤0R≤0 或 R>100R>100

I1

 

 

 

RR 不是整数

I2

付款 PP

0<P≤1000<P≤100 且为整数

E2

P≤0P≤0 或 P>100P>100

I3

 

 

 

PP 不是整数

I4

付款与价格关系

P≥RPR

E3

P<RP<R

I5

 

② 运用等价类划分法设计测试用例,得到测试用例表。测试用例表格式如下:

 

序号

输入数据

覆盖等价类

输出

1

R=50,P=100R=50,P=100

E1, E2, E3

找零组合:1 张 50 元,0 张 10 元,0 张 5 元,0 张 1 元

2

R=23,P=50R=23,P=50

E1, E2, E3

找零组合:0 张 50 元,2 张 10 元,1 张 5 元,2 张 1 元

3

R=99,P=100R=99,P=100

E1, E2, E3

找零组合:0 张 50 元,0 张 10 元,0 张 5 元,1 张 1 元

4

R=1,P=100R=1,P=100

E1, E2, E3

找零组合:1 张 50 元,4 张 10 元,1 张 5 元,4 张 1 元

5

R=0,P=50R=0,P=50

I1

无效输入:价格和付款必须大于 0

6

R=101,P=100R=101,P=100

I1

无效输入:价格和付款不能超过 100 元

7

R=50,P=49R=50,P=49

I5

无效输入:付款金额不能小于价格

8

R=50.5,P=100R=50.5,P=100

I2

无效输入:价格和付款必须为整数

9

R=50,P=100.5R=50,P=100.5

I4

无效输入:价格和付款必须为整数

 

③ 运用边界值法设计测试用例。

序号

输入数据

覆盖边界值

输出

1

R=1,P=1R=1,P=1

RR 和 PP 的最小值

无需找零

2

R=100,P=100R=100,P=100

RR 和 PP 的最大值

无需找零

3

R=1,P=100R=1,P=100

RR 最小值,PP 最大值

找零组合:1 张 50 元,4 张 10 元,1 张 5 元,4 张 1 元

4

R=99,P=100R=99,P=100

RR 接近最大值,PP 最大值

找零组合:0 张 50 元,0 张 10 元,0 张 5 元,1 张 1 元

5

R=50,P=50R=50,P=50

R=PR=P 的情况

无需找零

6

R=0,P=0R=0,P=0

无效边界值

无效输入:价格和付款必须大于 0

7

R=101,P=101R=101,P=101

无效边界值

无效输入:价格和付款不能超过 100 元

posted @ 2025-03-24 08:09  不会JAVA的小袁  阅读(41)  评论(0)    收藏  举报