1.类图

2.主要界面

当输入为空,输入非数值型数据时,程序会弹出错误信息,并要求重新输入:

正常情况下的输出结果为:

3.代码如下

 1 import java.util.Arrays;
 2
 3 public class Commission {
 4
 5     //判断输入是否有效
 6     public boolean validate(String  num){
 7         if ("".equals(num)||num==null||!(num.matches("^[0-9]*$"))) {
 8             return false;
 9         }
10         return true;
11     }
12
13     // 计算佣金
14     public float commission(int headphone, int shell, int protector) {
15         float commission = 0;// 佣金
16         float headphoneSale = headphone * 80;
17         float shellSale = shell * 10;
18         float protectorSale = protector * 8;
19         float totalSale = headphoneSale + shellSale + protectorSale;
20         if (totalSale < 1000 && totalSale > 0) {
21             commission = totalSale * 0.1f;
22
23         } else if (totalSale <= 1800) {
24             commission = totalSale * 0.15f;
25         } else {
26             commission = totalSale * 0.2f;
27
28         }
29         return commission;
30     }
31
32     // 销售额最高的配件
33     public String mostSale(int headphone, int shell, int protector) {
34         float headphoneSale = headphone * 80;
35         float shellSale = shell * 10;
36         float protectorSale = protector * 8;
37         float max = Math.max(Math.max(headphoneSale, shellSale), protectorSale);
38         System.out.println(max);
39         if (max==shellSale&&max==headphoneSale&&max==protectorSale) {
40             return "三种配件销售相同";
41         }
42         if (max==shellSale&&max==headphoneSale) {
43             return "手机,手机壳";
44         }
45         if (max==headphoneSale&&max==protectorSale) {
46             return "手机,手机膜";
47         }
48         if (max==shellSale&&max==protectorSale) {
49             return "手机壳,手机膜";
50         }
51         if (max==headphoneSale) {
52             return "手机";
53         }
54         if (max==shellSale) {
55             return "手机壳";
56         }
57         if (max == protectorSale) {
58             return "手机膜";
59         }
60         return null;
61
62
63     }
64
65     //销售最多与最少的数量差
66     public  int diffSale(int headphone, int shell, int protector){
67          int arrays[] = {headphone,shell,protector};
68          int result = 0;//结果
69          Arrays.sort(arrays);
70          System.out.println(arrays[2]);
71          System.out.println(arrays[0]);
72          result = arrays[2]-arrays[0];
73          return result;
74
75      }
76
77 }

View Code

  1 import java.awt.Color;
  2 import java.awt.Font;
  3 import java.awt.event.ActionEvent;
  4 import java.awt.event.ActionListener;
  5
  6 import javax.swing.JButton;
  7 import javax.swing.JFrame;
  8 import javax.swing.JLabel;
  9 import javax.swing.JOptionPane;
 10 import javax.swing.JTextField;
 11
 12 public class CommissionPanel {
 13     JFrame jFrame = new JFrame("佣金计算程序");
 14     // 标签
 15     JLabel titleLab = new JLabel("手机配件佣金计算程序");
 16     JLabel inputLab = new JLabel("请输入销售的数量:");
 17     JLabel headphoneLab = new JLabel("手机:");
 18     JLabel shellLab = new JLabel("手机壳:");
 19     JLabel protectorLab = new JLabel("贴膜:");
 20     JLabel resultLab = new JLabel("应返还的佣金:");
 21     JLabel result2Lab = new JLabel("销售额最高的配件是:");
 22     JLabel result3Lab = new JLabel("销售配件最多与最少数量相差:");
 23
 24     // 按钮
 25     JButton submit = new JButton("OK");
 26     JButton reset = new JButton("Cancel");
 27
 28     // 文本框
 29     JTextField headphoneText = new JTextField();
 30     JTextField shellText = new JTextField();
 31     JTextField protectorText = new JTextField();
 32     JTextField resultText = new JTextField();
 33     JTextField result2Text = new JTextField();
 34     JTextField result3Text = new JTextField();
 35
 36     public CommissionPanel(){
 37         //设置字体
 38         Font titleFont = new Font("宋体", Font.BOLD, 20);
 39         titleLab.setFont(titleFont);
 40         Font font = new Font("宋体", Font.BOLD, 16);
 41         headphoneLab.setFont(font);
 42         shellLab.setFont(font);
 43         protectorLab.setFont(font);
 44         resultLab.setFont(font);
 45         result2Lab.setFont(font);
 46         result3Lab.setFont(font);
 47         resultText.setFont(font);
 48         result2Text.setFont(font);
 49         result3Text.setFont(font);
 50
 51
 52         //设置按钮监听
 53         submit.addActionListener(new ActionListener() {
 54
 55             @Override
 56             public void actionPerformed(ActionEvent e) {
 57                 if (e.getSource()==submit) {
 58                     String strheadphone = headphoneText.getText();
 59                     String strshell = shellText.getText();
 60                     String strprotector = protectorText.getText();
 61                     Commission commission = new Commission();
 62                     //当输入满足条件
 63                     if (commission.validate(strheadphone)&&commission.validate(strshell)&&commission.validate(strprotector)) {
 64                         //将字符型转变为整数型
 65                         int headphone = Integer.parseInt(strheadphone);
 66                         int shell = Integer.parseInt(strshell);
 67                         int protector = Integer.parseInt(strprotector);
 68                         //计算结果
 69                         float result = commission.commission(headphone, shell, protector);
 70                         String result2 = commission.mostSale(headphone, shell, protector);
 71                         int result3 = commission.diffSale(headphone, shell, protector);
 72                         //设置内容
 73                         resultText.setText(String.valueOf(result));
 74                         result2Text.setText(result2);
 75                         result3Text.setText(String.valueOf(result3));
 76                     }else{
 77                         JOptionPane.showMessageDialog(null, "输入有误,请重新输入!");
 78                         headphoneText.setText("");
 79                         shellText.setText("");
 80                         protectorText.setText("");
 81                     }
 82
 83                 }
 84
 85             }
 86         });
 87
 88         reset.addActionListener(new ActionListener() {
 89
 90             @Override
 91             public void actionPerformed(ActionEvent e) {
 92                 headphoneText.setText("");
 93                 shellText.setText("");
 94                 protectorText.setText("");
 95                 resultText.setText("");
 96                 result2Text.setText("");
 97                 result3Text.setText("");
 98             }
 99         });
100
101         //设置颜色
102         resultText.setDisabledTextColor(Color.BLACK);
103         result2Text.setDisabledTextColor(Color.BLACK);
104         result3Text.setDisabledTextColor(Color.BLACK);
105
106         //设置无法输入
107         resultText.setEnabled(false);
108         result2Text.setEnabled(false);
109         result3Text.setEnabled(false);
110
111         //设置组件的位置
112         jFrame.setLayout(null);
113         titleLab.setBounds(60,5,400,20);
114         headphoneLab.setBounds(20,50,60,20);
115         headphoneText.setBounds(65,50,50,20);
116         shellLab.setBounds(120,50,100,20);
117         shellText.setBounds(180,50,50,20);
118         protectorLab.setBounds(250,50,60,20);
119         protectorText.setBounds(295,50,50,20);
120         submit.setBounds(80, 100, 100, 25);
121         reset.setBounds(200, 100, 100, 25);
122         resultLab.setBounds(20,150,120,25);
123         resultText.setBounds(130,150,120,25);
124         result2Lab.setBounds(20,200,180,25);
125         result2Text.setBounds(180,200,180,25);
126         result3Lab.setBounds(20,250,240,25);
127         result3Text.setBounds(250,250,120,25);
128
129
130         //添加组件
131         jFrame.add(titleLab);
132         jFrame.add(headphoneLab);
133         jFrame.add(headphoneText);
134         jFrame.add(shellLab);
135         jFrame.add(shellText);
136         jFrame.add(protectorLab);
137         jFrame.add(protectorText);
138         jFrame.add(submit);
139         jFrame.add(reset);
140         jFrame.add(resultLab);
141         jFrame.add(resultText);
142         jFrame.add(result2Lab);
143         jFrame.add(result2Text);
144         jFrame.add(result3Lab);
145         jFrame.add(result3Text);
146
147         //设置Frame
148         jFrame.setLocation(100, 100);
149         jFrame.setSize(400, 400);
150         jFrame.setVisible(true);
151
152
153     }
154
155
156
157 }

View Code

1 public class Main {
2
3     public static void main(String args[]){
4         CommissionPanel panel = new CommissionPanel();
5     }
6 }

View Code

转载于:https://www.cnblogs.com/lucerner/p/6792939.html

第4次作业类测试代码+087+饶慧敏相关推荐

  1. 第4次作业类测试代码+105032014138+牟平

    类测试代码的具体要求如下: 设计三角形完整程序 已经完成的方法是:  String triangle(int a,int b,int c) 现在要求继续增加新的功能: 建立界面,至少包含以下元素,但不 ...

  2. 第四次作业类测试代码+036+吴心怡

    一.类图 二.代码 package application; public class Commission { /* * hp:耳机 80元 mpc:手机壳 10元 cpsp:手机贴膜 8元 */ ...

  3. 第4次作业类测试代码+105032014065+方绎杰

    一.类图 二.代码 Date类: package examOne;import java.util.Scanner;import snippet.Snippet;public class Date { ...

  4. 第4次作业类测试代码+105032014070+胡阳洋

    1.类图. 2.代码及界面. package test.java.distinguish.triangle;public class Distinguish {/*** * @param a 三角形边 ...

  5. 第4次作业类测试代码+163+张玉洁

    1.类图: 2.结果: 3.代码及界面: 1 package test; 2 public class Triangle { 3 public String triangle(int a,int b, ...

  6. 第4次作业类测试代码+001+陈定国

    1.类图 2.代码 Triangle类: package triangleDemo; public class Triangle{public String triangle(int a,int b, ...

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

    1.类图 2.代码 常量定义: private final static int HP_PRICE=80; private final static int SHELL_PRICE=10; priva ...

  8. 第4次作业类测试代码+043+杨晨宇

    triangle的代码: package triangle;import java.text.DecimalFormat;public class Triangle {public Triangle( ...

  9. 第5次作业+087+饶慧敏

    测试链接:http://www.cnblogs.com/leezoey/p/6803945.html 1)被测项目界面 该同学暂未做与界面相关的方法以及界面的实现. 2)测试用例设计表 符号标记:耳机 ...

最新文章

  1. 嵌入式编程笔记之六--设备树初体验
  2. Java中的多线程你只要看这一篇就够了
  3. word2vec 中的数学原理具体解释(四)基于 Hierarchical Softmax 的模型
  4. 【Elasticsearch】第2篇:Elasticsearch分布式安装
  5. Linux自启进程管理工具,进程管理工具Supervisor的安装及使用
  6. 计算机浏览器应用程序,基于浏览器的应用程序
  7. V-SQL的简单使用
  8. 拓端tecdat|R语言Bootstrap的岭回归和自适应LASSO回归可视化
  9. 计算机青蓝云题库,计算机三级上机题库 计算机三级网络技术上机题库《南开100题》.doc...
  10. linux win10 mac地址修改,两种方法教你修改Win10专业版MAC物理地址
  11. YOLACT论文阅读及解析
  12. 关于网站速度优化的一点建议
  13. c++闭区间内的素数
  14. 【软件推荐】Linux的一些好玩的软件
  15. 智能LED电子钟的制作
  16. DataX及DataX-Web
  17. 获取蓝奏网盘无密码下载链接分析思路
  18. mysql8 设置binlog过期时间
  19. Es Bucket聚合(桶聚合) Terms Aggregation与Significant Terms Aggregation
  20. SpringBoot JPA(JpaRepository)动态查询 分页展示

热门文章

  1. 串口服务器 数控系统,网络数控制造系统中常用DNC通讯接口模式
  2. 如何应对互联网行业的「中年危机」?
  3. Rocksdb 数据库--MANIFEST文件
  4. ssh远程管理及控制
  5. CAM350技巧【导入gerber文件】【单位尺度设置】【CAM测量】【操作说明与快捷键】
  6. Android RTSP 摄像头推流
  7. ubuntu 连接显示器
  8. ubuntu18.04 android,ubuntu18.04系统的安装以及基本配置
  9. 尔雅科幻中的物理学答案
  10. 最短路径(Dijkstra算法)