1. 程式人生 > >第4次作業類測試代碼+105032014045+楊銘河

第4次作業類測試代碼+105032014045+楊銘河

rfi color too efi rgs text blog ace val

1、類圖:

技術分享

2、代碼:

(1)計算類:

class Arithmetic{
  //邏輯計算類
private int headphoneNum; private int mpShellNum; private int csProtectorNum; public Arithmetic(int hp, int ms, int cp) { this.headphoneNum = hp; this.mpShellNum = ms; this.csProtectorNum = cp; } public
float commission(){ int total = headphoneNum*80+mpShellNum*10+csProtectorNum*8; float commission = 0; if(total<1000){ commission = (float) (total*0.1); }else if(total>=1000 && total<=1800){ commission = (float) (1000*0.1+(total-1000)*0.15); }
else if(total>1800){ commission = (float) (1000*0.1+800*0.15+(total-1800)*0.2); } return commission; } public String mostSale(){ StringBuilder mSale = new StringBuilder("耳機 "); int max = headphoneNum; if (max < mpShellNum) { mSale.replace(
0, mSale.length(), "手機殼 "); max = mpShellNum; }else if(max == mpShellNum){ mSale.append("手機殼 "); } if (max < csProtectorNum) { mSale.replace(0, mSale.length(), "貼膜 "); max = mpShellNum; }else if(max == csProtectorNum){ mSale.append("貼膜 "); } return mSale.toString(); } public int diffSale(){ int[] goods = {headphoneNum,mpShellNum,csProtectorNum}; for (int i = 0, len = goods.length; i < len; i++) { for (int j = i+1; j < len; j++) { if (goods[i] < goods[j]) { int min = goods[i]; goods[i] = goods[j]; goods[j] = min; } } } int dSale = goods[0]-goods[2]; return dSale; } }

(2)界面類:

class ActionHandle{
  //界面設計類
private JFrame frame = new JFrame("手機配件傭金計算器"); private JLabel infoLab = new JLabel("請輸入銷售數量: "); private JLabel headphoneLab = new JLabel("耳機: "); private JLabel mpShellLab = new JLabel("手機殼: "); private JLabel csProtectorLab = new JLabel("貼膜: "); private JLabel refundLab = new JLabel("應返還的傭金: "); private JLabel mostSaleLab = new JLabel("銷售額最高的配件是: "); private JLabel diffSaleLab = new JLabel("銷售配件最多與最少數量相差: "); private JTextField headphoneFie = new JTextField(); private JTextField mpShellFie = new JTextField(); private JTextField csProtectorFie = new JTextField(); private JTextField refundFie = new JTextField(); private JTextField mostSaleFie = new JTextField(); private JTextField diffSaleFie = new JTextField(); private JButton submit = new JButton("OK"); private JButton reset = new JButton("reset"); public ActionHandle() { Font fnt = new Font("Serirf",Font.BOLD,12); frame.setLayout(null); infoLab.setFont(fnt);
      //submit按鍵監聽方法 submit.addActionListener(
new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource()==submit) { String headphoneNumStr = new String(headphoneFie.getText()); String mpShellNumStr = new String(mpShellFie.getText()); String csProtectorNumStr = new String(csProtectorFie.getText()); try { int headphoneNumInt = Integer.parseInt(headphoneNumStr); int mpShellNumInt = Integer.parseInt(mpShellNumStr); int csProtectorNumInt = Integer.parseInt(csProtectorNumStr); Arithmetic arithmetic = new Arithmetic(headphoneNumInt, mpShellNumInt, csProtectorNumInt); refundFie.setText(String.valueOf(arithmetic.commission())); mostSaleFie.setText(arithmetic.mostSale()); diffSaleFie.setText(String.valueOf(arithmetic.diffSale())); } catch (NumberFormatException e2) { Toolkit.getDefaultToolkit().beep(); JOptionPane.showMessageDialog(null, "輸入數量不滿足要求", "警告", JOptionPane.WARNING_MESSAGE); } } } });
//reset按鍵監聽方法 reset.addActionListener(
new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource()==reset) { headphoneFie.setText(""); mpShellFie.setText(""); csProtectorFie.setText(""); refundFie.setText(""); mostSaleFie.setText(""); diffSaleFie.setText(""); } } });
//關閉 frame.addWindowListener(
new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(1); } });
//控件位置安排 infoLab.setBounds(
5, 5, 120, 20); frame.add(infoLab); headphoneLab.setBounds(5, 30, 60, 20); headphoneFie.setBounds(65, 30, 60, 20); mpShellLab.setBounds(135, 30, 60, 20); mpShellFie.setBounds(195, 30, 60, 20); csProtectorLab.setBounds(265, 30, 60, 20); csProtectorFie.setBounds(325, 30, 60, 20); submit.setBounds(50, 60, 90, 20); reset.setBounds(190, 60, 90, 20); refundLab.setBounds(5, 90, 90, 20); refundFie.setBounds(95, 90, 110, 20); mostSaleLab.setBounds(5, 120, 130, 20); mostSaleFie.setBounds(135, 120, 110, 20); diffSaleLab.setBounds(5, 150, 180, 20); diffSaleFie.setBounds(185, 150, 60, 20); frame.add(headphoneLab); frame.add(mpShellLab); frame.add(csProtectorLab); frame.add(refundLab); frame.add(mostSaleLab); frame.add(diffSaleLab); frame.add(headphoneFie); frame.add(mpShellFie); frame.add(csProtectorFie); frame.add(refundFie); frame.add(mostSaleFie); frame.add(diffSaleFie); frame.add(submit); frame.add(reset); frame.setSize(420, 250); frame.setVisible(true); } }

(3)執行類:

public class swing {
    public static void main(String[] args) {
        new ActionHandle();
    }
}

3、界面:

技術分享

第4次作業類測試代碼+105032014045+楊銘河