⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sale.java

📁 简单的小型超市管理
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;

class SaleAdd extends JDialog implements ActionListener
{
	ConnectionSQL conSql;
	JButton okButn;
	JTextField numT, nameT, priceT, countT, cusT, dataT;
	SaleAdd(JFrame f, String s, boolean b)
	{
		super(f, s, b);
		Container con = getContentPane();	
		con.setLayout(new BorderLayout());
		setSize(250, 330); 
		setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);   //关闭
		okButn = new JButton("确定");
		okButn.addActionListener(this);

		Date nowTime = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"),
						 dateStr = new SimpleDateFormat("yyyy年MM月dd日");
		
		numT = new JTextField(dateFormat.format(nowTime));
		numT.setEditable(false);
		nameT = new JTextField(10);
		priceT = new JTextField(5);
		countT = new JTextField(5);
		cusT = new JTextField(10);
		dataT = new JTextField(dateStr.format(nowTime));
		dataT.setEditable(false);
		
		Box box1, box2, box3, base;
		box1 = Box.createVerticalBox();
		box1.add(new JLabel("流水号:"));
		box1.add(Box.createVerticalStrut(20));
		box1.add(new JLabel("货物名称:"));
		box1.add(Box.createVerticalStrut(20));
		box1.add(new JLabel("价格:"));
		box1.add(Box.createVerticalStrut(20));
		box1.add(new JLabel("销售量:"));
		box1.add(Box.createVerticalStrut(20));
		box1.add(new JLabel("购买商:"));
		box1.add(Box.createVerticalStrut(20));
		box1.add(new JLabel("销售日期:"));
		box1.add(Box.createVerticalStrut(5));
		
		box2 = Box.createVerticalBox();
		box2.add(Box.createVerticalStrut(12));
		box2.add(numT);
		box2.add(Box.createVerticalStrut(12));
		box2.add(nameT);
		box2.add(Box.createVerticalStrut(12));
		box2.add(priceT);
		box2.add(Box.createVerticalStrut(12));
		box2.add(countT);
		box2.add(Box.createVerticalStrut(12));
		box2.add(cusT);
		box2.add(Box.createVerticalStrut(12));
		box2.add(dataT);
		box2.add(Box.createVerticalStrut(12));
		
		box3 = Box.createHorizontalBox();
		box3.add(Box.createHorizontalStrut(18));
		box3.add(box1);
		box3.add(Box.createHorizontalStrut(10));
		box3.add(box2);
		box3.add(Box.createHorizontalStrut(18));
		
		base = Box.createVerticalBox();
		base.add(box3);
		base.add(okButn);
		base.add(Box.createVerticalStrut(18));
		
		add(base);	
		validate();
	}
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成方法存根
		if(nameT.getText().trim().equals("") ||	priceT.getText().trim().equals("") || 
				cusT.getText().trim().equals("") ||	countT.getText().trim().equals(""))
			{
				JOptionPane.showMessageDialog(this, "有空值!", "提示", JOptionPane.WARNING_MESSAGE);
			}
			else
			{
				float price=0;
				int count=0;		
				boolean isdigit=true;
				try {
					price = Float.parseFloat(priceT.getText());
					count = Integer.parseInt(countT.getText());				
				}
				catch(NumberFormatException ee)
				{
					isdigit = false;
					JOptionPane.showMessageDialog(this, "价格和采购量应为数字!", "警告", JOptionPane.WARNING_MESSAGE);
				}
				if(isdigit)
				{
					int message = JOptionPane.showConfirmDialog(this, "是否确定销售?", "提示", JOptionPane.YES_NO_OPTION);
					if (message == JOptionPane.YES_OPTION && isdigit)
					{
						try {
							conSql = new ConnectionSQL();
							conSql.sql = conSql.con.createStatement();
							String selectSql = String.format("select * from CARGO where 名称 = '%s' and 库存量 >= %d",
									nameT.getText(), count);
							conSql.rs = conSql.sql.executeQuery(selectSql);
							if(conSql.rs.next())          //有记录时执行
							{
								String insertSql = String.format("insert into SALE values('%s','%s','%f','%d','%s','%s')", 
										numT.getText(), nameT.getText(), price, count, cusT.getText(), dataT.getText());
								conSql.sql.executeUpdate(insertSql);    //插入销售信息
								String updateSql = String.format("update CARGO set 库存量 = 库存量-%d where 名称 = '%s'", count, nameT.getText());
								conSql.sql.executeUpdate(updateSql);         //更新货物的库存量
							}
							else
							{
								JOptionPane.showMessageDialog(this, "无此货物或库存量不足\n请修正!", "警告", JOptionPane.WARNING_MESSAGE);
							}
						}
						catch(SQLException ee) {}
						nameT.setText(null);		priceT.setText(null);
						cusT.setText(null);			countT.setText(null);
						
						Date nowTime = new Date();
						SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"),
										 dateStr = new SimpleDateFormat("yyyy年MM月dd日");
						numT.setText(dateFormat.format(nowTime));
						dataT.setText(dateStr.format(nowTime));
					}
				}
			}
	}
}

class SaleList extends JDialog implements ActionListener
{
	ConnectionSQL conSql;
	SaleAdd SaAdd;
	JButton all, print, add;
	JTextField tFrom, tTo;
	JTable table;
	DefaultTableModel defaultModel = null;
	public String numS="", nameS="", priceS="", countS="", cusS="", dataS="";
	SaleList (Frame f, String s, boolean b)
	{
		super(f, s, b);
		Container con = getContentPane();	
		con.setLayout(new BorderLayout());
		JPanel pNorth = new JPanel(),
			   pCenter = new JPanel();
		 
		setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);   //关闭
		
		all = new JButton("全部");
		print = new JButton("打印");
		add = new JButton("销售");
		all.addActionListener(this);
		print.addActionListener(this);
		add.addActionListener(this);
		tFrom = new JTextField(8);
		tTo = new JTextField(8);
		
		pNorth.add(new JLabel("从:"));
		pNorth.add(tFrom);
		pNorth.add(new JLabel("到"));
		pNorth.add(tTo);
		pNorth.add(all);
		pNorth.add(print);
		pNorth.add(add);
		
		Object a[][] = new Object[0][0];
		Object n[] = {"流水号","货物名称","销价","销售量","购买商","销售日期"};    //表格的列名
		defaultModel = new DefaultTableModel(a,n);
		table = new JTable(defaultModel);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		pCenter.add(new JScrollPane(table));
		
		con.add(pNorth, BorderLayout.NORTH);
		con.add(pCenter, BorderLayout.CENTER);
		
		setSize(600, 500);
	}
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成方法存根
		if (e.getSource()==print)
		{
			JOptionPane.showMessageDialog(this, "无打印机", "提示", JOptionPane.WARNING_MESSAGE);
		}
		else if (e.getSource()==all)
		{
			int row = defaultModel.getRowCount()-1;  //清空表格***********
			while(row>=0)
			{
				defaultModel.removeRow(row);
				defaultModel.setRowCount(row);
				row--;
			}   //*********************************
			
			Object o[] = {"", "", "", "", "",""};
			try {
				conSql = new ConnectionSQL();
				conSql.sql = conSql.con.createStatement();
				conSql.rs = conSql.sql.executeQuery("select * from SALE");
				while(conSql.rs.next())
				{
					o[0] = conSql.rs.getString(1);
					o[1] = conSql.rs.getString(2);
					o[2] = conSql.rs.getString(3);
					o[3] = conSql.rs.getString(4);
					o[4] = conSql.rs.getString(5);
					o[5] = conSql.rs.getString(6);
					defaultModel.addRow(o);    //增加一行
				}
				conSql.con.close();
			}
			catch(SQLException ee)	{}
		}
		else if (e.getSource()==add)
		{
			SaAdd = new SaleAdd(null, "销售货物", true);
			SaAdd.setLocationRelativeTo(null);
			SaAdd.setVisible(true);
		}
	}
	
}

public class Sale {
	public static void main(String args[])
	{
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -