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、界面:
时间: 2024-10-18 12:56:40