zoukankan      html  css  js  c++  java
  • 7.基础知识小应用

    根据到目前为止已经复习了的基础知识,在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;

    二、CODING

    一、主体结构如下:

    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("*************************************
    ");
     3         System.out.println("			 1.商品维护");
     4         System.out.println("			 2.前台收银");
     5         System.out.println("			 3.商品管理");
     6         System.out.println("
    *************************************
    ");
     7 
     8         System.out.println("
    请选择,输入选项或者按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     }

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

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

     1 public static void maintaincePage() {
     2         System.out.println("商场购物管理系统>>商品维护");
     3         System.out.println("*************************************
    ");
     4         System.out.println("			 1.添加商品
    ");
     5         System.out.println("			 2.更改商品
    ");
     6         System.out.println("			 3.删除商品
    ");
     7         System.out.println("			 4.显示所有商品
    ");
     8         System.out.println("			 5.查询商品
    ");
     9         System.out.println("*************************************
    ");
    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     }

    4.写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("	正在执行添加商品操作
    ");
    16         System.out.println("
    请输入要添加商品的名称");
    17         String goodsName = ScannerChoice.ScannerInfo();
    18 
    19         System.out.println("
    请输入要添加商品的价格");
    20         double goodsPrice = Double.parseDouble(ScannerChoice.ScannerInfo());
    21 
    22         System.out.println("
    请输入要添加的商品的数量");
    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("
    	您已经成功添加商品到数据库!");
    30         } else {
    31             System.out.println("添加失败!");
    32         }
    33     }
    34 }

    5.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 }

    6.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.然后把之前的代码中一些冗余的输出语句进行了删除。完成添加商品功能之后,我们要开始着手写维护商品功能,一样的,在GoodPage类中创建一个updateGoodsPage()方法,在maintaincePage()方法中调用它

     1  /**
     2      * 2.修改商品界面
     3      */
     4     public static void updateGoodsPage() {
     5         System.out.println("请输入想要更改的商品名字:");
     6         //开始查询
     7         String gid = QueryPrint.query("updateGoodsPage");
     8         while(true) {
     9             //交互
    10             System.out.println("
    ********请选择您要更改的内容********
    ");
    11             System.out.println("1.更改商品-名称");
    12             System.out.println("2.更改商品-价格");
    13             System.out.println("3.更改商品-数量");
    14             System.out.println("
    请输入选项,或者按0返回上一级菜单:");
    15             String choice = ScannerChoice.ScannerInfo();
    16             String regex = "[0-3]";
    17             if(choice.matches(regex)) {
    18                 switch(choice) {
    19                     case "0" ://返回上一级菜单,即商品维护页面
    20                         MainPage.maintaincePage();
    21                         break;
    22                     case "1" ://更改商品名称,调用数据库
    23                         System.out.println("您要将该商品名修改为什么:");
    24                         String name = ScannerChoice.ScannerInfo();
    25                         Goods goods = new Goods(gid,name);
    26                         boolean flag = GoodsDao.updateGoods(1,goods);
    27                         if (flag) {
    28                             System.out.println("成功更新商品名至数据库!");
    29                         }
    30                         else {
    31                             System.err.println("更新商品名失败!");
    32                         }
    33                         break;
    34                     case "2" ://更改商品价格,调用数据库
    35                         System.out.println("您要将该商品价格修改为多少:");
    36                         double price = ScannerChoice.ScannerPrice();
    37                         goods = new Goods(gid,price);
    38                         boolean flag2 = GoodsDao.updateGoods(2,goods);
    39                         if (flag2) {
    40                             System.out.println("成功更新商品价格至数据库!");
    41                         }
    42                         else {
    43                             System.err.println("更新商品价格失败!");
    44                         }
    45                         break;
    46                     case "3" ://更改商品数量,调用数据库
    47                         int num = ScannerChoice.ScannerNum();
    48                         goods = new Goods(gid,num);
    49                         boolean flag3 = GoodsDao.updateGoods(3,goods);
    50                         if (flag3) {
    51                             System.out.println("成功更新商品数量至数据库!");
    52                         }
    53                         else {
    54                             System.err.println("更新商品数量失败!");
    55                         }
    56                         break;
    57                     default:
    58                         break;
    59                 }
    60             }
    61             else {
    62                 System.out.println("您的输入有误!");
    63             }
    64         }
    65 
    66     }
    View Code

    为了实现这个方法,要使用GoodsDao类的updateGoods方法,这个是最主要的,它有两个参数,第一个整形参数决定了要更新该商品的什么内容,1,2,3分别代表名称,价格和数量。

     1     /**
     2      * 根据参数和商品实例修改商品信息
     3      * @param key 该参数决定修改商品的什么内容,1,2,3分别对应名称,价格和数量
     4      * @param goods 通过用户键盘输入的内容对商品实例进行修改,从而得到新的信息
     5      * @return
     6      */
     7     public static boolean updateGoods(int key,Goods goods) {
     8         boolean flag = false;
     9         conn = DBtool.getConn();
    10         switch (key) {
    11             case 1 ://key=1,修改商品名称
    12                 String sqlName = "UPDATE GOODS SET GNAME = ? WHERE GID = ? ";
    13                 try {
    14                     pstmt = conn.prepareStatement(sqlName);
    15                     pstmt.setString(1,goods.getGname());
    16                     pstmt.setString(2,goods.getGid());
    17                     int rs =  pstmt.executeUpdate();
    18                     if (rs > 0) {
    19                         flag = true;
    20                     }
    21                 } catch (SQLException e) {
    22                     e.printStackTrace();
    23                 } finally {
    24                     DBtool.close(conn,pstmt);
    25                 }
    26             case 2://key=2,修改商品价格
    27                 String sqlPrice = "UPDATE GOODS SET GPRICE = ? WHERE GID = ?";
    28                 try {
    29                     pstmt = conn.prepareStatement(sqlPrice);
    30                     pstmt.setDouble(1,goods.getGprice());
    31                     pstmt.setString(2,goods.getGid());
    32                     int rs =  pstmt.executeUpdate();
    33                     if (rs > 0) {
    34                         flag = true;
    35                     }
    36                 } catch (SQLException e) {
    37                     e.printStackTrace();
    38                 } finally {
    39                     DBtool.close(conn,pstmt);
    40                 }
    41             case 3://key=3,修改商品数量
    42                 String sqlNum = "UPDATE GOODS SET GNUM = ? WHERE GID = ?";
    43                 try {
    44                     pstmt = conn.prepareStatement(sqlNum);
    45                     pstmt.setInt(1,goods.getGnum());
    46                     pstmt.setString(2,goods.getGid());
    47                     int rs =  pstmt.executeUpdate();
    48                     if (rs > 0) {
    49                         flag = true;
    50                     }
    51                 } catch (SQLException e) {
    52                     e.printStackTrace();
    53                 } finally {
    54                     DBtool.close(conn,pstmt);
    55                 }
    56         }
    57         return flag;
    58     }
    View Code

     另外还有两个辅助的工具类,一个用于接到键盘输入的商品名称后,进行查询并排版打印商品信息的QueryPrint类,用到了其中的query()方法(仍在考虑是否需要将该方法写到GoodsDao类中,功能重叠。。。)

    另一个是获取键盘输入以后进行判断选择的工具类,ScannerChoice类。用到了其中的

    ScannerInfo(),
    ScannerPrice(),
    ScannerNum()
    方法
     1  /**
     2      *
     3      * @return 获取的键盘输入
     4      */
     5     public static String ScannerInfo() {
     6         Scanner scanner = new Scanner(System.in);
     7         return scanner.next();
     8     }
     9 
    10     /**
    11      *  从键盘获取用户输入的价格
    12      * @return 获取到的价格,小数点后保留两位
    13      */
    14     public static double ScannerPrice() {
    15         double num = 0.00;
    16         while(true) {
    17             Scanner scanner = new Scanner(System.in);
    18             System.out.print("保留小数点后两位,请输入:");
    19             //通过正则表达式保留小数点后两位
    20             String regex = "([1-9][0-9]*\.([0-9]{2}))|[0]\.([0-9]{2})";
    21             String info = scanner.next();
    22             if (info.matches(regex)) {
    23                  num = Double.parseDouble(info);
    24             } else {
    25                 System.out.println("输入格式不正确");
    26                 continue;
    27             }
    28             break;
    29         }
    30         return num;
    31     }
    32 
    33     public static int ScannerNum() {
    34         int num = 0;
    35         String regex = "[1-9]|[1-9][0-9]+";//商品数量的正则表达式
    36         while (true) {
    37             System.out.println("请输入商品的新数量:");
    38             Scanner scanner = new Scanner(System.in);
    39             String info = scanner.next();
    40             if(info.matches(regex)) {
    41                 num = Integer.parseInt(info);
    42             } else {//输入格式有误
    43                 System.err.println("输入格式有误");
    44                 continue;
    45             }
    46             break;
    47         }
    48         return num;
    49     }
    View Code

    8.接下来考虑编写删除商品方法。在GoodsPage类中添加一个方法,deleteGoodsPage().

     1  /**
     2      * 3.删除商品界面
     3      */
     4     public static void deleteGoodsPage() {
     5         System.out.println("请输入想要删除的商品名字:");
     6         String gid = QueryPrint.query("deleteGoodsPage");
     7         while(true) {
     8             System.out.println("
    确认要删除该商品吗?(Y/N)");
     9             String regex = "[y]|[Y]|[n]|[N]";
    10             String info = ScannerChoice.ScannerInfo();
    11             if(info.matches(regex)) {
    12                 //判断用户输入的是Y还是N
    13                 if("n".equals(info) || "N".equals(info)) {
    14                     //返回上一层
    15                     MainPage.maintaincePage();
    16                 }
    17                 else { //输入Y,调用数据库的方法进行处理
    18                     boolean flag = GoodsDao.deleteGoods(gid);
    19                     if(flag) {
    20                         System.err.println("删除成功!");
    21                         //删除成功以后回到上一级界面
    22                         MainPage.maintaincePage();
    23                     }
    24                     else
    25                         System.err.println("删除失败!");
    26                 }
    27             }
    28             else {//输入有误
    29                 System.out.println("输入有误!请重新输入!");
    30             }
    31         }
    32     }
    View Code

    9.接下来继续编写显示所有商品的方法。在GoodsPage类中添加一个方法,showAllGoodsPage()。

    /**
         * 4.显示所有商品界面
         */
        public static void showAllGoodsPage() {
            GoodsDao.showAllGoods();
        }

    在这个方法里面调用了GoodsDao类的showAllGoods()方法,对数据库进行操作。

     1  /**
     2      *  查询所有商品信息
     3      * @return
     4      */
     5     public static void showAllGoods() {
     6         conn = DBtool.getConn();
     7         String sql = "SELECT * FROM GOODS";
     8         ArrayList<Goods> goodsList = new ArrayList<Goods>();
     9         try{
    10             pstmt = conn.prepareStatement(sql);
    11             rs = pstmt.executeQuery();
    12             rs.last();
    13             if(rs.getRow() > 0) {
    14                 System.err.println("查询成功!");
    15             } else {
    16                 System.err.println("库存为空!");
    17                 //回到上一级菜单
    18                 MainPage.maintaincePage();
    19             }
    20             rs.first();
    21             System.out.println("********************************************商品列表******************************************
    ");
    22             System.out.printf("%-13s %-13s %-13s %-13s %13s
    ","商品编号","商品名称","商品价格","商品数量","备注");
    23             //接下来遍历ResultSet,把商品信息存到List中
    24             while(rs.next()) {
    25                 String gname = rs.getString("GNAME");
    26                 String gid = rs.getString("GID");
    27                 double gprice = rs.getDouble("GPRICE");
    28                 int gnum = rs.getInt("GNUM");
    29                 Goods goods = new Goods(gid,gname,gprice,gnum);
    30                 //打印信息
    31                 System.out.printf("
    %-17s %-17s %-17s %-17s",goods.getGid(),goods.getGname(),goods.getGprice(),+goods.getGnum());
    32                 if(gnum < 10) {
    33                     System.out.printf("%-17s","该商品库存已不足十件!");
    34                 }
    35                 System.out.println();
    36             }
    37             while(true) {
    38                 System.out.println("
    按1回到上一级菜单,按0退出程序,请输入:");
    39                 String regex = "[0]|[1]";
    40                 String info = ScannerChoice.ScannerInfo();
    41                 if(info.matches(regex)) {
    42                     if("0".equals(info)) //退出程序
    43                         System.exit(-1);
    44                     if("1".equals(info)) //回到上一级菜单
    45                         MainPage.maintaincePage();
    46                 } else {
    47                     System.out.println("输入格式有误!");
    48                 }
    49             }
    50 
    51         } catch (SQLException e) {
    52             e.printStackTrace();
    53         } finally {
    54             DBtool.close(conn,pstmt,rs);
    55         }
    56     }
    View Code

    测试功能:

    虽然界面有点简陋,功能基本上应该是没有问题了。嗯。不知道为什么使用了printf对输出进行格式化之后,为什么还会有对不齐的事情发生,搞不懂,昨天试了很久都没有解决这个问题,暂时先不管了。现在进入下一步

    10.完成查询商品界面。

    在查询界面,我们要根据用户输入的查询条件进行相应的查询:

    1>按商品数量升序查询

    2>按商品价格升序查询

    3>输入关键字查询商品

    这个方法还是很有意思的。
      1  /**
      2      * 5.查询商品界面
      3      */
      4     public static void queryGoodsPage() {
      5         System.out.println("执行查询商品操作
    ");
      6         System.out.println("1.按商品数量升序查询");
      7         System.out.println("2.按商品价格升序查询");
      8         System.out.println("3.输入关键字查询商品");
      9         //用正则确定输入的格式
     10         String regex = "[0-3]";
     11         ArrayList<Goods> goodsList = null;
     12         while(true) {
     13             System.out.println("请选择,输入数字或按0返回上一级菜单:");
     14             String info = ScannerChoice.ScannerInfo();
     15             if(info.matches(regex)) {//输入符合规则
     16                 if("1".equals(info)) {//按商品数量升序
     17                     //获取查询到的商品集合
     18                     goodsList = GoodsDao.queryGoods();
     19                     //将集合进行排序
     20                     Collections.sort(goodsList, (o1,o2) -> { return o1.getGnum()-o2.getGnum();});
     21                     //打印商品信息
     22                     System.out.println("********************************************商品列表******************************************
    ");
     23                     System.out.printf("%-13s %-13s %-13s %-13s %13s
    ","商品编号","商品名称","商品价格","商品数量","备注");
     24                     for (Goods goods : goodsList) {
     25                         System.out.printf("
    %-17s %-17s %-17s %-17s",goods.getGid(),goods.getGname(),goods.getGprice(),+goods.getGnum());
     26                         if(goods.getGnum() == 0) {
     27                             System.out.printf("该商品已售空");
     28                         }
     29                         else if(goods.getGnum() < 10) {
     30                             System.out.printf("%-17s","该商品库存已不足十件!");
     31                         }
     32                         System.out.println();
     33                     }
     34                     System.out.println("
    查询结束,继续查询输入1,返回上一级菜单输入0,输入其他任意内容退出程序!");
     35                     String choice = ScannerChoice.ScannerInfo();
     36                     if("1".equals(choice)) {
     37                         queryGoodsPage();
     38                     }
     39                     else if ("0".equals(choice)) {
     40                         MainPage.maintaincePage();
     41                     }
     42                     else
     43                         System.exit(-1);
     44                 }
     45                 else if ("2".equals(info)) {//按商品价格升序
     46                     //获取查询到的商品集合
     47                     goodsList = GoodsDao.queryGoods();
     48                     //将集合进行排序
     49                     Collections.sort(goodsList, (o1,o2) -> {  return (int)(o1.getGprice()-o2.getGprice());});
     50                     //打印商品信息
     51                     System.out.println("********************************************商品列表******************************************
    ");
     52                     System.out.printf("%-13s %-13s %-13s %-13s %13s
    ","商品编号","商品名称","商品价格","商品数量","备注");
     53                     for (Goods goods : goodsList) {
     54                         System.out.printf("
    %-17s %-17s %-17s %-17s",goods.getGid(),goods.getGname(),goods.getGprice(),+goods.getGnum());
     55                         if(goods.getGnum() == 0) {
     56                             System.out.printf("该商品已售空");
     57                         }
     58                         else if(goods.getGnum() < 10) {
     59                             System.out.printf("%-17s","该商品库存已不足十件!");
     60                         }
     61                         System.out.println();
     62                     }
     63                     System.out.println("
    查询结束,继续查询输入1,返回上一级菜单输入0,输入其他任意内容退出程序!");
     64                     String choice = ScannerChoice.ScannerInfo();
     65                     if("1".equals(choice)) {
     66                         queryGoodsPage();
     67                     }
     68                     else if ("0".equals(choice)) {
     69                         MainPage.maintaincePage();
     70                     }
     71                     else
     72                         System.exit(-1);
     73                 }
     74                 else if ("3".equals(info)) {
     75                     System.out.println("请输入关键字:");
     76                     goodsList = GoodsDao.queryGoods();
     77                     String keyWord = ScannerChoice.ScannerInfo();
     78                     int num = 0;
     79                     System.out.println("********************************************商品列表******************************************
    ");
     80                     System.out.printf("%-13s %-13s %-13s %-13s %13s
    ","商品编号","商品名称","商品价格","商品数量","备注");
     81                     for(Goods goods : goodsList) {
     82                         if(goods.getGname().contains(keyWord)) {
     83                             num++;
     84                             System.out.printf("
    %-17s %-17s %-17s %-17s",goods.getGid(),goods.getGname(),goods.getGprice(),+goods.getGnum());
     85                             if(goods.getGnum() == 0) {
     86                                 System.out.printf("该商品已售空");
     87                             }
     88                             else if(goods.getGnum() < 10) {
     89                                 System.out.printf("%-17s","该商品库存已不足十件!");
     90                             }
     91                             System.out.println();
     92                         }
     93                     }
     94                     if(num == 0)
     95                         System.out.println("找不到您要的商品");
     96                     System.out.println("
    查询结束,继续查询输入1,返回上一级菜单输入0,输入其他任意内容退出程序!");
     97                     String choice = ScannerChoice.ScannerInfo();
     98                     if("1".equals(choice)) {
     99                         queryGoodsPage();
    100                     }
    101                     else if ("0".equals(choice)) {
    102                         MainPage.maintaincePage();
    103                     }
    104                     else
    105                         System.exit(-1);
    106                 }
    107             break;
    108             } else { //输入格式不正确
    109                 System.out.println("输入格式有误!");
    110             }
    111 
    112         }
    113     }
    View Code

    在GoodsPage类中新增一个queryGoodsPage()方法,然后这个方法调用GoodsDao类中的queryGoods()获取到全部商品的集合,再从集合中根据条件分析选取数据,

    queryGoods()

     1 /**
     2      * 查询所有商品信息
     3      * @return 返回查询到的商品集合
     4      */
     5     public static ArrayList<Goods> queryGoods() {
     6         ArrayList<Goods> goodsList = new ArrayList<Goods>();
     7         conn = DBtool.getConn();
     8         String sql = "SELECT * FROM GOODS";
     9         try{
    10             pstmt = conn.prepareStatement(sql);
    11             rs = pstmt.executeQuery();
    12             //接下来遍历ResultSet,把商品信息存到List中
    13             while(rs.next()) {
    14                 String gname = rs.getString("GNAME");
    15                 String gid = rs.getString("GID");
    16                 double gprice = rs.getDouble("GPRICE");
    17                 int gnum = rs.getInt("GNUM");
    18                 Goods goods = new Goods(gid,gname,gprice,gnum);
    19                 goodsList.add(goods);
    20             }
    21         } catch (SQLException e) {
    22             e.printStackTrace();
    23         } finally {
    24             DBtool.close(conn,pstmt,rs);
    25         }
    26         return goodsList;
    27     }
    View Code
  • 相关阅读:
    [C++]C++11右值引用
    [cocos2d-x]registerScriptHandler和registerScriptTapHandler区别
    [深度探索C++对象模型]关于成员初始化列表(member initiallization list)
    [深度探索C++对象模型]memcpy和memset注意事项
    sql server isnull函数
    sql server 数据类型
    sql server SQL 服务器
    案例:按钮拖动移动
    PHP 随笔记
    laravel5 事务回滚
  • 原文地址:https://www.cnblogs.com/blogforvi/p/9381855.html
Copyright © 2011-2022 走看看