首页 > 代码库 > 第4次作业类测试代码_078_刘玲志
第4次作业类测试代码_078_刘玲志
1.类图:
2.输入合法时,计算结果:
输入不合法时(输入为空,不为数字,长度不在int长度范围内等),提示输入有误,并清空输入框,重新输入:
3.代码:
1 package Practiseone; 2 3 import java.awt.EventQueue; 4 import java.awt.Font; 5 import java.awt.Frame; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 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.JPanel; 14 import javax.swing.JTextField; 15 import javax.swing.border.EmptyBorder; 16 17 public class MyJFrame extends JFrame { 18 19 private JPanel contentPane; 20 private JTextField headphone; 21 private JTextField shell; 22 private JTextField protctor; 23 private JTextField Commission; 24 private JTextField MaxNum; 25 private JTextField Difference; 26 27 /** 28 * Launch the application. 29 */ 30 public static void main(String[] args) { 31 EventQueue.invokeLater(new Runnable() { 32 public void run() { 33 try { 34 MyJFrame frame = new MyJFrame(); 35 frame.setVisible(true); 36 } catch (Exception e) { 37 e.printStackTrace(); 38 } 39 } 40 }); 41 } 42 43 /** 44 * Create the frame. 45 */ 46 public MyJFrame() { 47 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 48 setBounds(100, 100, 450, 300); 49 contentPane = new JPanel(); 50 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 51 setContentPane(contentPane); 52 contentPane.setLayout(null); 53 54 JLabel lblNewLabel = new JLabel("手机配件佣金计算程序"); 55 lblNewLabel.setFont(new Font("隶书", Font.PLAIN, 16)); 56 lblNewLabel.setBounds(135, -2, 186, 50); 57 contentPane.add(lblNewLabel); 58 59 60 JButton btnNewButton = new JButton("OK"); 61 btnNewButton.addActionListener(new ActionListener() { 62 public void actionPerformed(ActionEvent arg0) { 63 if(arg0.getSource()==btnNewButton){ 64 String headphoneNum = headphone.getText(); 65 String shellNum = shell.getText(); 66 String protctorNum = protctor.getText(); 67 Commission commission = new Commission(); 68 if (commission.correct(headphoneNum)&& 69 commission.correct(shellNum)&& 70 commission.correct(protctorNum)) 71 { 72 int headphone = Integer.parseInt(headphoneNum); 73 int shell = Integer.parseInt(shellNum); 74 int protctor = Integer.parseInt(protctorNum); 75 //输出计算佣金 76 double result1 = commission.Commission(headphoneNum, shellNum, protctorNum); 77 Commission.setText(String.valueOf(result1)); 78 //输出最大销售量 79 String result2 = commission.mostSale(headphone, shell, protctor); 80 MaxNum.setText(result2); 81 //输出销售数量差 82 double result3 = commission.diffSale(headphone, shell, protctor); 83 Difference.setText(String.valueOf(result3)); 84 }else 85 { 86 JOptionPane.showMessageDialog(null, "输入有误,请重新输入!"); 87 headphone.setText(""); 88 shell.setText(""); 89 protctor.setText(""); 90 } 91 } 92 } 93 }); 94 btnNewButton.setBounds(83, 93, 93, 23); 95 contentPane.add(btnNewButton); 96 97 JButton btnNewButton_1 = new JButton("Cancle"); 98 btnNewButton_1.addActionListener(new ActionListener() { 99 @Override 100 public void actionPerformed(ActionEvent arg1) { 101 // TODO Auto-generated method stub 102 System.exit(0); 103 } 104 105 }); 106 btnNewButton_1.setBounds(215, 93, 93, 23); 107 contentPane.add(btnNewButton_1); 108 109 JLabel lblNewLabel_2 = new JLabel("耳机:"); 110 lblNewLabel_2.setBounds(22, 55, 54, 15); 111 contentPane.add(lblNewLabel_2); 112 113 JLabel lblNewLabel_3 = new JLabel("手机壳:"); 114 lblNewLabel_3.setBounds(137, 55, 54, 15); 115 contentPane.add(lblNewLabel_3); 116 117 JLabel label = new JLabel("贴膜:"); 118 label.setBounds(276, 55, 54, 15); 119 contentPane.add(label); 120 121 headphone = new JTextField(); 122 headphone.setBounds(54, 54, 66, 21); 123 contentPane.add(headphone); 124 headphone.setColumns(10); 125 126 shell = new JTextField(); 127 shell.setBounds(188, 54, 66, 21); 128 contentPane.add(shell); 129 shell.setColumns(10); 130 131 protctor = new JTextField(); 132 protctor.setBounds(316, 54, 66, 21); 133 contentPane.add(protctor); 134 protctor.setColumns(10); 135 136 JLabel lblNewLabel_1 = new JLabel("应返还的佣金:"); 137 lblNewLabel_1.setBounds(18, 138, 115, 23); 138 contentPane.add(lblNewLabel_1); 139 140 Commission = new JTextField(); 141 Commission.setBounds(108, 138, 139, 23); 142 contentPane.add(Commission); 143 Commission.setColumns(10); 144 145 JLabel label_1 = new JLabel("销售额最高的配件是:"); 146 label_1.setBounds(17, 179, 153, 23); 147 contentPane.add(label_1); 148 149 MaxNum = new JTextField(); 150 MaxNum.setColumns(10); 151 MaxNum.setBounds(137, 176, 139, 23); 152 contentPane.add(MaxNum); 153 154 JLabel label_2 = new JLabel("销售配件最多与最少数量之差:"); 155 label_2.setBounds(17, 217, 241, 22); 156 contentPane.add(label_2); 157 158 Difference = new JTextField(); 159 Difference.setColumns(10); 160 Difference.setBounds(189, 217, 139, 23); 161 contentPane.add(Difference); 162 } 163 }
1 import java.util.Arrays; 2 3 public class Commission { 4 public static final int headphonePrice = 80; 5 public static final int shellPrice = 10; 6 public static final int protctorPrice = 8; 7 //判断输入是否符合要求 8 public boolean correct(String num) { 9 if("".equals(num)||!(num.matches("[0-9]+"))||num.length()>8||num==null) 10 { 11 return false; 12 } 13 return true; 14 } 15 16 public double Commission(String headphone,String shell,String protctor) 17 { 18 //定义一个变量存储佣金 19 double commission = 0; 20 //计算销售额 21 double salesAmount = headphonePrice*Integer.valueOf(headphone).intValue() 22 +shellPrice*Integer.valueOf(shell).intValue() 23 +protctorPrice*Integer.valueOf(protctor).intValue(); 24 //判断销售额 25 if (salesAmount<1000&&salesAmount>=0) { 26 commission=salesAmount*0.1; 27 }else if (salesAmount>=1000&&salesAmount<1800) { 28 commission = (salesAmount-1000)*0.15+100; 29 }else if (salesAmount>=1800) 30 { 31 commission = (salesAmount-1800)*0.2+220; 32 } 33 return commission; 34 } 35 36 public String mostSale(int headphone,int shell,int protctor) 37 { 38 39 double headphoneSale = headphone * 80; 40 double shellSale = shell * 10; 41 double protctorSale = protctor * 8; 42 double arrays[] = {headphoneSale,shellSale,protctorSale}; 43 Arrays.sort(arrays); 44 double max = arrays[2]; 45 String result=""; 46 if (max==headphoneSale) 47 { 48 result+="耳机"; 49 if (max==shellSale) 50 { 51 result+="、手机壳"; 52 } 53 if(max==protctorSale) 54 { 55 result+="、手机膜"; 56 } 57 58 }else if (max==shellSale) 59 { 60 result+="手机壳"; 61 if(max==protctorSale) 62 { 63 result+="、手机膜"; 64 } 65 }else { 66 result+="手机膜"; 67 } 68 return result; 69 } 70 71 public static int diffSale(int headphone, int shell, int protector){ 72 int arrays[] = {headphone,shell,protector}; 73 int result1 = 0;//结果 74 Arrays.sort(arrays); 75 result1 = arrays[2]-arrays[0]; 76 return result1; 77 78 } 79 }
第4次作业类测试代码_078_刘玲志
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。