您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 市场营销 > 超市收银系统(Java语言实现)
packagecashier;importjava.awt.BorderLayout;importjava.awt.FlowLayout;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.ObjectInputStream;importjava.io.ObjectOutputStream;importjava.text.DateFormat;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.HashMap;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JTextArea;importjavax.swing.JTextField;importjavax.swing.Timer;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;@SuppressWarnings(serial)publicclassSystemWindowextendsJFrameimplementsActionListener{staticCheckstandcheckstand;//此变量单纯为了保存读至保存HashMap文件中的数据,相当于一个中间媒介/***供显示的商品信息*/Goodsgood;/***保存收银台信息的文件*/staticFilefile;staticCheckstandc;//放在此处并不和前面的checkstand相矛盾,此变量是为了读取、保存柜台信息staticLongnumber;//输入的卡号Timertime;//获取系统时间JPanelleftPanel;JButtonmember;//会员服务JButtoncommon;//普通服务JButtonfindMemberInfo;//查找会员信息JButtonfindAllMemberInfo;//查看所有会员信息JButtonfindGoodsSurplus;//查看商品剩余量JButtonfindSaleroom;//查看柜台销售额JPanelrightPanel;JTextFieldshowTime;JPanelrightPanelSon;publicSystemWindow(){super(欢迎使用超市收银系统);file=newFile(checkstand.dat);SystemWindow.readCheckstandInfo();//读取柜台信息Goods.getSurplusFromFile();//这一步很重要!good=newGoods();time=newTimer(1000,this);//时间time.start();this.setLayout(newGridLayout(1,2,4,4));leftPanel=newJPanel();leftPanel.setLayout(newGridLayout(6,1,4,4));member=newJButton(会员服务);common=newJButton(普通服务);findMemberInfo=newJButton(查找会员信息);findAllMemberInfo=newJButton(查看所有会员信息);findGoodsSurplus=newJButton(查看商品剩余量);findSaleroom=newJButton(查看柜台销售额);member.addActionListener(this);common.addActionListener(this);findMemberInfo.addActionListener(this);findAllMemberInfo.addActionListener(this);findGoodsSurplus.addActionListener(this);findSaleroom.addActionListener(this);leftPanel.add(member);leftPanel.add(common);leftPanel.add(findMemberInfo);leftPanel.add(findAllMemberInfo);leftPanel.add(findGoodsSurplus);leftPanel.add(findSaleroom);rightPanel=newJPanel();showTime=newJTextField(12);rightPanelSon=newJPanel();showTime.setEditable(false);rightPanel.setLayout(newBorderLayout(1,1));rightPanel.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})publicstaticHashMapClubCard,CustomergetHashMap(){Filefile=Checkstand.file;if(file.length()==0){checkstand=newCheckstand();checkstand.definedForSystemWindow();}elsetry{ObjectInputStreamin=newObjectInputStream(newFileInputStream(file));checkstand.cardAndCustomer=(HashMapClubCard,Customer)in.readObject();in.close();}catch(IOExceptione){System.err.println(e.getMessage());}catch(ClassNotFoundExceptione1){System.err.println(文件未找到!);}returncheckstand.cardAndCustomer;}/***保存柜台信息对象串行化*/publicstaticvoidAddCheckstandInfo(){try{if(c==null)c=newCheckstand();ObjectOutputStreamout=newObjectOutputStream(newFileOutputStream(file));out.writeObject(c);out.close();}catch(IOExceptione){System.out.println(e.getMessage());}}/***从文件中读取柜台信息对象串行化*@throwsClassNotFoundException*/publicstaticvoidreadCheckstandInfo(){try{ObjectInputStreamin=newObjectInputStream(newFileInputStream(file));Objecto=in.readObject();if(o!=null)c=(Checkstand)o;elsec=newCheckstand();//在此处生成柜台对象再合适不过来in.close();}catch(IOExceptione){System.out.println(e.getMessage());}catch(ClassNotFoundExceptione1){System.out.println(文件未找到!);}}publicstaticvoidmain(String[]args){newSystemWindow();}@OverridepublicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubDateFormatformat=newSimpleDateFormat();Datedate=newDate();showTime.setText(北京时间:+format.format(date));finalStringPrintTime=format.format(date);//将信息打印在顾客的消费信息上if(e.getSource()==member){rightPanelSon.removeAll();//事先先移除rightPanelSon中的所有的组件this.paintAll(getGraphics());//相当于重画的功能,此部骤相当重要rightPanelSon.setLayout(newFlowLayout());rightPanelSon.add(newJPanel());rightPanelSon.add(newJPanel());finalJTextAreagoodsInfo=newJTextArea();//显示商品信息rightPanelSon.add(goodsInfo);goodsInfo.setEditable(false);//不可编辑goodsInfo.setText(good.toString());Stringid=JOptionPane.showInputDialog(请输入会员卡卡号:);try{number=Long.parseLong(id);}catch(NumberFormatExceptione1){JOptionPane.showMessageDialog(null,输入格式有误!);return;}finalHashMapClubCard,Customerc1=SystemWindow.getHashMap();if(c1==null){JOptionPane.showMessageDialog(null,会员信息不存在!,null,JOptionPane.WARNING_MESSAGE);return;}else{intflag=0;Customercustomer1=null;ClubCardcard2=null;for(ClubCardcard:c1.keySet()){if(number.equals(card.getId())){JTextAreaarea=newJTextArea();rightPanelSon.add(area);area.setText(c1.get(card).toString());//输出顾客信息customer1=c1.get(card);flag=customer1.getCustomeCard().getGrade();card2=card;}//if}//forif(customer1==null){JOptionPane.showMessageDialog(null,会员信息不存在!,null,JOptionPane.WARNING_MESSAGE);return;}fi
本文标题:超市收银系统(Java语言实现)
链接地址:https://www.777doc.com/doc-3611345 .html