第4次作业类测试代码+105032014162+张倩
Posted Qian_Z
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第4次作业类测试代码+105032014162+张倩相关的知识,希望对你有一定的参考价值。
一、类图
二、代码
1 package demo04; 2 import java.awt.event.ActionEvent; 3 import java.awt.event.ActionListener; 4 import java.awt.Color; 5 import java.awt.Font; 6 import javax.swing.JFrame; 7 import javax.swing.JLabel; 8 import javax.swing.JButton; 9 import javax.swing.JTextField; 10 import javax.swing.JOptionPane; 11 public class caculate { 12 public static void main(String args[]){ 13 JFrame frame = new JFrame("配件销售计算程序"); 14 frame.setLayout(null); 15 //建立标签对象 16 JLabel title = new JLabel("请输入销售数量:"); 17 JLabel headphone = new JLabel("耳机:"); 18 JLabel shell = new JLabel("手机壳:"); 19 JLabel protector = new JLabel("贴膜:"); 20 JLabel commission = new JLabel("应返还的佣金:"); 21 JLabel mostSale = new JLabel("销售额最高的配件是:"); 22 JLabel diffSale = new JLabel("销售配件最多与最少数量相差:"); 23 //建立单行文本框 24 JTextField t_headphone = new JTextField(); 25 JTextField t_shell = new JTextField(); 26 JTextField t_protector = new JTextField(); 27 JTextField t_commission = new JTextField(); 28 JTextField t_mostSale = new JTextField(); 29 JTextField t_diffSale = new JTextField(); 30 //建立按钮对象 31 JButton submit = new JButton("确定"); 32 JButton cancel = new JButton("清除"); 33 34 //使用匿名内部类为“确定”按钮添加监听事件 35 submit.addActionListener(new ActionListener() 36 { 37 @Override 38 public void actionPerformed(ActionEvent arg0) 39 { 40 int headphone = Commission.changeToInt(t_headphone.getText()); 41 if(headphone < 0) 42 { 43 JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "出错", JOptionPane.ERROR_MESSAGE); 44 return; 45 } 46 int protector=Commission.changeToInt(t_protector.getText()); 47 if(protector < 0) 48 { 49 JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "出错", JOptionPane.ERROR_MESSAGE); 50 return; 51 } 52 int shell=Commission.changeToInt(t_shell.getText()); 53 if(shell < 0) 54 { 55 JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "出错", JOptionPane.ERROR_MESSAGE); 56 return; 57 } 58 float returnAns=Commission.commission(headphone, shell, protector); 59 String returnString=String.format("%.2f元", returnAns); 60 t_commission.setText(returnString); 61 String maxAns=Commission.mostSale(headphone, shell, protector); 62 t_mostSale.setText(maxAns); 63 int differenceAns=Commission.diffSale(headphone, shell, protector); 64 String differenceString=String.format("%d", differenceAns); 65 t_diffSale.setText(differenceString); 66 } 67 68 }); 69 //使用匿名内部类为“清除”按钮添加监听事件 70 cancel.addActionListener(new ActionListener() 71 { 72 @Override 73 public void actionPerformed(ActionEvent arg0) 74 { 75 t_headphone.setText(""); 76 t_protector.setText(""); 77 t_shell.setText(""); 78 t_commission.setText(""); 79 t_mostSale.setText(""); 80 t_diffSale.setText(""); 81 } 82 }); 83 //设置标签大小及位置 84 Font ft = new Font("宋体",Font.BOLD,24); 85 title.setFont(ft); 86 title.setBounds(62, 62, 250, 25); 87 headphone.setFont(ft); 88 headphone.setBounds(80, 124, 300, 25); 89 shell.setFont(ft); 90 shell.setBounds(210, 124, 250, 25); 91 protector.setFont(ft); 92 protector.setBounds(380, 124, 155, 25); 93 commission.setFont(ft); 94 commission.setBounds(62, 248, 500, 25); 95 mostSale.setFont(ft); 96 mostSale.setBounds(62, 310, 900, 25); 97 diffSale.setFont(ft); 98 diffSale.setBounds(62, 372, 1000, 25); 99 //设置文字大小及位置 100 Font fft=new Font("宋体",Font.BOLD,14); 101 t_headphone.setBounds(150, 124, 50, 25); 102 t_shell.setBounds(310, 124, 50, 25); 103 t_protector.setBounds(450, 124, 50, 25); 104 t_commission.setBounds(230, 248, 200, 25); 105 t_commission.setFont(fft); 106 t_commission.setForeground(Color.black);//设置字体颜色 107 t_mostSale.setBounds(300, 310, 200, 25); 108 t_mostSale.setFont(fft); 109 t_mostSale.setForeground(Color.black); 110 t_diffSale.setBounds(400, 372, 150, 25); 111 t_diffSale.setFont(fft); 112 t_diffSale.setForeground(Color.black); 113 //设置按钮位置及大小 114 submit.setBounds(180, 187, 62, 31); 115 cancel.setBounds(320, 187, 62, 31); 116 //把所有组件添加到frame中 117 frame.add(title);frame.add(headphone);frame.add(shell);frame.add(protector); 118 frame.add(commission);frame.add(mostSale);frame.add(diffSale); 119 frame.add(t_headphone);frame.add(t_shell);frame.add(t_protector); 120 frame.add(t_commission);frame.add(t_mostSale);frame.add(t_diffSale); 121 frame.add(submit);frame.add(cancel); 122 //设置显示画布大小及显示的位置 123 frame.setSize(600, 490); 124 frame.setLocation(300,300); 125 frame.setVisible(true); 126 } 127 128 }
package demo04; public class Commission { //判断输入的值是否符合要求 public static int changeToInt(String number) { int result=0; try{ result = Integer.parseInt(number); } catch (Exception e){ result=-1; } return result; } //计算应该返还的佣金 public static float commission(int headphone,int shell,int protector) { int all = headphone*80+shell*10+protector*8; float result = 0; if(headphone>=0&&shell>=0&&protector>=0) { if(all<1000&&all>=0) { result = (float) (all*0.1); } else if(all>=1000 && all<=1800) { result = (float) (100+(all-1000)*0.15); } else if(all>1800) { result = (float) (100+800*0.15+(all-1800)*0.2); } } return result; } //计算销售额最高的配件 public static String mostSale(int headphone, int shell, int protector) { int headphoneSales = headphone * 80; int shellSales = shell * 10; int protectorSales = protector * 8; String result=""; if(headphoneSales > shellSales && headphoneSales > protectorSales) { result+="耳机"; } if(shellSales > headphoneSales && shellSales > protectorSales) { result+="手机壳"; } if(protectorSales > headphoneSales && protectorSales > shellSales) { result+="贴膜"; } if(headphoneSales==shellSales&&shellSales==protectorSales) { result+="三种配件销售额一样"; } if(headphoneSales==shellSales&&headphoneSales>protectorSales) { result+="耳机和手机壳"; } if(headphoneSales==protector&&headphoneSales>shellSales) { result+="耳机和贴膜"; } if(shellSales==protectorSales&&shellSales>headphoneSales) { result+="手机壳和贴膜"; } return result; } //销售配件最多与最少数量相差: 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; } }
三、界面
以上是关于第4次作业类测试代码+105032014162+张倩的主要内容,如果未能解决你的问题,请参考以下文章