第4次作业类测试代码+063+陈彧
Posted ChainYugi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第4次作业类测试代码+063+陈彧相关的知识,希望对你有一定的参考价值。
一、类图
二、代码
package swing; 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 Swing { 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_difference; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Swing window = new Swing(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public Swing() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setTitle("\\u624B\\u673A\\u914D\\u4EF6\\u4F63\\u91D1\\u8BA1\\u7B97\\u7A0B\\u5E8F"); frame.getContentPane().setBackground(SystemColor.menu); frame.setBounds(100, 100, 450, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); JLabel lblNewLabel = new JLabel("\\u8BF7\\u8F93\\u5165\\u9500\\u552E\\u6570\\u91CF\\uFF1A"); lblNewLabel.setBounds(10, 10, 131, 15); frame.getContentPane().add(lblNewLabel); JLabel label = new JLabel("\\u8033\\u673A\\uFF1A"); label.setBounds(10, 51, 46, 15); frame.getContentPane().add(label); JLabel label_1 = new JLabel("\\u624B\\u673A\\u58F3\\uFF1A"); label_1.setBounds(142, 51, 57, 15); frame.getContentPane().add(label_1); JLabel lblNewLabel_1 = new JLabel("\\u8D34\\u819C\\uFF1A"); lblNewLabel_1.setBounds(285, 51, 57, 15); frame.getContentPane().add(lblNewLabel_1); 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("\\u5E94\\u8FD4\\u8FD8\\u7684\\u4F63\\u91D1\\uFF1A"); 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("\\u9500\\u552E\\u989D\\u6700\\u9AD8\\u7684\\u914D\\u4EF6\\uFF1A"); label_3.setBounds(10, 165, 131, 15); frame.getContentPane().add(label_3); JLabel label_4 = new JLabel("\\u9500\\u552E\\u914D\\u4EF6\\u6700\\u591A\\u4E0E\\u6700\\u5C11\\u6570\\u91CF\\u76F8\\u5DEE\\uFF1A"); 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_difference = new JTextField(); textField_difference.setEditable(false); textField_difference.setBounds(198, 195, 194, 21); frame.getContentPane().add(textField_difference); textField_difference.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_difference.setText(differenceString); } }); btnOk.setBounds(10, 87, 93, 23); frame.getContentPane().add(btnOk); JButton btnCancle = new JButton("Cancle"); btnCancle.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { textField_headphone.setText(""); textField_protector.setText(""); textField_shell.setText(""); textField_return.setText(""); textField_max.setText(""); textField_difference.setText(""); } }); btnCancle.setBounds(299, 87, 93, 23); frame.getContentPane().add(btnCancle); } }
package swing; public class Commission{ //String To Int public static int changeToInt(String number){ int ans=0; try{ ans = Integer.parseInt(number); } catch (Exception e){ ans=-1; // TODO: handle exception } 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; } }
三、界面
PS:第一次用JAVA写界面,根本不会做。拜访了很多大佬才了解了一点基础。感觉上了一门实际是JAVA课的软测课。
以上是关于第4次作业类测试代码+063+陈彧的主要内容,如果未能解决你的问题,请参考以下文章