简单的超市管理系统
发布日期:2021-05-04 09:31:34 浏览次数:21 分类:技术文章

本文共 16246 字,大约阅读时间需要 54 分钟。

view包:LoginFrame.java,AddGoodsFrame.java,CashierManagerFrame.java

/** *  */package com.qst.supermarket.view;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextField;import com.qst.supermarket.model.User;import com.qst.supermarket.service.UserService;/** * @author 12345678 * */public class LoginFrame extends JFrame{   	private JPanel mainJPanel;	// 声明标签 用户名: 密码: 账户类型:	private JLabel lblUsername;	private JLabel lblPassword;	private JLabel lblAccountTye;          	// 声明文本框 就是要输入信息用的	private JTextField tfUsername;	private JPasswordField tfPassword;	// 下拉框	private JComboBox abAccountType;	     JButton LoginButton,CancelButton;          //     service     UserService userService=new UserService();                         	public LoginFrame() {   		// TODO Auto-generated constructor stub		// 创建窗体主键放到方法里面		Init();		this.setSize(448, 366);   this.setBackground(new Color(255,255,255));		// 设置窗体居中		this.setLocationRelativeTo(null);				// 使最大化失效this.setResizable(false)		// this.setResizable(false);		this.setDefaultCloseOperation(LoginFrame.EXIT_ON_CLOSE);		this.setVisible(true);	}	/**	 * 	 */	private void Init() {   		// TODO Auto-generated method stub		// 容器,依赖JFrame 相当于JFrame是桌子 JPanel是桌子上的布				// 设置不使用布局管理 	mainJPanel.setLayout(null);		// 白色new Color(255,255,255) 黑色new Color(0,0,0)		mainJPanel.setBackground(new Color(255, 255, 255));		// 标签		lblUsername = new JLabel();		lblPassword = new JLabel();		lblAccountTye = new JLabel();		// 存放文本框		tfUsername = new JTextField("请输入账号:");				//		加上按键 登入 取消				LoginButton=new JButton("登入");		CancelButton=new JButton("取消");									// 密码用JPasswordFiled();它们是不一样的		tfPassword = new JPasswordField();		// tfAccountTye = new JTextField();		// 下拉框 用于选择 收银员 系统管理员		abAccountType = new JComboBox(new String[] {    "收银员", "管理员" });						// 可以设置成图标		lblUsername				.setIcon(new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("img/account.png")));		lblPassword				.setIcon(new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("img/password.png")));		lblAccountTye				.setIcon(new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("img/选择身份.png")));		// 设置标签大小		lblUsername.setBounds(100, 50, 100, 50);		// 设置文本框大小		tfUsername.setBounds(160, 50, 200, 50);		lblPassword.setBounds(100, 100, 100, 50);		lblPassword.requestFocus();		tfPassword.setBounds(160, 100, 200, 50);		lblAccountTye.setBounds(100, 150, 100, 50);		abAccountType.setBounds(160, 150, 200, 50);						// 按钮		LoginButton = new JButton("登录");		CancelButton = new JButton("取消");						LoginButton.setBounds(170, 200,60, 30);		CancelButton.setBounds(249, 200, 60, 30);			// 向mianJpanel中添加组件		mainJPanel.add(lblUsername);		mainJPanel.add(lblPassword);		mainJPanel.add(lblAccountTye);		mainJPanel.add(tfUsername);		mainJPanel.add(tfPassword);		mainJPanel.add(abAccountType);        mainJPanel.add(LoginButton);        mainJPanel.add(CancelButton);                                        		//		文本框监听事件  //		使用MouseAListener要实现所有鼠标事件的方法不方便		//		用MouseAdaptet方法很方便只需使用一个就好 		tfUsername.addMouseListener(new MouseAdapter() {   //			鼠标释			public void mouseReleased(MouseEvent e) {   				tfUsername.setText(" ");//鼠标释放时文本框内容为空 			}					});				//	登录按钮加监听 即点击登录按钮执行这个操作					LoginButton.addActionListener(new ActionListener() {   						@Override			public void actionPerformed(ActionEvent e) {   //				获取页面值			String username=tfUsername.getText();						username.trim();			//			getPassword(); 是字符数组char[]  	 	    String password=new String(tfPassword.getPassword());			String userType=(String)abAccountType.getSelectedItem();			 //			System.out.println(username);//			System.out.println(password);//			System.out.println(userType);//							if (username==null|| username.length()==0||username.equals("请输入账号:")) {   				username.trim();//				JOptionPane.showMessageDialog(parentComponent, message); parentComponent 父组件 message是提示消息//				设置窗体居中 匿名内部类访问外部类需要 调用外部类名 LoginFrame.this//				JOptionPane.showMessageDialog(null, "请输入用户名");				JOptionPane.showMessageDialog( LoginFrame.this, "请输入用户名!");				return;			}									if (password==null|| password.length()==0) {   			 				JOptionPane.showMessageDialog( LoginFrame.this, "请输入密码!");			}																										//			查询数据库:根据输入的用户名和密码进行比较   view service  dao--->table 					   			User user=userService.login(username, password, userType);		//			   System.out.println(user);			if (user==null) {   				JOptionPane.showMessageDialog( LoginFrame.this, "用户名或者密码不正确");                 return; 			}else {   				if(userType.equals("收银员")){   										System.out.println("欢迎进入收银员界面");					//					主窗体关闭但是程序仍然运行					LoginFrame.this.dispose();										//					启动另一个窗体					new CashierManagerFrame(user);				}				if(userType.equals("管理员")){   										System.out.println("欢迎进入管理员界面");										LoginFrame.this.dispose();					new AdminManagerFrame(user);				}							}          							}		});												        	}	/**	 * @param args	 */    public static void main(String[] args) throws Exception {   		new LoginFrame();	}}

AddGoods.java

/**
*
*/

package com.qst.supermarket.view;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.File;import java.util.Collection;import java.util.List;import java.util.Map;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import com.qst.supermarket.model.Goods;import com.qst.supermarket.service.GoodsService;import com.qst.supermarket.service.GoodsTypeService;/** * @author 12345678 * */public class AddGoodsFrame  extends JFrame implements ActionListener  {     //	接收一个父窗体,	private AdminManagerFrame adminManagerFrame;			private JPanel mainPanel,btnPanel ;	private JComboBox goodstypeCombox;	private JTextField g_barcode,g_name,g_price,g_num;	private JButton addBtn,batchBtn;		private Map
goodsTypeMap; private GoodsTypeService goodsTypeSevice=new GoodsTypeService(); private GoodsService goodsService=new GoodsService(); public AddGoodsFrame(AdminManagerFrame adminManagerFrame) { super(); this.adminManagerFrame=adminManagerFrame; // 创建窗体主键放到方法里面 Init(); this.setSize(448, 366); // 设置标题 this.setTitle("添加商品"); // 设置窗体居中 this.setLocationRelativeTo(null); // 使最大化失效this.setResizable(false) // this.setResizable(false); this.setDefaultCloseOperation(LoginFrame.DISPOSE_ON_CLOSE); this.setVisible(true); } /** * */ private void Init() { // TODO Auto-generated method stub // 把数据库中的数据库goodsType的数据导入到页面中 goodsTypeMap=goodsTypeSevice.getAllGoodsType(); Collection
values=goodsTypeMap.values();// String[] type=new String[values.size()]; String[] typeNameArray=new String[values.size()]; if (values!=null||values.size()>0) { int i=0; for (String goodsTypeName : values) { typeNameArray[i]=goodsTypeName; i++; } } mainPanel = new JPanel(new GridLayout(6,1,0,20)); goodstypeCombox=new JComboBox
(typeNameArray); goodstypeCombox.setSize(60, 40); g_barcode=new JTextField("商品条形码"); g_name=new JTextField("商品名称"); g_price=new JTextField("商品价格"); g_num=new JTextField("商品数量"); btnPanel=new JPanel(new FlowLayout(FlowLayout.CENTER,20,2)); addBtn=new JButton("添加"); batchBtn=new JButton("批量添加"); addBtn.addActionListener(this); batchBtn.addActionListener(this); mainPanel.add(goodstypeCombox); mainPanel.add(g_barcode); mainPanel.add(g_name); mainPanel.add(g_price); mainPanel.add(g_num); btnPanel.add(addBtn); btnPanel.add(batchBtn); mainPanel.add(btnPanel); // 鼠标事件 g_barcode.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { g_barcode.setText(" "); } }); g_name.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { g_name.setText(" "); } }); g_price.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { g_price.setText(" "); } }); g_num.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { g_num.setText(" "); } }); // 别忘了加上这块布到当前页面 this.add(mainPanel); } public static void main(String[] args) { new AddGoodsFrame(null); } /* (non-Javadoc) * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ @Override public void actionPerformed(ActionEvent e) { if (e.getSource()==addBtn) { // 下拉框 String tname=(String)goodstypeCombox.getSelectedItem(); // 通过tname获取tcode String tcode=null; for ( Map.Entry
entry : goodsTypeMap.entrySet()) { String typeName=entry.getValue(); if(typeName.equals(tname)){ tcode=entry.getKey(); } } String barcode=g_barcode.getText(); String name=g_name.getText();// 把文本框里面的值解析为相应类型的值 double price=Double.parseDouble(g_price.getText()); Integer num=Integer.valueOf(g_num.getText().trim() ); Goods goods=new Goods(barcode,price,num,name,tcode,tname); // 保存数据 boolean flag=goodsService.saveGoods(goods); // 加入到父表格 if (flag) { // 表格列名相对应 Object[] rowData=new Object[]{ name,barcode,price,num,tcode,tname}; this.adminManagerFrame.goodsTableModel.addRow(rowData);// while (true) { JOptionPane.showMessageDialog(this, "添加成功");// break;// } }else{ JOptionPane.showMessageDialog(this, "保存数据错误请与管理员联系"); } } if (e.getSource()==batchBtn) { JFileChooser fc=new JFileChooser(); int result=fc.showOpenDialog(AddGoodsFrame.this); // System.out.println(result); if(result==JFileChooser.APPROVE_OPTION){ // 获取文件 File file=fc.getSelectedFile(); List
goodlist=goodsService.readXMLloaddata(file); boolean flag=goodsService.saveGoods(goodlist); if (flag) { JOptionPane.showMessageDialog(this, "保存成功"); return; }else { JOptionPane.showMessageDialog(this, "保存失败"); return; } }else if(result==JFileChooser.CANCEL_OPTION){ JOptionPane.showMessageDialog(this, "没有选择文件"); return; } else { JOptionPane.showMessageDialog(this, "操作失误请与管理员联系"); return; } } } }

CashierManagerFrame.java

/** *  */package com.qst.supermarket.view;import java.awt.BorderLayout;import java.awt.Component;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Date;import java.util.UUID;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import com.qst.supermarket.model.Goods;import com.qst.supermarket.model.User;import com.qst.supermarket.service.GoodsService;import com.qst.supermarket.utils.RegexUtils;/** * @author 12345678 * */public class CashierManagerFrame extends JFrame implements ActionListener {   	private static User cashier;		private JPanel mainPanel,infoPanel,InputPanel,cashierPanel;	private JLabel 	lblCashier,lblName,lblGname,lblGnprice,lblGnum,lblGtotal,lblGnameValue,lblGnpriceValue,lblGnumValue,lblGtotalValue;		private JLabel 	lblBarcode, lblBuynum;	private JTextField tfBarcode,tfBuynum;	private GoodsService goodsService=new GoodsService();		private Goods goods;	public CashierManagerFrame( User cashier) {   		this.cashier=cashier;				// 创建窗体主键放到方法里面		Init();		this.setSize(800, 300);		// 设置窗体居中		this.setLocationRelativeTo(null);		// 使最大化失效this.setResizable(false)		// this.setResizable(false);		this.setDefaultCloseOperation(LoginFrame.DISPOSE_ON_CLOSE);		this.setVisible(true);	}	/**	 * 	 */	private void Init() {   		mainPanel=new JPanel(new BorderLayout());		mainPanel.add(getInfoPanel(),BorderLayout.NORTH);     	mainPanel.add(getInputPanel(),BorderLayout.CENTER);     	mainPanel.add(getCashierPanel(),BorderLayout.SOUTH);		//		记住mainPanel要加入窗体里面		this.add(mainPanel);			}		/**	 * @return	 */	private Component getCashierPanel() {   				cashierPanel=new JPanel(new FlowLayout(FlowLayout.LEFT,20,10));					lblCashier=new JLabel("收银员");		lblName=new JLabel(cashier.getUsername());				cashierPanel.add(lblCashier);		cashierPanel.add(lblName);//		不可以返回空值否则  Exception in thread "main" java.lang.NullPointerException		return cashierPanel;	}	/**	 * @return	 */	private Component getInputPanel() {   		InputPanel=new JPanel(new FlowLayout(FlowLayout.LEFT,40,30));	    //	商品条形码			lblBarcode=new JLabel("商品条形码");		tfBarcode=new JTextField(null,null,10);				//		商品数量		lblBuynum=new JLabel("购买数量");		tfBuynum=new JTextField(null,null,10);				tfBarcode.addActionListener(this);		tfBuynum.addActionListener(this);				 		InputPanel.add(lblBarcode);		InputPanel.add(tfBarcode);				InputPanel.add(lblBuynum);		InputPanel.add(tfBuynum);				return InputPanel;	}	/**	 * @return	 */	private Component getInfoPanel() {   		infoPanel=new JPanel(new FlowLayout(FlowLayout.LEFT,40,30));		lblGname=new JLabel("商品名称");		lblGnprice=new JLabel("商品单价");		lblGnum=new JLabel("库存数量");				lblGtotal=new JLabel("商品总额");				lblGnameValue=new JLabel();		lblGnpriceValue=new JLabel();		lblGnumValue=new JLabel();				lblGtotalValue=new JLabel();						infoPanel.add(lblGname);		infoPanel.add(lblGnameValue);		infoPanel.add(lblGnprice);		infoPanel.add(lblGnpriceValue);		infoPanel.add(lblGnum);		infoPanel.add(lblGnumValue);		infoPanel.add(lblGtotal);		infoPanel.add(lblGtotalValue);								return infoPanel;	}	public static void main(String[] args) {   		User cashier=new User();		cashier.setUsername("hou");		new CashierManagerFrame(cashier);	}	/* (non-Javadoc)	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)	 */	@Override	public void actionPerformed(ActionEvent e) {   				String barcode=tfBarcode.getText();		if(barcode==null||barcode.length()==0){   									JOptionPane.showMessageDialog(this, "请输入条形码");			return;		}else{   			//			查询条形码查询商品	     goods=goodsService.selectedGoodsByBarcode(barcode);			//			System.out.println(goods);			if (goods!=null) {   								//				商品数据显示在lblVaule中								lblGnameValue.setText(goods.getGname());;				lblGnpriceValue.setText(String.valueOf(goods.getPrice()));;				lblGnumValue.setText(String.valueOf(goods.getNum()));;								lblGtotalValue.setText("0.0");;								//				使光标定位到商品数量文本框				tfBuynum.requestFocus();							}else{   												JOptionPane.showMessageDialog(this, "输入的条形码错误,请重新输入");				return;							}		}				if(e.getSource()==tfBuynum){       	       	   String amount=tfBuynum.getText();//    	   判断是否是数字    	     RegexUtils.isDigits(amount);    	   if (!RegexUtils.isDigits(amount)) {       			JOptionPane.showMessageDialog(this, "输入商品数量有误请重新输入");				return;		}     	   //    	   购买数量    	   int buyAmmount=Integer.parseInt(amount);//    	   库存数量    	   int repAmmount=goods.getNum();    	      	       	   int result=repAmmount-buyAmmount;    	       	   if (result<0) {       		   JOptionPane.showMessageDialog(this, "库存不足请联系管理员");				return;		   }    	       	   if (result>0) {        	   double price=goods.getPrice();    	   double total=price*buyAmmount;    	   lblGtotalValue.setText(String.valueOf(total));    		   JOptionPane.showMessageDialog(this, "订单成功");    		   return;    	   }    	  			    	       	       	   //    	   保存订单记录    	   //    	   订单ID    	   String orderid=UUID.randomUUID().toString();//    	   员工id    	   Integer cashierName=cashier.getId();    	   //    	   系统时间    	    Date date=new Date();    	   // 条形码    	    String barcode1=goods.getBarcode();    	       	    //    	    保存订单数量    	    Integer num=goods.getNum();		}	    	          }	}
上一篇:整型,长整型(long)和short
下一篇:public,protect,private

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年03月29日 13时47分49秒