1. 程式人生 > >7.基礎知識小應用

7.基礎知識小應用

分享 分享圖片 int tool void 正則 ice drive src

根據到目前為止已經復習了的基礎知識,在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, double gprice, 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,double gprice,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("\t\t\t 1.商品維護");
 4         System.out.println("\t\t\t 2.前臺收銀");
 5         System.out.println("\t\t\t 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("\t\t\t 1.添加商品\n");
 5         System.out.println("\t\t\t 2.更改商品\n");
 6         System.out.println("\t\t\t 3.刪除商品\n");
 7         System.out.println("\t\t\t 4.顯示所有商品\n");
 8         System.out.println("\t\t\t 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("\n\t您已經成功添加商品到數據庫!");
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表中有了我們剛才添加的數據。

技術分享圖片

7.基礎知識小應用