超市收银系统(Java语言实现)_第1页
超市收银系统(Java语言实现)_第2页
超市收银系统(Java语言实现)_第3页
超市收银系统(Java语言实现)_第4页
超市收银系统(Java语言实现)_第5页
已阅读5页,还剩26页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、package cashier;import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.Objec

2、tInputStream;import java.io.ObjectOutputStream;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.Timer;imp

3、ort javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;SuppressWarnings("serial")public class SystemWindow extends JFrame implements ActionListenerstatic Checkstand checkstand;/此变量单纯为了保存读至保存HashMap文件中的数据,相当于一个中间媒介/* * 供显示的商品信息 */Goods good;/* * 保存收银台信息的文件 */static File

4、 file; static Checkstand c;/放在此处并不和前面的checkstand相矛盾,此变量是为了读取、保存柜台信息 static Long number;/输入的卡号 Timer time;/获取系统时间JPanel leftPanel;JButton member;/会员服务JButton common;/普通服务 JButton findMemberInfo;/查找会员信息JButton findAllMemberInfo;/查看所有会员信息JButton findGoodsSurplus;/查看商品剩余量JButton findSaleroom;/查看柜台销售额JPa

5、nel rightPanel;JTextField showTime;JPanel rightPanelSon;public SystemWindow() super("欢迎使用超市收银系统");file=new File("checkstand.dat");SystemWindow.readCheckstandInfo();/读取柜台信息Goods.getSurplusFromFile();/这一步很重要!good=new Goods();time=new Timer(1000,this);/时间time.start();this.setLayout(

6、new GridLayout(1,2,4,4);leftPanel=new JPanel();leftPanel.setLayout(new GridLayout(6,1,4,4);member=new JButton("会员服务");common=new JButton("普通服务");findMemberInfo=new JButton("查找会员信息");findAllMemberInfo=new JButton("查看所有会员信息");findGoodsSurplus=new JButton("查

7、看商品剩余量");findSaleroom=new JButton("查看柜台销售额");member.addActionListener(this);common.addActionListener(this);findMemberInfo.addActionListener(this);findAllMemberInfo.addActionListener(this);findGoodsSurplus.addActionListener(this);findSaleroom.addActionListener(this);leftPanel.add(membe

8、r);leftPanel.add(common);leftPanel.add(findMemberInfo);leftPanel.add(findAllMemberInfo);leftPanel.add(findGoodsSurplus);leftPanel.add(findSaleroom);rightPanel=new JPanel();showTime=new JTextField(12);rightPanelSon=new JPanel();showTime.setEditable(false);rightPanel.setLayout(new BorderLayout(1,1);ri

9、ghtPanel.add(showTime,BorderLayout.NORTH);rightPanel.add(BorderLayout.CENTER,rightPanelSon);this.add(leftPanel);this.add(rightPanel);this.setSize(600,500);this.setLocation(500,127);this.setVisible(true);/* * * return 返回文件中的记录 */SuppressWarnings( "unchecked", "static-access" )publ

10、ic static HashMap<ClubCard,Customer> getHashMap()File file=Checkstand.file;if(file.length()=0) checkstand=new Checkstand();checkstand.definedForSystemWindow();else try ObjectInputStream in=new ObjectInputStream(new FileInputStream(file);checkstand.cardAndCustomer=(HashMap<ClubCard,Customer&

11、gt;)in.readObject();in.close();catch(IOException e)System.err.println(e.getMessage();catch(ClassNotFoundException e1)System.err.println("文件未找到!");return checkstand.cardAndCustomer;/* * 保存柜台信息 对象串行化 */public static void AddCheckstandInfo() try if(c=null)c=new Checkstand();ObjectOutputStream

12、 out=new ObjectOutputStream(new FileOutputStream(file);out.writeObject(c);out.close();catch(IOException e)System.out.println(e.getMessage();/* * 从文件中读取柜台信息 对象串行化 * throws ClassNotFoundException */public static void readCheckstandInfo() try ObjectInputStream in=new ObjectInputStream(new FileInputStre

13、am(file); Object o=in.readObject(); if(o!=null) c=(Checkstand)o; else c=new Checkstand();/在此处生成柜台对象再合适不过来 in.close(); catch(IOException e)System.out.println(e.getMessage();catch(ClassNotFoundException e1)System.out.println("文件未找到!"); public static void main(String args) new SystemWindow();

14、 Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubDateFormat format=new SimpleDateFormat();Date date=new Date();showTime.setText(" 北京时间:"+format.format(date);final String PrintTime=format.format(date);/将信息打印在顾客的消费信息上if(e.getSource()=member) rightPanelSon.

15、removeAll();/事先先移除rightPanelSon中的所有的组件this.paintAll(getGraphics();/相当于重画的功能,此部骤相当重要rightPanelSon.setLayout(new FlowLayout();rightPanelSon.add(new JPanel();rightPanelSon.add(new JPanel();final JTextArea goodsInfo=new JTextArea();/显示商品信息rightPanelSon.add(goodsInfo);goodsInfo.setEditable(false);/不可编辑 g

16、oodsInfo.setText(good.toString(); String id=JOptionPane.showInputDialog("请输入会员卡卡号:"); try number=Long.parseLong(id); catch(NumberFormatException e1) JOptionPane.showMessageDialog(null, "输入格式有误!"); return; final HashMap<ClubCard,Customer> c1=SystemWindow.getHashMap(); if(c1=

17、null) JOptionPane.showMessageDialog(null, "会员信息不存在!",null, JOptionPane.WARNING_MESSAGE); return; else int flag=0; Customer customer1=null; ClubCard card2 = null; for(ClubCard card:c1.keySet() if(number.equals(card.getId() JTextArea area=new JTextArea(); rightPanelSon.add(area); area.setTex

18、t(c1.get(card).toString();/输出顾客信息 customer1=c1.get(card); flag=customer1.getCustomeCard().getGrade(); card2=card; /if /for if(customer1=null) JOptionPane.showMessageDialog(null, "会员信息不存在!",null, JOptionPane.WARNING_MESSAGE); return; final ClubCard card1 = card2; final int flag1=flag; final

19、 Customer customer=customer1; JPanel pane1=new JPanel(); pane1.setLayout(new GridLayout(6,2,2,2); JLabel j1=new JLabel("购"+good.getGoodsNames().get(0)+"的数量");final JTextField jt1=new JTextField(); JLabel j2=new JLabel("购"+good.getGoodsNames().get(1)+"的数量");fin

20、al JTextField jt2=new JTextField(); JLabel j3=new JLabel("购"+good.getGoodsNames().get(2)+"的数量");final JTextField jt3=new JTextField(); JLabel j4=new JLabel("购"+good.getGoodsNames().get(3)+"的数量");final JTextField jt4=new JTextField(); JLabel j5=new JLabel("

21、;购"+good.getGoodsNames().get(4)+"的数量");final JTextField jt5=new JTextField(); JLabel j6=new JLabel("购"+good.getGoodsNames().get(5)+"的数量");final JTextField jt6=new JTextField(); pane1.add(j1);pane1.add(jt1); pane1.add(j2);pane1.add(jt2); pane1.add(j3);pane1.add(jt3)

22、; pane1.add(j4);pane1.add(jt4); pane1.add(j5);pane1.add(jt5); pane1.add(j6);pane1.add(jt6); rightPanelSon.add(pane1); jt1.setText("0");/商品件数都默认设为0件 jt2.setText("0"); jt3.setText("0"); jt4.setText("0"); jt5.setText("0"); jt6.setText("0"); fi

23、nal JButton cal=new JButton("计算"); final JButton empty=new JButton("归零"); final JLabel hint=new JLabel();/对计算所得总额进行反应 rightPanelSon.add(cal); rightPanelSon.add(hint); rightPanelSon.add(empty); cal.addActionListener(new ActionListener() SuppressWarnings( "static-access")

24、 Override public void actionPerformed(ActionEvent e) Double total=0.0; / TODO Auto-generated method stub if(e.getSource()=cal) try String s1=jt1.getText();int num1=Integer.parseInt(s1); String s2=jt2.getText();int num2=Integer.parseInt(s2); String s3=jt3.getText();int num3=Integer.parseInt(s3); Stri

25、ng s4=jt4.getText();int num4=Integer.parseInt(s4); String s5=jt5.getText();int num5=Integer.parseInt(s5); String s6=jt6.getText();int num6=Integer.parseInt(s6); total+=num1*good.getPrice().get(0)+ num2*good.getPrice().get(1)+ num3*good.getPrice().get(2)+ num4*good.getPrice().get(3)+ num5*good.getPri

26、ce().get(4)+ num6*good.getPrice().get(5); /更新商品剩余量 good.updateSurplus(0, num1); good.updateSurplus(1, num2); good.updateSurplus(2, num3); good.updateSurplus(3, num4); good.updateSurplus(4, num5); good.updateSurplus(5, num6); /保存商品剩余信息到文件 good.updateSurplusFromFile(); /显示商品信息要发生变化 goodsInfo.setText(g

27、ood.toString(); if(flag1=5) hint.setText("该顾客享受9.5折优惠,本应收¥"+total+"实收¥"+(total*0.95); /更改柜台销售量 c.getSaleroomFromFile(); c.setSaleroom(c.getSaleroom()+total); SystemWindow.AddCheckstandInfo(); customer.addCustomeInfo(PrintTime+"消费¥"+total+",享受9.5折优惠后实收¥"+(total

28、*0.95); if(total>=1000&&total<2000) JOptionPane.showMessageDialog(null, "恭喜该顾客会员卡升级为"蓝钻卡",此后购物享受9折优惠!"); new Card2(); customer.getCustomeCard().setGrade(4);/在后面统一保存信息 else if(total>=2000&&total<4000) JOptionPane.showMessageDialog(null, "恭喜该顾客会员卡升级

29、为"贵族卡",此后购物享受8.5折优惠!"); new Card3(); customer.getCustomeCard().setGrade(3);/在后面统一保存信息 else if(total>=4000&&total<10000) JOptionPane.showMessageDialog(null, "恭喜该顾客会员卡升级为"白钻卡",此后购物享受8折优惠!"); new Card4(); customer.getCustomeCard().setGrade(2);/在后面统一保存信息

30、else if(total>=10000) JOptionPane.showMessageDialog(null, "恭喜该顾客会员卡升级为"至尊卡",此后购物享受7折优惠!"); new Card5(); customer.getCustomeCard().setGrade(1);/在后面统一保存信息 else if(flag1=4) hint.setText("该顾客享受9折优惠,本应收¥"+total+"实收¥"+(total*0.9); /更改柜台销售量 c.getSaleroomFromFile()

31、; c.setSaleroom(c.getSaleroom()+total); SystemWindow.AddCheckstandInfo(); customer.addCustomeInfo(PrintTime+"消费¥"+total+",享受9折优惠后实收¥"+(total*0.9); if(total>=2000&&total<4000) JOptionPane.showMessageDialog(null, "恭喜该顾客会员卡升级为"贵族卡",此后购物享受8.5折优惠!");

32、new Card3(); customer.getCustomeCard().setGrade(3);/在后面统一保存信息 else if(total>=4000&&total<10000) JOptionPane.showMessageDialog(null, "恭喜该顾客会员卡升级为"白钻卡",此后购物享受8折优惠!"); new Card4(); customer.getCustomeCard().setGrade(2);/在后面统一保存信息 else if(total>=10000) JOptionPane.sh

33、owMessageDialog(null, "恭喜该顾客会员卡升级为"至尊卡",此后购物享受7折优惠!"); new Card5(); customer.getCustomeCard().setGrade(1);/在后面统一保存信息 else if(flag1=3) hint.setText("该顾客享受8.5折优惠,本应收¥"+total+"实收¥"+(total*0.85); /更改柜台销售量 c.getSaleroomFromFile(); c.setSaleroom(c.getSaleroom()+tota

34、l); SystemWindow.AddCheckstandInfo(); customer.addCustomeInfo(PrintTime+"消费¥"+total+",享受8.5折优惠后实收¥"+(total*0.85); if(total>=4000&&total<10000) JOptionPane.showMessageDialog(null, "恭喜该顾客会员卡升级为"白钻卡",此后购物享受8折优惠!"); new Card4(); customer.getCustomeCa

35、rd().setGrade(2);/在后面统一保存信息 else if(total>=10000) JOptionPane.showMessageDialog(null, "恭喜该顾客会员卡升级为"至尊卡",此后购物享受7折优惠!"); new Card5(); customer.getCustomeCard().setGrade(1);/在后面统一保存信息 else if(flag1=2) hint.setText("该顾客享受8折优惠,本应收¥"+total+"实收¥"+(total*0.8); /更改柜

36、台销售量 c.getSaleroomFromFile(); c.setSaleroom(c.getSaleroom()+total); SystemWindow.AddCheckstandInfo(); customer.addCustomeInfo(PrintTime+"消费¥"+total+",享受8折优惠后实收¥"+(total*0.8); if(total>=10000) JOptionPane.showMessageDialog(null, "恭喜该顾客会员卡升级为"至尊卡",此后购物享受7折优惠!"

37、;); new Card5(); customer.getCustomeCard().setGrade(1);/在后面统一保存信息 else if(flag1=1) hint.setText("该顾客享受7折优惠,本应收¥"+total+"实收¥"+(total*0.7); /更改柜台销售量 c.getSaleroomFromFile(); c.setSaleroom(c.getSaleroom()+total); SystemWindow.AddCheckstandInfo(); customer.addCustomeInfo(PrintTime+&q

38、uot;消费¥"+total+",享受7折优惠后实收¥"+(total*0.7); /try catch(NumberFormatException e1)JOptionPane.showMessageDialog(null, "输入格式有误!"); c1.put(card1, customer);/放在此处->统一保存 Checkstand.save(); /if ); empty.addActionListener(new ActionListener() Override public void actionPerformed(Ac

39、tionEvent e) / TODO Auto-generated method stub if(e.getSource()=empty) jt1.setText("0");/商品件数清空 jt2.setText("0"); jt3.setText("0"); jt4.setText("0"); jt5.setText("0"); jt6.setText("0"); ); /else else if(e.getSource()=common) rightPanelSon.r

40、emoveAll();/事先先移除rightPanelSon中的所有的组件this.paintAll(getGraphics();/相当于重画的功能,此部骤相当重要rightPanelSon.setLayout(new FlowLayout();final JTextArea goodsInfo=new JTextArea();/显示商品信息rightPanelSon.add(goodsInfo);goodsInfo.setEditable(false);/不可编辑 goodsInfo.setText(good.toString(); JPanel pane1=new JPanel(); pa

41、ne1.setLayout(new GridLayout(6,2,2,2); JLabel j1=new JLabel("购"+good.getGoodsNames().get(0)+"的数量");final JTextField jt1=new JTextField(); JLabel j2=new JLabel("购"+good.getGoodsNames().get(1)+"的数量");final JTextField jt2=new JTextField(); JLabel j3=new JLabel(&q

42、uot;购"+good.getGoodsNames().get(2)+"的数量");final JTextField jt3=new JTextField(); JLabel j4=new JLabel("购"+good.getGoodsNames().get(3)+"的数量");final JTextField jt4=new JTextField(); JLabel j5=new JLabel("购"+good.getGoodsNames().get(4)+"的数量");final

43、 JTextField jt5=new JTextField(); JLabel j6=new JLabel("购"+good.getGoodsNames().get(5)+"的数量");final JTextField jt6=new JTextField(); pane1.add(j1);pane1.add(jt1); pane1.add(j2);pane1.add(jt2); pane1.add(j3);pane1.add(jt3); pane1.add(j4);pane1.add(jt4); pane1.add(j5);pane1.add(jt5

44、); pane1.add(j6);pane1.add(jt6); rightPanelSon.add(pane1); jt1.setText("0");/商品件数都默认设为0件 jt2.setText("0"); jt3.setText("0"); jt4.setText("0"); jt5.setText("0"); jt6.setText("0"); final JButton cal=new JButton("计算"); final JButton empty=new JButton("归零"); final JLabel hint=new JLabel();/对计算所得总额进行反应 rightPanelSo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论