第4次作业类测试代码+141+汤阳斌
1.类图:
2.界面和相应功能:
计算销售额最大的配件
//销售额最大的配件 public static String mostSale(int hp, int mpc, int cpsp){ String mostsale = null; int my_hp = 0,my_mpc = 0,my_cpsp = 0; my_hp = hp*80; my_mpc = mpc * 10; my_cpsp = cpsp * 8; if(my_hp > my_mpc){ if(my_hp > my_cpsp){ mostsale = "耳机"; } else{ mostsale = "手机贴膜"; } }else{ if(my_mpc > my_cpsp){ mostsale = "手机壳"; }else{ mostsale = "手机贴膜"; } } return mostsale; }
计算销售数量差最大:
//销售配件最多与最少之差 public static int diffSale(int hp, int mpc, int cpsp){ int diffsale = 0; int df1 = Math.abs(hp - mpc); int df2 = Math.abs(hp - cpsp); int df3 = Math.abs(mpc - cpsp); diffsale = df1 > df2 ? df1:df2; diffsale = diffsale > df3 ? diffsale : df3; return diffsale; }
界面设计:
package demo; 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.JTextField; import org.junit.experimental.theories.ParameterSignature; public class shabi { public static void main(String[] args) { JFrame frame = new JFrame("佣金計算"); frame.setLayout(null); JLabel title = new JLabel("請輸入銷售數量:"); JLabel hp = new JLabel("耳機:"); JLabel mpc = new JLabel("手機殼:"); JLabel cpsp = new JLabel("貼膜:"); JLabel commission = new JLabel("應返還的佣金:"); JLabel mostsale = new JLabel("銷售額最高的配件是:"); JLabel diffsale = new JLabel("銷售陪嫁最多與最少數量相差:"); JButton ok = new JButton("ok"); JButton cancel = new JButton("cancel"); JTextField out_hp = new JTextField(); JTextField out_mpc = new JTextField(); JTextField out_cpsp = new JTextField(); JTextField out_commission = new JTextField(); JTextField out_mostsale = new JTextField(); JTextField out_diffsale = new JTextField(); ok.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub int i_hp = Integer.parseInt(out_hp.getText()); int i_mpc = Integer.parseInt(out_mpc.getText()); int i_cpsp = Integer.parseInt(out_cpsp.getText()); float i_commission = Commission.commission(i_hp, i_mpc, i_cpsp); String s_commission = String.format("%f", i_commission); out_commission.setText(s_commission); String i_mostsale = Commission.mostSale(i_hp, i_mpc, i_cpsp); out_mostsale.setText(i_mostsale); int i_diffsale = Commission.diffSale(i_hp, i_mpc, i_cpsp); String s_diffsale = String.format("%d", i_diffsale); out_diffsale.setText(s_diffsale); } }); cancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub out_hp.setText(""); out_mpc.setText(""); out_cpsp.setText(""); out_commission.setText(""); out_mostsale.setText(""); out_diffsale.setText(""); } }); title.setBounds(5,5,220,20); hp.setBounds(5,40,30,20); mpc.setBounds(75,40,50,20); cpsp.setBounds(155,40,30,20); commission.setBounds(5,95,100,20); mostsale.setBounds(5,125,120,20); diffsale.setBounds(5,155,180,20); out_hp.setBounds(35,40,30,20); out_mpc.setBounds(125,40,30,20); out_cpsp.setBounds(185,40,30,20); out_commission.setBounds(95,90,120,20); out_mostsale.setBounds(125,125,90,20); out_diffsale.setBounds(185,155,30,20); ok.setBounds(10,70,75,20); cancel.setBounds(120,70,75,20); frame.add(title); frame.add(hp); frame.add(mpc); frame.add(cpsp); frame.add(commission); frame.add(mostsale); frame.add(diffsale); frame.add(out_hp); frame.add(out_mpc); frame.add(out_cpsp); frame.add(out_commission); frame.add(out_mostsale); frame.add(out_diffsale); frame.add(ok); frame.add(cancel); frame.setSize(250, 250); frame.setLocation(250,250); frame.setVisible(true); } }
界面: