java实现商品信息管理系统

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

java实现商品信息管理系统

  2021-04-02 我要评论

超市商品管理系统,供大家参考,具体内容如下

题目要求

超市中商品分为四类,分别是食品、化妆品、日用品和饮料。每种商品都包含商品名称、价格、库存量和生产厂家、品牌等信息。主要完成对商品的销售、统计和简单管理。这个题目相对简单,可以用一张表实现信息的保存和处理,因此不再给出数据库设计参考。

功能要求

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

运行结果如图:

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们