溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java怎么實現(xiàn)商品信息管理系統(tǒng)

發(fā)布時間:2021-04-15 10:19:52 來源:億速云 閱讀:201 作者:小新 欄目:編程語言

小編給大家分享一下java怎么實現(xiàn)商品信息管理系統(tǒng),希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

具體內(nèi)容如下

題目要求

超市中商品分為四類,分別是食品、化妝品、日用品和飲料。每種商品都包含商品名稱、價格、庫存量和生產(chǎn)廠家、品牌等信息。主要完成對商品的銷售、統(tǒng)計和簡單管理。這個題目相對簡單,可以用一張表實現(xiàn)信息的保存和處理,因此不再給出數(shù)據(jù)庫設(shè)計參考。

功能要求

(1)銷售功能。

  購買商品時,先輸入類別,然后輸入商品名稱,并在庫存中查找該商品的相關(guān)信息。如果有庫存量,輸入購買的數(shù)量,進行相應(yīng)計算。如果庫存量不夠,給出提示信息,結(jié)束購買。

(2)商品簡單管理功能。

  添加功能:主要完成商品信息的添加。
  查詢功能:可按商品類別、商品名稱、生產(chǎn)廠家進行查詢。若存在相應(yīng)信息,輸出所查詢的信息,若不存在該記錄,則提示“該記錄不存在!”。
  修改功能:可根據(jù)查詢結(jié)果對相應(yīng)的記錄進行修改。
  刪除功能:主要完成商品信息的刪除。先輸入商品類別,再輸入要刪除的商品名稱,根據(jù)查詢結(jié)果刪除該物品的記錄,如果該商品不在物品庫中,則提示“該商品不存在”。

(3)統(tǒng)計功能。

  輸出當(dāng)前庫存中所有商品的總數(shù)及詳細信息;可按商品的價格、庫存量、生產(chǎn)廠家進行統(tǒng)計,輸出統(tǒng)計信息時,要按從大到小進行排序。

(7)商品信息存盤:將當(dāng)前程序中的商品信息存入文件中。

(8)讀出信息:從文件中將商品信息讀入程序。

問題的解決方案

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

設(shè)計思路

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

實現(xiàn):

建立并連接數(shù)據(jù)庫與基本表

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

結(jié)構(gòu)設(shè)計

該系統(tǒng)用于對商品的基本信息進行管理,主要包括添加、修改、查詢和刪除商品基本信息,為了方便,全部操作均在界面中完成。由此,將該系統(tǒng)結(jié)構(gòu)設(shè)計為登錄模塊、顧客模塊、商家模塊。由于涉及界面設(shè)計,因此調(diào)用了java.awt.、java.awt.event.、javax.swing.、java.util.、javax.swing.event.*、java.sql.*等包。

實現(xiàn)登錄模塊

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

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("商品信息管理系統(tǒng)");
 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);
 }
}

運行結(jié)果

java怎么實現(xiàn)商品信息管理系統(tǒng)

實現(xiàn)顧客操作界面

當(dāng)選擇“顧客”時,單擊“登錄”按鈕就可以進入顧客操作系統(tǒng)了,然后就可以對摸個學(xué)生的信息進行輸入、修改和刪除,也能對同學(xué)的信息進行查詢和對程序進行查詢。當(dāng)打算離開時,還要有一個選項用來退出學(xué)生信息管理系統(tǒng)。根據(jù)設(shè)計構(gòu)想,首先要搭建一個界面,然后把顧客的操作分為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);
 }
}

運行結(jié)果

java怎么實現(xiàn)商品信息管理系統(tǒng)

商家操作界面相比顧客操作界面多了商品信息的增加﹑刪除和修改功能,其實現(xiàn)方法與顧客操作界面類似,在此不再贅述。

添加商品信息

每個按鈕都對應(yīng)著一個操作界面,當(dāng)點擊商家操作下的“增加商品信息”按鈕時,將彈出如圖所示的界面,它調(diào)用了AddGoods.java類實現(xiàn)該功能。通過對“增加信息”這一子菜單設(shè)置監(jiān)聽,彈出界面。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("生產(chǎn)廠家");
 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);
 }
 }
}

運行效果:

java怎么實現(xiàn)商品信息管理系統(tǒng)

刪除商品信息

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

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, "刪除失?。?quot;);
 } catch (SQLException e1) {
 JOptionPane.showMessageDialog(null, "此商品不存在!");
 e1.printStackTrace();
 }
 }
 if (e.getSource() == Reset) {
 Number.setText(null);
 }
 if (e.getSource() == Exit)
 this.setVisible(false);
 }
}

如圖,只需輸入商品編號便可刪除該商品的全部信息。

java怎么實現(xiàn)商品信息管理系統(tǒng)

修改商品信息

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

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);
 }
 }
}

運行結(jié)果

java怎么實現(xiàn)商品信息管理系統(tǒng)

查詢商品信息

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

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("生產(chǎn)廠家");
 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("生產(chǎn)廠家: "+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);
}}}

運行結(jié)果

java怎么實現(xiàn)商品信息管理系統(tǒng)

退出系統(tǒng)

當(dāng)在對商品進行增加﹑刪除﹑修改和查詢的界面時,點擊“退出”按鈕,即可彈出如圖4-7所示界面,它調(diào)用了

UsingExit.java類實現(xiàn)該功能,部分代碼如下:

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);
 }
}

運行結(jié)果如圖:

java怎么實現(xiàn)商品信息管理系統(tǒng)

看完了這篇文章,相信你對“java怎么實現(xiàn)商品信息管理系統(tǒng)”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI