第4次作业类测试代码+087+饶慧敏
1.类图
2.主要界面
当输入为空,输入非数值型数据时,程序会弹出错误信息,并要求重新输入:
正常情况下的输出结果为:
3.代码如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Main { 2 3 public static void main(String args[]){ 4 CommissionPanel panel = new CommissionPanel(); 5 } 6 }