JAVA實現商品信息管理系統

超市商品管理系統

點此下載源碼

題目要求

超市中商品分爲四類,分別是食品、化妝品、日用品和飲料。每種商品都包含商品名稱、價格、庫存量和生產廠家、品牌等信息。主要完成對商品的銷售、統計和簡單管理。這個題目相對簡單,可以用一張表實現信息的保存和處理,因此不再給出數據庫設計參考。
功能要求
(1)銷售功能。
  購買商品時,先輸入類別,然後輸入商品名稱,並在庫存中查找該商品的相關信息。如果有庫存量,輸入購買的數量,進行相應計算。如果庫存量不夠,給出提示信息,結束購買。
(2)商品簡單管理功能。
  添加功能:主要完成商品信息的添加。
  查詢功能:可按商品類別、商品名稱、生產廠家進行查詢。若存在相應信息,輸出所查詢的信息,若不存在該記錄,則提示“該記錄不存在!”。
  修改功能:可根據查詢結果對相應的記錄進行修改。
  刪除功能:主要完成商品信息的刪除。先輸入商品類別,再輸入要刪除的商品名稱,根據查詢結果刪除該物品的記錄,如果該商品不在物品庫中,則提示“該商品不存在”。
(3)統計功能。
  輸出當前庫存中所有商品的總數及詳細信息;可按商品的價格、庫存量、生產廠家進行統計,輸出統計信息時,要按從大到小進行排序。
(7)商品信息存盤:將當前程序中的商品信息存入文件中。
(8)讀出信息:從文件中將商品信息讀入程序。

問題的解決方案

根據系統功能要求,可以將問題解決分爲以下步驟:
(1)應用系統分析,建立該系統的功能模塊框圖以及界面的組織和設計;
(2)分析系統中的各個實體及它們之間的關係;
(3)根據問題描述,設計系統的類層次;
(4)完成類層次中各個類的描述;
(5)完成類中各個成員函數的定義;
(6)完成系統的應用模塊;
(7)功能調試;

設計思路

可以對超市商品進行管理的人員主要有超市的商家和顧客,商家可以對超市的商品進行增﹑刪﹑改﹑查操作,而顧客只能查詢和購買商品。增加商品時,要添加商品的全部信息(編號﹑類別﹑名稱﹑價格﹑庫存量﹑品牌﹑生產廠家),刪除時只需要輸入商品編號便可刪除該商品的全部信息,修改時要先輸入商品編號,然後再確定要修改該商品的哪一個值,以及要將該值修改爲什麼,查詢時只要輸入想要查詢商品的任意一個信息並選擇商品類別便可查出該商品的全部信息。

實現:

建立並連接數據庫與基本表

連接數據庫時需要用到JDBC,它由Java編程語言編寫的類和接口組成,是實現Java與各種數據庫連接的關鍵,提供了將Java與數據庫連接起來的程序接口,使用戶可以以SQL的形式編寫訪問請求,然後傳給數據庫,其結果再由這一接口返回,從而實現對數據庫中數據操作的目的。超市商品管理系統採用了MySQL作爲數據庫,所建的系統數據庫名爲“goods”。通過需求分析、概念設計與邏輯設計,可知該系統數據庫只需建立一個商品表即可

結構設計

該系統用於對商品的基本信息進行管理,主要包括添加、修改、查詢和刪除商品基本信息,爲了方便,全部操作均在界面中完成。由此,將該系統結構設計爲登錄模塊、顧客模塊、商家模塊。由於涉及界面設計,因此調用了java.awt.、java.awt.event.、javax.swing.、java.util.、javax.swing.event.*、java.sql.*等包。

實現登錄模塊

要生成一個界面,可應用AWT知識。設置其名字爲商品信息管理系統;設置佈局管理器爲(null)佈局管理器,方便往其中放組件;設置窗口大小和位置,還要設置窗口可見性。
生成界面後,接下來就需要實現每個功能,第一個功能就是要對操作對象的身份進行選擇,這裏要用下拉列表的形式進行選擇,也可以用單選按鈕來完成這個功能。在這項功能中,首先要選擇身份,所以要定義一個JLabel來說明,定義完JLabel後,就需要定義一個JComoBox,下拉列表框。 
輸入用戶名和密碼。需要用兩個JLabel來指明需要輸入用戶名和密碼。輸入用戶名需要定義一個JTextField,單文本框。同時輸入文本,但輸入密碼和輸入用戶名是不一樣的,它需要定義成JPasswordField,它的輸出結果爲“*****”這樣的形式。 
創建兩個按鈕,一個是登錄按鈕,另一個是取消登錄按鈕,用來輸入的用戶名和密碼及選擇的身份進行提交,然後根據選擇的身份來選擇需要進入那個界面,其代碼如下:

public class info_Manage extends JFrame implements ActionListener{
	private JLabel username = new JLabel("用戶名");
	private JTextField userName = new JTextField();		
	private JLabel psw = new JLabel("密碼");	
	private JPasswordField Psw = new JPasswordField();
	JLabel jlp=new JLabel("身份");
	String str[]={"顧客","商家"};
	JComboBox jcb=new JComboBox(str);	
	private JButton jb1 = new JButton("登錄");
	private JButton jb2 = new JButton("取消");	
	public info_Manage(){		
		this.setTitle("商品信息管理系統");
		this.setLayout(null);
		username.setBounds(100,50,100,20);
		this.add(username);		
		userName.setBounds(150,50,100,20);
		this.add(userName);
		psw.setBounds(100,100,100,20);
		this.add(psw);
		Psw.setBounds(150,100,100,20);
		this.add(Psw);
		jlp.setBounds(100,150,100,20);
		this.add(jlp);
		jcb.setBounds(150,150,100,20);
		this.add(jcb);
		jcb.addActionListener(this);
		jb1.setBounds(100,210,60,20);
		this.add(jb1);
		jb1.addActionListener(this);
		jb2.setBounds(200,210,60,20);
		this.add(jb2);     
		jb2.addActionListener(this);
		this.setVisible(true);
		this.setBounds(10,10,390,330);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {
		new info_Manage();
	}
	public void actionPerformed(ActionEvent e) {		
		if (e.getSource() == jb1) {
			String name=userName.getText();
			String password = new String(Psw.getPassword());
			if(name.length()==0&&password.length()!=0)
				JOptionPane.showMessageDialog( null, "請輸入用戶名");
			else  if(name.length()!=0&&password.length()==0)
				JOptionPane.showMessageDialog( null, "請輸入密碼");
			else if(name.length()==0&&name.length()==0)
					JOptionPane.showMessageDialog( null, "請輸入用戶名和密碼");
			else if(jcb.getSelectedIndex()==0&&name.length()!=0&&name.length()!=0)
				new custom_Manage();
			else if(jcb.getSelectedIndex()==1&&name.length()!=0&&password.length()!=0)
				new seller_Manage();
		}
		else if(e.getSource()==jb2)
			System.exit(0);
	}
}

運行結果
在這裏插入圖片描述

實現顧客操作界面

當選擇“顧客”時,單擊“登錄”按鈕就可以進入顧客操作系統了,然後就可以對摸個學生的信息進行輸入、修改和刪除,也能對同學的信息進行查詢和對程序進行查詢。當打算離開時,還要有一個選項用來退出學生信息管理系統。根據設計構想,首先要搭建一個界面,然後把顧客的操作分爲2大塊,分別是商品信息查詢和退出登錄,其部分代碼如下:

class custom_Manage extends JFrame implements ActionListener{
	JMenu cm=new JMenu("請選擇您需要的操作:");
	JButton cm1=new JButton("商品信息查詢");
	JButton cm2=new JButton("退出登錄");
	public void actionPerformed(ActionEvent e){
		 if(e.getSource()==cm1)new SetGoods();
		else if(e.getSource()==cm2)	this.setVisible(false);
	}
}

運行結果
在這裏插入圖片描述
商家操作界面相比顧客操作界面多了商品信息的增加﹑刪除和修改功能,其實現方法與顧客操作界面類似,在此不再贅述。

添加商品信息

每個按鈕都對應着一個操作界面,當點擊商家操作下的“增加商品信息”按鈕時,將彈出如圖所示的界面,它調用了AddGoods.java類實現該功能。通過對“增加信息”這一子菜單設置監聽,彈出界面。AddGoods.java的部分代碼如下:

class AddGoods extends JFrame implements ActionListener {
	JLabel JL = new JLabel("添加基本信息:");
	JLabel number = new JLabel("商品編號");
	JTextField Number = new JTextField();
	
	JLabel JClass=new JLabel("類別");
	String str[]={"食品","化妝品","日用品","飲料"};
	JComboBox jcb=new JComboBox(str);
	
	JLabel name = new JLabel("商品名稱");
	JTextField Name = new JTextField();
	JLabel price=new JLabel("商品價格");
	JTextField Price = new JTextField();
	JLabel storage= new JLabel("庫存量");
	JTextField Storage = new JTextField();
	JLabel brand= new JLabel("品牌");
	JTextField Brand = new JTextField();
	JLabel vender = new JLabel("生產廠家");
	JTextField Vender = new JTextField();
	
	JTextField jt=new JTextField(10);
	JButton Add = new JButton("添加");
	JButton Reset = new JButton("重置");
	JButton Exit = new JButton("退出");
	String sql = "";

	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==Add) {
			String snumber=Number.getText();
			String svender=Vender.getText();
			String sname=Name.getText();
			String sprice=Price.getText();
			String sstorage=Storage.getText();
			String sbrand=Brand.getText();
			try {
				Connection cot=ConnectionFactory.getConnection();
				Statement stm=cot.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE ,ResultSet.CONCUR_UPDATABLE );
				int s=jcb.getSelectedIndex();
				String jc=null;
				if(s==0)jc="食品";
				else if(s==1)jc="化妝品";
				else if(s==2)jc="日用品";
				else if(s==3)jc="飲料";
sql="insert into goods values('"+snumber+"','"+jc+"','"+sname+"',"+sprice+","+sstorage+",'"+sbrand+"','"+svender+"')";
				int n=stm.executeUpdate(sql);
				if(n!=0)JOptionPane.showMessageDialog(null,"添加成功!");
				else JOptionPane.showMessageDialog(null,"該商品已存在!");
			}catch(Exception ee) {
				ee.printStackTrace();
			}
		}
		if(e.getSource()==Reset) {
			Number.setText(null);
			Name.setText(null);
			Vender.setText(null);
			Price.setText(null);
			Storage.setText(null);
			Brand.setText(null);
		}
		if(e.getSource()==Exit) {
			this.setVisible(false);
		}
	}
}

運行結果在這裏插入圖片描述

刪除商品信息

當選擇商家操作系統下的刪除商品信息的按鈕時,將彈出圖4-4所示的界面,它調用了DeleteGoodst.java類實現該功能,其部分代碼如下:

class DeleteGoods extends JFrame implements ActionListener {
	JMenu JL = new JMenu("刪除基本信息");
	JLabel number = new JLabel("商品編號");
	JTextField Number = new JTextField();
	JButton Del = new JButton("刪除");
	JButton Reset = new JButton("重置");
	JButton Exit = new JButton("退出");
	String sql = "";

public void actionPerformed(ActionEvent e) {
		if (e.getSource() == Del) {
			Statement stm=null;
			Connection cot;
			try {
				cot=ConnectionFactory.getConnection();
				stm= cot.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE ,ResultSet.CONCUR_UPDATABLE );
				sql ="delete from goods where number='"+Number.getText()+"'";
				int n = stm.executeUpdate(sql);
				if (n!=0)
					JOptionPane.showMessageDialog(null, "刪除成功!");
				else
				JOptionPane.showMessageDialog(null, "刪除失敗!");
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(null, "此商品不存在!");
				e1.printStackTrace();
			}
		}
		if (e.getSource() == Reset) {
			Number.setText(null);
		}
		if (e.getSource() == Exit)
			this.setVisible(false);
	}
}                     

如圖,只需輸入商品編號便可刪除該商品的全部信息。
在這裏插入圖片描述

修改商品信息

當選擇商家操作系統下的“修改信息”按鈕時,將彈出界面,只要輸入商品的編號,然後選擇所要修改的該編號商品的列名,最後輸入想要將其修改成爲的值,即可修改該商品的某一項信息。用了GetGoods.java類實現該功能。其部分代碼如下:

class GetGoods extends JFrame implements ActionListener{
	JLabel JL = new JLabel("修改商品信息", JLabel.CENTER);
	JLabel number = new JLabel("請輸入您要修改的商品編號");
	JTextField Number = new JTextField();	
	JLabel massage = new JLabel("請輸入您要修改的商品信息");
	JTextField Massage = new JTextField();	
	JLabel afterget=new JLabel("您想要將該列信息修改爲:");
	JTextField Afterget = new JTextField();	
	JTextField jt=new JTextField(10);
	JButton Get = new JButton("修改");
	JButton Reset = new JButton("重置");
	JButton Exit = new JButton("退出");
	String sql = "";
public void actionPerformed(ActionEvent e){
		if(e.getSource()==Get){
			Statement stm=null;
			Connection cot;
			try{
				cot=ConnectionFactory.getConnection();				stm=cot.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE ,ResultSet.CONCUR_UPDATABLE );
				sql="update goods set "+Massage.getText()+"='"+Afterget.getText()+"' where number='"+Number.getText()+"'";
				int n=stm.executeUpdate(sql);
				if(n!=0)JOptionPane.showMessageDialog(null,"修改成功!");
				else JOptionPane.showMessageDialog(null,"修改失敗!");
			}catch(Exception er){
				er.printStackTrace();
			}
		}
		if(e.getSource()==Reset){
			Number.setText(null);
			Massage.setText(null);
			Afterget.setText(null);
		}
		if(e.getSource()==Exit) {
			this.setVisible(false);
		}
	}
}

運行結果在這裏插入圖片描述

查詢商品信息

當選擇顧客或者商家操作系統下的“查詢商品信息”按鈕時,將彈出如圖所示的界面,它調用了SetGoods.java類實現該功能,部分代碼如下:

class SetGoods extends JFrame implements ActionListener {
	JLabel JL = new JLabel("請用以下任意一種方式查詢您想要的東西", JLabel.CENTER);
	JLabel number = new JLabel("商品編號");
	JTextField Number = new JTextField();	
	JLabel JClass=new JLabel("類別");
	String str[]={"無","食品","化妝品","日用品","飲料"};
	JComboBox jcb=new JComboBox(str);	
	JLabel name = new JLabel("商品名稱");
	JTextField Name = new JTextField();
	JLabel price=new JLabel("商品價格");
	JTextField Price = new JTextField();
	JLabel brand= new JLabel("品牌");
	JTextField Brand = new JTextField();
	JLabel vender = new JLabel("生產廠家");
	JTextField Vender = new JTextField();	
	JTextField jt=new JTextField(10);
	JButton Set = new JButton("查詢");
	JButton purchase = new JButton("購買");
	JButton Reset = new JButton("重置");
	JButton Exit = new JButton("退出");
	String sql = "";
public void actionPerformed(ActionEvent e) {
		if (e.getSource() == Set) {
			Statement stm=null;
			Connection cot;
			try{
				cot=ConnectionFactory.getConnection();
stm=cot.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE ,ResultSet.CONCUR_UPDATABLE );
				if(Number.getText()!=null)
					sql="select * from goods where number='"+Number.getText()+"'";
				else if(Name.getText()!=null)
					sql="select * from goods where name='"+Name.getText()+"'";
				else if(Price.getText()!=null)
					sql="select * from goods where price='"+Price.getText()+"'";
				else if(Brand.getText()!=null)
					sql="select * from goods where brand='"+Brand.getText()+"'";
				else if(Vender.getText()!=null)
					sql="select * from goods where vender='"+Vender.getText()+"'";
				ResultSet rs=stm.executeQuery(sql);
				while(rs.next()) {
					System.out.println("商品編號: "+Number.getText());
					int s=jcb.getSelectedIndex();
					if(s==0)
					JOptionPane.showMessageDialog( null, "請選擇商品類別!" );
					else if(s==1)System.out.println("商品類別: 食品");
					else if(s==2)System.out.println("商品類別: 化妝品");
					else if(s==3)System.out.println("商品類別: 日用品");
					else if(s==4)System.out.println("商品類別: 飲料");
					System.out.println("商品名稱: "+rs.getString("name"));
					System.out.println("價格: "+rs.getString("price"));
					System.out.println("庫存量: "+rs.getString("storage"));
					System.out.println("品牌: "+rs.getString("brand"));
					System.out.println("生產廠家: "+rs.getString("vender"));
				}
			}catch(Exception ee){
				JOptionPane.showMessageDialog( null, "該商品不存在!" );
				ee.printStackTrace();
			}
		}
		else if(e.getSource()==purchase){new Purchase();}
		else if(e.getSource()==Reset){
			Number.setText(null);
			Name.setText(null);
			Vender.setText(null);
			Price.setText(null);
			Brand.setText(null);
		}
		else if(e.getSource()==Exit) {
this.setVisible(false);
}}}

運行結果在這裏插入圖片描述

退出系統

當在對商品進行增加﹑刪除﹑修改和查詢的界面時,點擊“退出”按鈕,即可彈出如圖4-7所示界面,它調用了UsingExit.java類實現該功能,部分代碼如下:

class UsingExit extends JFrame implements ActionListener{
	JLabel Info=new JLabel("確認退出?");
	JButton JExit=new JButton("確認");
	JButton Cancel=new JButton("取消");
public void actionPerformed(ActionEvent e){
		if(e.getSource()==JExit)
			System.exit(0);
		else if(e.getSource()==Cancel)
			setVisible(false);
	}
}

運行結果如圖:
在這裏插入圖片描述

點此下載源碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章