第4次作业类测试代码+105032014099+吴丹丹

一、类图

 

二、界面和相应的功能

输入不满足要求时,弹出提示框 

当三种配件的销售额一样时,显示三种配件销售额相同

三、代码

  1 package test04;
  2 import java.awt.Frame;
  3 import java.awt.Color;
  4 import java.awt.Font;
  5 import java.awt.event.ActionEvent;
  6 import java.awt.event.ActionListener;
  7 import java.text.DecimalFormat;
  8 
  9 import javax.swing.JButton;
 10 import javax.swing.JFrame;
 11 import javax.swing.JLabel;
 12 import javax.swing.JOptionPane;
 13 import javax.swing.JTextField;
 14 
 15 public class MyJFrame{
 16 
 17     public static void main(String[] args) {
 18         JFrame f=new JFrame("手机配件佣金计算程序");    //实例化窗体对象
 19         
 20         //创建标签对象
 21         JLabel input = new JLabel("请输入销售的数量:");
 22         JLabel headphone = new JLabel("耳机:");
 23         JLabel shell = new JLabel("手机壳:");
 24         JLabel protector  = new JLabel("贴膜:");
 25         JLabel output1 = new JLabel("应返还的佣金:");
 26         JLabel output2 = new JLabel("销售额最高的配件是:");
 27         JLabel output3 = new JLabel("销售配件最多与最少数量相差:");
 28         
 29         //创建单行输入文本
 30         JTextField txt_headphone = new JTextField();
 31         JTextField txt_shell = new JTextField();
 32         JTextField txt_protector = new JTextField();
 33         JTextField txt_output1 = new JTextField();
 34         JTextField txt_output2 = new JTextField();
 35         JTextField txt_output3 = new JTextField();
 36         
 37         //创建按钮
 38         JButton btn_ok = new JButton("OK");
 39         JButton btn_cancel = new JButton("Cancel");
 40         
 41         //按钮监听事件
 42         btn_ok.addActionListener(new ActionListener() {
 43             public void actionPerformed(ActionEvent e) {
 44                 if (e.getSource()==btn_ok) {
 45                     String headphoneN = txt_headphone.getText();
 46                     String shellN = txt_shell.getText();
 47                     String protectorN = txt_protector.getText();
 48                     Commission commission = new Commission();
 49                     
 50                     if(commission.isNumeric(headphoneN)&&commission.isNumeric(shellN)&&commission.isNumeric(protectorN))
 51                     {
 52                         DecimalFormat df=new DecimalFormat("#.0"); //限制输出佣金的小数点后的位数
 53                         
 54                         //转化为整型
 55                         int headphone = Integer.parseInt(headphoneN);
 56                         int shell = Integer.parseInt(shellN);
 57                         int protector = Integer.parseInt(protectorN);
 58                         
 59                         //计算结果
 60                         double output1 = commission.commission(headphone, shell, protector);
 61                         String output2 = commission.mostSale(headphone, shell, protector);
 62                         int output3 = commission.diffSale(headphone, shell, protector);
 63                         //输出结果
 64                         txt_output1.setText(String.valueOf(df.format(output1)));
 65                         txt_output2.setText(output2);
 66                         txt_output3.setText(String.valueOf(output3));
 67                     }else
 68                     {
 69                         JOptionPane.showMessageDialog(null, "输入有误,请重新输入!");
 70                         txt_headphone.setText("");
 71                         txt_shell.setText("");
 72                         txt_protector.setText("");
 73                     }
 74                 }
 75             }
 76         });
 77         //Cancel按钮
 78         btn_cancel.addActionListener(new ActionListener() {
 79             public void actionPerformed(ActionEvent e) {
 80                 txt_headphone.setText("");
 81                 txt_shell.setText("");
 82                 txt_protector.setText("");
 83                 txt_output1.setText("");
 84                 txt_output2.setText("");
 85                 txt_output3.setText("");
 86             } 
 87         });
 88         
 89         //设置字体
 90         Font font = new Font("宋体", Font.BOLD, 12);
 91         Font font2 =new Font("宋体", Font.BOLD, 10);
 92         input.setFont(font);
 93         headphone.setFont(font);
 94         shell.setFont(font);
 95         protector.setFont(font);
 96         output1.setFont(font);
 97         output2.setFont(font);
 98         output3.setFont(font);
 99         txt_output1.setFont(font2);
100         txt_output2.setFont(font2);
101         txt_output3.setFont(font2);
102         
103         //设置颜色
104         txt_output1.setSelectedTextColor(Color.black);
105         txt_output2.setSelectedTextColor(Color.black);
106         txt_output3.setSelectedTextColor(Color.black);
107         
108         //设置输出文本框不可编辑
109         txt_output1.setEnabled(false);
110         txt_output2.setEnabled(false);
111         txt_output3.setEnabled(false);
112         
113         //设置组件位置及大小
114         input.setBounds(13, -3, 186, 50);
115         headphone.setBounds(14, 55, 54, 15);    
116         shell.setBounds(137, 55, 54, 15);
117         protector.setBounds(276, 55, 54, 15);
118         output1.setBounds(18, 138, 115, 23);
119         output2.setBounds(17, 179, 153, 23);
120         output3.setBounds(17, 217, 241, 22);
121         txt_headphone.setBounds(54, 54, 66, 21);
122         txt_shell.setBounds(190, 54, 66, 21);
123         txt_protector.setBounds(321, 52, 66, 21);
124         txt_output1.setBounds(120, 140, 139, 23);
125         txt_output2.setBounds(158, 177, 139, 23);
126         txt_output3.setBounds(215, 217, 139, 23);
127         btn_ok.setBounds(83, 93, 93, 23);
128         btn_cancel.setBounds(215, 93, 93, 23);
129         
130         //添加组件
131         f.add(input);
132         f.add(headphone);
133         f.add(shell);
134         f.add(protector);
135         f.add(btn_ok);
136         f.add(btn_cancel);
137         f.add(output1);
138         f.add(output2);
139         f.add(output3);
140         f.add(txt_headphone);
141         f.add(txt_shell);
142         f.add(txt_protector);
143         f.add(txt_output1);
144         f.add(txt_output2);
145         f.add(txt_output3);
146         
147         //设置窗体
148         f.setLayout(null);
149         f.setBackground(Color.WHITE);
150         f.setSize(450, 300);     
151         f.setLocation(300, 200);
152         f.setVisible(true);    
153     }
154 }
View Code
 1 package test04;
 2 
 3 public class Commission {
 4     //判断输入是否合法
 5     public final static boolean isNumeric(String s) { 
 6         if("".equals(s)||!(s.matches("^[0-9]*$"))||s.length()>8||s==null)
 7         {
 8         return false;
 9         }
10         return true;
11         }
12     
13     //计算佣金
14     public double commission(int headphone,int shell,int protector)
15     {   
16         double commission=0;     //初始化佣金为0
17         int headphone_price=80;  //耳机单价80;
18         int shell_price=10;      //手机壳单价10;
19         int protector_price=8;   //手机贴膜单价8;
20         int total=headphone*headphone_price+shell*shell_price+protector*protector_price;
21         
22         if(total<1000&&total>0){            //销售额不足1000提取10%佣金
23             commission=total*0.1;
24         }else if(total<=1800){              //销售额在1000-1800,提取15%佣金
25             commission=total*0.15;
26         }else {                             //销售额大于1800提起20%佣金   
27         commission=total*0.2;
28         }
29         return commission;
30     }
31 
32     //销售额最高的配件
33     public String mostSale(int headphone,int shell,int protector)
34     {
35         int headphoneSale = headphone*80;
36         int shellSale = shell*10;
37         int protectorSale = protector*8;
38         
39         String result="";
40         int max=Math.max(Math.max(headphoneSale, shellSale), protectorSale);
41         if (max==headphoneSale) 
42         {
43             result = "耳机";        
44         }
45         if (max==shellSale) 
46         {
47             result = "手机壳";
48         }
49         if (max==protectorSale) 
50         {
51             result = "贴膜";
52         }
53         if (max==headphoneSale&&max==shellSale) 
54         {
55             result = "耳机,手机壳";
56         }
57         if (max==headphoneSale&&max==protectorSale) 
58         {
59             result = "耳机,贴膜";
60         }
61         if (max==shellSale&&max==protectorSale) 
62         {
63             result = "手机壳,贴膜";
64         }
65         if (max==headphoneSale&&max==shellSale&&max==protectorSale) {
66             result = "三种配件销售额相同";        
67         }
68         return result;
69     }
70         
71     //计算销售配件最多与最少之差
72     public int diffSale(int headphone, int shell, int protector)
73     {
74         int m=Math.max(Math.max(headphone, shell), protector); 
75         int n=Math.min(Math.min(headphone, shell), protector); 
76         return m-n;         
77     }
78 }
79                 
View Code

 

posted @ 2017-05-03 17:11  Esperer`  阅读(285)  评论(0编辑  收藏  举报