加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

7.基础知识小应用

发布时间:2020-12-12 13:28:39 所属栏目:百科 来源:网络整理
导读:根据到目前为止已经复习了的基础知识,在git上找了个小程序写着玩玩,下面记录过程。 一,设计数据库 ER图 建表 建表我使用的是MySQL数据库,用的是navicat软件,ER图照搬git上的,由于作者使用的是Oracle建表,所以数据类型有些出入,问题不大。 商品表 CRE

根据到目前为止已经复习了的基础知识,在git上找了个小程序写着玩玩,下面记录过程。

一,设计数据库

ER图

建表

建表我使用的是MySQL数据库,用的是navicat软件,ER图照搬git上的,由于作者使用的是Oracle建表,所以数据类型有些出入,问题不大。

商品表

CREATE TABLE `goods` (
  `GID` int(10) NOT NULL COMMENT ‘商品编号,自动生成‘,`GNAME` varchar(20) NOT NULL COMMENT ‘商品名称,唯一约束‘,`GPRICE` decimal(18,1) NOT NULL COMMENT ‘商品价格‘,`GNUM` int(11) NOT NULL COMMENT ‘商品数量‘,PRIMARY KEY (`GID`),UNIQUE KEY `GNAME` (`GNAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

销售员表

CREATE TABLE `salesman` (
  `SID` int(10) NOT NULL COMMENT ‘营业员编号,自动生成‘,`SPASSWORD` varchar(20) NOT NULL COMMENT ‘营业员密码‘,`SNAME` varchar(10) NOT NULL COMMENT ‘营业员姓名,用于登录收银,唯一约束‘,PRIMARY KEY (`SID`),UNIQUE KEY `SNAME` (`SNAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

商品销售表

CREATE TABLE `gsales` (
  `GSID` int(10) NOT NULL COMMENT ‘销售编号,自动生成‘,`GID` int(10) NOT NULL COMMENT ‘商品编号‘,`SID` int(10) NOT NULL COMMENT ‘营业员编号‘,`SDATE` datetime NOT NULL COMMENT ‘销售日期‘,`SNUM` int(11) NOT NULL COMMENT ‘销售数量‘,PRIMARY KEY (`GSID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

二、

?

一、主体结构如下:

?

dao:处理数据功能

db:数据库相关

entity:模型相关

page:页面相关

tools:需要用到的工具类

二、

1.我的思路是,从简单到复杂开始写,首先根据已经建好的数据库表编写实体类。

Goods.java

package entity;

public class Goods {
    //商品编号,自动生成
    private int gid;
    //商品名称
    private String gname;
    //商品价格
    private double gprice;
    //商品数量
    private int gnum;

    public Goods(int gid,String gname,double gprice,int gnum) {
        this.gid = gid;
        this.gname = gname;
        this.gprice = gprice;
        this.gnum = gnum;
    }

    public Goods(String gname,int gnum) {
        this.gname = gname;
        this.gprice = gprice;
        this.gnum = gnum;
    }

    /*
     * 根据编号更改商品数量
     */
    public Goods(int gid,int gnum) {
        this.gid = gid;
        this.gnum = gnum;
    }

    /*
     * 根据编号更改商品价格
     */
    public Goods(int gid,double gprice) {
        this.gid = gid;
        this.gprice = gprice;
    }

    /*
     * 根据商品编号更改商品名称
     */
    public Goods(int gid,String gname) {
        this.gid = gid;
        this.gname = gname;
    }

    //getter,setter方法
    public int getGid() {
        return gid;
    }

    public void setGid(int gid) {
        this.gid = gid;
    }

    public String getGname() {
        return gname;
    }

    public void setGname(String gname) {
        this.gname = gname;
    }

    public double getGprice() {
        return gprice;
    }

    public void setGprice(double gprice) {
        this.gprice = gprice;
    }

    public int getGnum() {
        return gnum;
    }

    public void setGnum(int gnum) {
        this.gnum = gnum;
    }
}
View Code

?SalesMan.java

 1 package entity;
 2 
 3 public class SalesMan {
 4     //营业员编号
 5     private int sid;
 6     //营业员密码
 7     private String spassword;
 8     //营业员姓名
 9     private String sname;
10 
11     /*
12      * 根据sid设置密码
13      */
14     public SalesMan(int sid,String spassword) {
15         this.sid = sid;
16         this.spassword = spassword;
17     }
18 
19     /*
20      * 根据sid设置姓名
21      */
22     public SalesMan (int sid,String sname,String spassword) {
23         this.sid = sid;
24         this.sname = sname;
25         this.spassword = spassword;
26     }
27 
28     public SalesMan(String spassword,String sname) {
29         this.sid = sid;
30         this.spassword = spassword;
31         this.sname = sname;
32     }
33     
34     //getter,setter方法
35     public int getSid() {
36         return sid;
37     }
38 
39     public void setSid(int sid) {
40         this.sid = sid;
41     }
42 
43     public String getSpassword() {
44         return spassword;
45     }
46 
47     public void setSpassword(String spassword) {
48         this.spassword = spassword;
49     }
50 
51     public String getSname() {
52         return sname;
53     }
54 
55     public void setSname(String sname) {
56         this.sname = sname;
57     }
58 }
View Code

Gsales.java

 1 package entity;
 2 
 3 import java.util.Date;
 4 
 5 public class Gsales {
 6 
 7     //商品编号
 8     private int gid;
 9     //销售员编号
10     private int sid;
11     //销售数量
12     private int snum;
13     
14     
15     private String gname;
16     private double gprice;
17     private int gnum;
18     private int allSnum;    //单种商品销量总和
19     
20     public Gsales(int gid,int sid,int snum) {
21         this.gid = gid;
22         this.sid = sid;
23         this.snum = snum;
24     }
25     
26     public Gsales(String gname,int gnum,int allSnum) {
27         this.gname = gname;
28         this.gprice = gprice;
29         this.gnum = gnum;
30         this.allSnum = allSnum;
31     }
32     
33     //setter,getter方法
34     public int getGid() {
35         return gid;
36     }
37 
38     public void setGid(int gid) {
39         this.gid = gid;
40     }
41 
42     public int getSid() {
43         return sid;
44     }
45 
46     public void setSid(int sid) {
47         this.sid = sid;
48     }
49 
50     public int getSnum() {
51         return snum;
52     }
53 
54     public void setSnum(int snum) {
55         this.snum = snum;
56     }
57 
58     public String getGname() {
59         return gname;
60     }
61 
62     public void setGname(String gname) {
63         this.gname = gname;
64     }
65 
66     public double getGprice() {
67         return gprice;
68     }
69 
70     public void setGprice(double gprice) {
71         this.gprice = gprice;
72     }
73 
74     public int getGnum() {
75         return gnum;
76     }
77 
78     public void setGnum(int gnum) {
79         this.gnum = gnum;
80     }
81 
82     public int getAllSnum() {
83         return allSnum;
84     }
85 
86     public void setAllSnum(int allSnum) {
87         this.allSnum = allSnum;
88     }
89 }
View Code

?

2.编写主界面

主界面示意图:

主界面展示方法

 1     private static void mainPage() {
 2         System.out.println("*************************************n");
 3         System.out.println("ttt 1.商品维护");
 4         System.out.println("ttt 2.前台收银");
 5         System.out.println("ttt 3.商品管理");
 6         System.out.println("n*************************************n");
 7 
 8         System.out.println("n请选择,输入选项或者按0退出:");
 9         while (true) {
10             String choice = ScannerChoice.ScannerInfo();
11             String regex = "[0-3]";//正则表达式
12             if(choice.matches(regex)) {
13                 int info = Integer.parseInt(choice);
14                 switch (info) {
15                     case 0:
16                         System.out.println("----------------------------");
17                         System.out.println("您已退出系统!");
18                         System.exit(-1);//退出程序,返回值随便设置
19                         break;
20                     case 1:
21                         System.out.println("正在进入商品维护页面...");
22                         break;
23                     case 2:
24                         System.out.println("正在进入前台收银页面...");
25                         break;
26                     case 3:
27                         System.out.println("正在进入商品管理页面...");
28                         break;
29                     default:
30                         break;
31                 }
32             }
33             else {
34                 System.err.println("输入有误!");
35                 System.out.print("重新选择或按0退出.");
36             }
37         }
38     }

之后会将这个方法完善,把打印语句改成对应的方法,进入到下个界面。

接下来编写商品维护界面的方法。

 1 public static void maintaincePage() {
 2         System.out.println("商场购物管理系统>>商品维护");
 3         System.out.println("*************************************n");
 4         System.out.println("ttt 1.添加商品n");
 5         System.out.println("ttt 2.更改商品n");
 6         System.out.println("ttt 3.删除商品n");
 7         System.out.println("ttt 4.显示所有商品n");
 8         System.out.println("ttt 5.查询商品n");
 9         System.out.println("*************************************n");
10         System.out.println("请选择,输入数字或者按0返回上一级菜单");
11         while(true) {
12             String choice = ScannerChoice.ScannerInfo();
13             String regex = "[0-5]";//正则表达式
14             if (choice.matches(regex)) {
15                 int info = Integer.parseInt(choice);
16                 switch (info) {
17                     case 0:
18                         mainPage();
19                         break;
20                     case 1:
21                         System.out.println("开始添加商品...");
22                         break;
23                     case 2:
24                         System.out.println("开始更改商品...");
25                         break;
26                     case 3:
27                         System.out.println("开始删除商品...");
28                         break;
29                     case 4:
30                         System.out.println("开始显示商品...");
31                         break;
32                     case 5:
33                         System.out.println("开始查询商品...");
34                         break;
35                     default:
36                         break;
37                 }
38             } else {
39                 System.err.println("输入有误!");
40                 System.out.print("重新选择或按0回到上一级菜单.");
41             }
42         }
43 
44     }

写maintaincePage()方法跟写mainPage()方法的思路类似,switch()中的方法暂时使用打印语句代替,接下来逐渐完善这些具体的方法,会涉及到数据库的增删查改。

?为了降低程序的耦合性,将与商品增删查改有关的操作在GoodsPage类中实现。

 1 package page;
 2 
 3 import dao.GoodsDao;
 4 import entity.Goods;
 5 import tools.ScannerChoice;
 6 
 7 /**
 8  * 操作商品界面
 9  */
10 public class GoodsPage {
11     /**
12      * 添加商品界面
13      */
14     public static void addGoodsPage() {
15         System.out.println("t正在执行添加商品操作n");
16         System.out.println("n请输入要添加商品的名称");
17         String goodsName = ScannerChoice.ScannerInfo();
18 
19         System.out.println("n请输入要添加商品的价格");
20         double goodsPrice = Double.parseDouble(ScannerChoice.ScannerInfo());
21 
22         System.out.println("n请输入要添加的商品的数量");
23         int goodsNumber = Integer.parseInt(ScannerChoice.ScannerInfo());
24 
25         Goods goods = new Goods(goodsName,goodsPrice,goodsNumber);
26         boolean flag = new GoodsDao().addGoods(goods);
27 
28         if(flag) {
29             System.out.println("nt您已经成功添加商品到数据库!");
30         } else {
31             System.out.println("添加失败!");
32         }
33     }
34 }

GoodsDao类是涉及商品类数据库操作的类。

 1 package dao;
 2 
 3 import entity.Goods;
 4 import tools.DBtool;
 5 
 6 import java.sql.Connection;
 7 import java.sql.PreparedStatement;
 8 import java.sql.SQLException;
 9 
10 public class GoodsDao {
11     public static boolean addGoods(Goods goods) {
12         Connection conn = DBtool.getConn();
13         String sql = "insert into GOODS(GNAME,GPRICE,GNUM,GID) values(?,?,?)";
14         PreparedStatement ptsmt = null;
15         try {
16             ptsmt = conn.prepareStatement(sql);
17             ptsmt.setString(1,goods.getGname());
18             ptsmt.setString(2,goods.getGprice()+"");
19             ptsmt.setString(3,goods.getGnum()+"");
20             ptsmt.setString(4,goods.getGid());
21 
22             int rs = ptsmt.executeUpdate();
23             if(rs > 0) {
24                 return true;
25             } else
26                 return false;
27 
28         } catch (SQLException e) {
29             e.printStackTrace();
30         } finally {
31             DBtool.close(conn,ptsmt);
32         }
33         return true;
34     }
35 }

DBtool类是用于获取jdbc连接和释放连接的工具类。

 1 package tools;
 2 
 3 import java.sql.*;
 4 
 5 //用于连接数据库的工具类
 6 public class DBtool {
 7     private static String driver = "com.mysql.jdbc.Driver";
 8     private static String url = "jdbc:mysql://localhost:3306/shopdb";
 9     private static String user = "root";
10     private static String password = "root";
11     private static Connection conn = null;
12 
13     //获取连接
14     public static Connection getConn() {
15         try{
16             Class.forName(driver);
17             conn = DriverManager.getConnection(url,user,password);
18         } catch (ClassNotFoundException e) {
19             e.printStackTrace();
20         } catch (SQLException e) {
21             e.printStackTrace();
22         }
23         return conn;
24     }
25 
26     //关闭连接,释放资源
27     public static void close(Connection conn,PreparedStatement pstm) {
28         try {
29             pstm.close();
30         } catch (SQLException e) {
31             e.printStackTrace();
32         } finally {
33             try {
34                 conn.close();
35             } catch (SQLException e) {
36                 e.printStackTrace();
37             }
38         }
39     }
40 }

到这里为止,添加商品的功能已经基本实现。

测试如下:

可以看到GOODS表中有了我们刚才添加的数据。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读