码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-05-01 20:53:07      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:static   arrays   span   分享   .so   comm   png   new   amp   

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+曾宏宇

标签:static   arrays   span   分享   .so   comm   png   new   amp   

原文地址:http://www.cnblogs.com/zenghongyu/p/6792534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!