public class Cash {
private double price;
private int number;
public Cash(double price, int number) {
super();
this.price = price;
this.number = number;
}
public double getCash(double rate) {
return pricenumberrate;
}
public double getCash(int moneyCondition,int moneyReturn) {
return pricenumber-(int)(pricenumber)/moneyCondition*moneyReturn;
}
}
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class CashSystem extends JFrame{
public static final int WIDTH=800;
public static final int HEIGHT=600;
private JTextArea showArea;
private JLabel showResult;
private JTextField txtprice;
private JTextField txtnumber;
private JComboBox cbMethod;
double total=0;
public CashSystem() {
super("商场收银系统");
Dimension d=this.getToolkit().getScreenSize();
this.setLocation((d.width-WIDTH)/2,(d.height-HEIGHT)/2);
this.setSize(WIDTH,HEIGHT);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
//North 1
JPanel v1=new JPanel();
BoxLayout b1=new BoxLayout(v1,BoxLayout.Y_AXIS);
v1.setLayout(b1);
v1.add(new JLabel("单价:"));
v1.add(new JLabel("数量:"));
v1.add(new JLabel("计算方式:"));
//North 2
JPanel v2=new JPanel();
BoxLayout b2=new BoxLayout(v2,BoxLayout.Y_AXIS);
v2.setLayout(b2);
txtprice = new JTextField(15);
txtnumber = new JTextField(15);
String[] items= {"正常收费","打8折","打7折","打5折","满300返100"};
cbMethod = new JComboBox(items);
v2.add(txtprice);
v2.add(txtnumber);
v2.add(cbMethod);
//North 3
JPanel v3=new JPanel();
BoxLayout b3=new BoxLayout(v3,BoxLayout.Y_AXIS);
v3.setLayout(b3);
JButton confirm = new JButton("确定");
confirm.addActionListener(new ConfirmHandler());
JButton cancel = new JButton("取消");
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtprice.setText(null);
txtnumber.setText(null);
showArea.setText(null);
showResult.setText(null);
}
});
v3.add(confirm);
v3.add(cancel);
JPanel h=new JPanel();
BoxLayout b=new BoxLayout(h,BoxLayout.X_AXIS);
h.setLayout(b);
h.add(v1);
h.add(v2);
h.add(v3);
this.add(h,"North");
//Center
showArea=new JTextArea();
JScrollPane jShowArea=new JScrollPane(showArea);
this.add(jShowArea);
//South
showResult=new JLabel("显示总价");
Font font = new Font("楷体",Font.ITALIC,20);
showResult.setFont(font);
this.add(showResult,"South");
this.setVisible(true);
}
class ConfirmHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
double price=Double.parseDouble(txtprice.getText());
int number=Integer.parseInt(txtnumber.getText());
int method=cbMethod.getSelectedIndex();
double totalPrice=0;
Cash cash=new Cash(price,number);
String condition="正常收费";
switch(method) {
case 0 : totalPrice = price*number;break;
case 1 : totalPrice = price*number*0.8;condition="打8折";break;
case 2 : totalPrice = price*number*0.7;condition="打7折";break;
case 3 : totalPrice = price*number*0.5;condition="打5折";break;
case 4 : totalPrice = price*number-(int)(price*number)/300*100;condition="满300返100";break;
}
total=total+totalPrice;
showArea.append(condition+"--单价:"+price+"数量:"+number+"合计:"+totalPrice+"\n");
showResult.setText("总计:"+total+"元");
}
}
public static void main(String[] args) {
new CashSystem();
}
}