设为首页 | 成都一卡通
一卡通相关技术
超市收银系统(Java语言实现)
发布时间:2013-10-29 来源:成都一卡通

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.ObjectInputStream; 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; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; @SuppressWarnings("serial") public class SystemWindow extends JFrame implements ActionListener{ static Checkstand checkstand;//此变量单纯为了保存读至保存HashMap文件中的数据,相当于一个中间媒介 /** * 供显示的商品信息 */ Goods good; /** * 保存收银台信息的文件 */ static File file; static Checkstand c;//放在此处并不和前面的checkstand相矛盾,此变量是为了读取、保存柜台信息 static Long number;//输入的卡号 Timer time;//获取系统时间 JPanel leftPanel; JButton member;//会员服务 JButton common;//普通服务 JButton findMemberInfo;//查找会员信息 JButton findAllMemberInfo;//查看所有会员信息 JButton findGoodsSurplus;//查看商品剩余量 JButton findSaleroom;//查看柜台销售额 JPanel 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(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("查看商品剩余量"); findSaleroom=new JButton("查看柜台销售额"); 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=new JPanel(); showTime=new JTextField(12); rightPanelSon=new JPanel(); showTime.setEditable(false); rightPanel.setLayout(new BorderLayout(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" }) public 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>)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 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 i