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

第4次作业类测试代码+033+王泓泽

时间:2017-05-01 22:12:29      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:tla   cto   tab   exception   err   销售额   swing   option   ext   

一、类图

技术分享

技术分享

二、代码

package test2;

import java.awt.EventQueue;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
 
public class Yongjin02 {
    private JFrame frame;
    private JTextField textField_headphone;
    private JTextField textField_shell;
    private JTextField textField_protector;
    private JTextField textField_return;
    private JTextField textField_max;
    private JTextField textField_diff;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Yongjin02 window = new Yongjin02();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
 
    public Yongjin02() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setTitle("佣金计算程序");
        frame.getContentPane().setBackground(SystemColor.menu);
        frame.setBounds(400, 300, 600, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
         
        JLabel lbl = new JLabel("请输入销售数量:");
        lbl.setBounds(10, 10, 131, 15);
        frame.getContentPane().add(lbl);
         
        JLabel label = new JLabel("耳机:");
        label.setBounds(10, 51, 46, 15);
        frame.getContentPane().add(label);
         
        JLabel label2 = new JLabel("手机壳:");
        label2.setBounds(142, 51, 57, 15);
        frame.getContentPane().add(label2);
         
        JLabel label3 = new JLabel("贴膜:");
        label3.setBounds(285, 51, 57, 15);
        frame.getContentPane().add(label3);
         
        textField_headphone = new JTextField();
        textField_headphone.setBounds(50, 48, 66, 21);
        frame.getContentPane().add(textField_headphone);
        textField_headphone.setColumns(10);
         
        textField_shell = new JTextField();
        textField_shell.setBounds(198, 48, 66, 21);
        frame.getContentPane().add(textField_shell);
        textField_shell.setColumns(10);
         
        textField_protector = new JTextField();
        textField_protector.setBounds(326, 48, 66, 21);
        frame.getContentPane().add(textField_protector);
        textField_protector.setColumns(10);
         
        JLabel label_2 = new JLabel("应返回的佣金:");
        label_2.setBounds(10, 132, 106, 15);
        frame.getContentPane().add(label_2);
         
        textField_return = new JTextField();
        textField_return.setEditable(false);
        textField_return.setBounds(198, 129, 194, 21);
        frame.getContentPane().add(textField_return);
        textField_return.setColumns(10);
         
        JLabel label_3 = new JLabel("销售额最高的配件:");
        label_3.setBounds(10, 165, 131, 15);
        frame.getContentPane().add(label_3);
         
        JLabel label_4 = new JLabel("销售配件最多与最少数量相差:");
        label_4.setBounds(10, 198, 189, 15);
        frame.getContentPane().add(label_4);
         
        textField_max = new JTextField();
        textField_max.setEditable(false);
        textField_max.setBounds(198, 162, 194, 21);
        frame.getContentPane().add(textField_max);
        textField_max.setColumns(10);
         
        textField_diff = new JTextField();
        textField_diff.setEditable(false);
        textField_diff.setBounds(198, 195, 194, 21);
        frame.getContentPane().add(textField_diff);
        textField_diff.setColumns(10);
         
        JButton btnOk = new JButton("OK");
        btnOk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int headphone = Commission.changeToInt(textField_headphone.getText());
                if(headphone < 0) {
                    JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                int protector=Commission.changeToInt(textField_protector.getText());
                if(protector < 0) {
                    JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                int shell=Commission.changeToInt(textField_shell.getText());
                if(shell < 0) {
                    JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                float returnAns=Commission.commission(headphone, shell, protector);
                String returnString=String.format("佣金金额:%.2f元", returnAns);
                textField_return.setText(returnString);
                String maxAns=Commission.mostSale(headphone, shell, protector);
                textField_max.setText(maxAns);
                int differenceAns=Commission.diffSale(headphone, shell, protector);
                String differenceString=String.format("%d", differenceAns);
                textField_diff.setText(differenceString);
            }
        });
        btnOk.setBounds(10, 87, 93, 23);
        frame.getContentPane().add(btnOk);
         
        JButton btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField_headphone.setText("");
                textField_protector.setText("");
                textField_shell.setText("");
                textField_return.setText("");
                textField_max.setText("");
                textField_diff.setText("");
            }
        });
        btnCancel.setBounds(299, 87, 93, 23);
        frame.getContentPane().add(btnCancel);
    }
}
package test2;

public class Commission{
    public static int changeToInt(String number){
        int ans=0;
        try{
            ans = Integer.parseInt(number);
        }
        catch (Exception e){
            ans=-1;
        }
        return ans;
    }
      
    //计算佣金
    public static float commission(int headphone, int shell, int protector){
        if(headphone < 0 || shell < 0 || protector < 0){
            return -1.0f;
        }
          
        float ans = 0.0f;
        long sum = 80 * headphone + 10 * shell + 8 * protector;
          
        if(sum < 1000){
            ans = sum * 0.1f;
        }
        else if(sum >= 1000 && sum <= 1800){
            ans = 100.0f + (sum - 1000) * 0.15f;
        }
        else{
            ans = (sum - 1800.0f) * 0.2f + 220.0f;
        }
          
        return ans;
    }
     
    //销售额最高的配件
    public static String mostSale(int headphone, int shell, int protector){
        long headphoneSales = headphone * 80;
        long shellSales = shell * 10;
        long protectorSales = protector * 8;
        String ans="";
        if(headphoneSales >= shellSales && headphoneSales >= protectorSales){
            ans+="耳机";
        }
        if(shellSales >= headphoneSales && shellSales >= protectorSales){
            if(ans.equals("")){
                ans+="手机壳";
            }
            else{
                ans+="和手机壳";
            }
        }
        if(protectorSales >= headphoneSales && protectorSales >= shellSales){
            if(ans.equals("")){
                ans+="贴膜";
            }
            else{
                ans+="和贴膜";
            }
        }
        return ans;
    }
     
    //销售配件最多与最少数量相差:
    public static int diffSale(int headphone, int shell, int protector){
        int max=Math.max(Math.max(headphone, shell), protector);
        int min=Math.min(Math.min(headphone, shell), protector);
        return max-min;
    }
}

三、结果

技术分享

 

技术分享

技术分享

技术分享

四、总结

第一次写带界面UI的java程序,还有很多不熟悉,比如输入错了后没有清空数据,但是主要功能还是没什么问题的。

 

第4次作业类测试代码+033+王泓泽

标签:tla   cto   tab   exception   err   销售额   swing   option   ext   

原文地址:http://www.cnblogs.com/k511959373/p/6792729.html

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