首页 > 代码库 > 第四次作业类测试代码+108+曾宏宇

第四次作业类测试代码+108+曾宏宇

1.类图

技术分享

 

2.代码

  常量定义:

private final static int HP_PRICE=80;
private final static int SHELL_PRICE=10;
private final static int PROTECTOR_PRICE=8;

  方法:

 commission(int headphone,int shell,int protector)

public static double commission(int headphone,int shell,int protector){
        double result=headphone*HP_PRICE+shell*SHELL_PRICE+protector*PROTECTOR_PRICE;
        if(result<1000)
        {
            return result*0.1;    //小于1000
        }
        else if(result>=1000&&result<=1800)
        {
            return 1000*0.1+(result-1000)*0.15;    //大于1000且小于1800
        }
        else
        {
            return 1000*0.1+800*0.15+(result-1800)*0.2;    //大于1800
        }
    }

  mostSale(int headphone,int shell,int protector)

    public static String mostSale(int headphone,int shell,int protector){
        String mostSale="";
        int temp=0;
        if(headphone>shell){
            temp=headphone;
            mostSale="耳机";
        }
        else{
            temp=shell;
            mostSale="手机壳";
        }
        if(temp<protector){
            temp=protector;
            mostSale="保护膜";
        }
        return mostSale;
    }

  diffSale(int headphone,int shell,int protector)

public static int diffSale(int headphone,int shell,int protector){
        int sales[]={headphone,shell,protector};
        Arrays.sort(sales);
        int diffSale=sales[sales.length-1]-sales[0];
        return diffSale;
    }

  图形化界面(事件处理)

  “ok”点击事件:

btn_ok.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String hp=tf_hp.getText();
                String shell=tf_shell.getText();
                String protector=tf_protector.getText();
                try {
                    int num_hp=Integer.parseInt(hp);
                    int num_shell=Integer.parseInt(shell);
                    int num_protector=Integer.parseInt(protector);

                    tf_com.setText(Tool.commission(num_hp, num_shell, num_protector)+"");
                    tf_ms.setText(Tool.mostSale(num_hp, num_shell, num_protector));
                    tf_ds.setText(Tool.diffSale(num_hp, num_shell, num_protector)+"");
                    
                } catch (Exception e2) {
                    JOptionPane.showMessageDialog(null, "输入有误,请重新输入!");
                    tf_hp.setText("");
                    tf_shell.setText("");
                    tf_protector.setText("");
                }
            }
        });

  “cancel”点击事件

btn_cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tf_hp.setText("");
                tf_shell.setText("");
                tf_protector.setText("");
            }
        });

3.运行界面

  输入合法:

技术分享

  输入不合法

 技术分享

 

第四次作业类测试代码+108+曾宏宇