给你们贴代码
package per.sww.page; import per.sww.tools.ScannerChoice; public class MainPage extends ScannerChoice{ public static void main(String[] args) { MainPage.mianPage(); } /** * No.1 * 实现登录界面 * 且实现退出功能 */ public static void mianPage(){ System.out.println("---------------------------\n"); System.out.println("\t 1.商品维护\n"); System.out.println("\t 2.前台收银\n"); System.out.println("\t 3.商品管理\n"); System.out.println("\t 0.退出程序\n"); System.out.println("---------------------------\n"); System.out.println("\n请输入选项:"); do{ //MainPage继承了ScannerChoice,在此处调用Scanner的ScannerInfoString方法 String choice = ScannerInfoString(); //创建一个正则验证输入 String regex = "[0-3]"; if(choice.matches(regex)){ int info = Integer.parseInt(choice); switch (info) { case 0: System.out.println("---------------------------\n"); System.out.println("您已退出!"); System.exit(1); break; case 1: MaintenancePage(); break; case 2: checkstandLogPage(); break; case 3: commodityManagementPage(); break; default: break; } } System.out.println("输入有误请重新输入:"); }while(true); } /** * No.4 * 主页面中[商品管理]选项类 */ private static void commodityManagementPage() { System.out.println("---------------------------\n"); System.out.println("\t 1.售货员管理\n"); System.out.println("\t 2.列出当日卖出列表\n"); System.out.println("---------------------------\n"); System.out.println("\n请输入选项,或者按 0 返回上一级菜单."); } /** * No.3 * 主页面中[前台收银]选项类 */ private static void checkstandLogPage() { System.out.println("---------------------------\n"); System.out.println("■■■■■■■■■■■■■■■■■欢迎购物■■■■■■■■■■■■■■■■■"); System.out.println("\t 1.登录系统\n"); System.out.println("\t 2.退出\n"); System.out.println("---------------------------\n"); } /** * No.2 * 主页面中[商品维护]选项类 */ public static void MaintenancePage(){ System.out.println("---------------------------\n"); System.out.println("\t 0.退出程序\n"); System.out.println("\t 1.添加商品\n"); System.out.println("\t 2.更改商品\n"); System.out.println("\t 3.删除商品\n"); System.out.println("\t 4.查询商品\n"); System.out.println("\t 5.显示所有商品\n"); System.out.println("---------------------------\n"); System.out.println("\n请输入选项,或者按 0 返回上一级菜单."); do{ String choice = ScannerInfoString(); String regex = "[0-3]"; if(choice.matches(regex)){ int info = Integer.parseInt(choice); switch (info) { case 0: mianPage(); System.exit(1); break; case 1: GoodsPage.addGoodsPage(); break; case 2: GoodsPage.upateGoodsPage(); break; case 3: GoodsPage.deleteGoodsPage(); break; case 4: GoodsPage.queryGoodsPage(); break; case 5: GoodsPage.displayGoodsPage(); break; default: break; } } System.out.println("输入有误请重新输入:"); }while(true); } }
package per.sww.page; import java.util.ArrayList; import per.sww.dao.GoodsDao; import per.sww.entity.Goods; import per.sww.tools.QueryPrint; import per.sww.tools.ScannerChoice; import sun.security.util.Length; public final class GoodsPage extends ScannerChoice { public static void addGoodsPage() { System.out.println("\t正在执行添加商品操作\n"); System.out.println("\n請輸入添加商品-名称"); String goodName = ScannerInfoString(); System.out.println("\n請輸入添加商品-价格"); double goodPrice = ScannerInfo(); System.out.println("\n請輸入添加商品-数量"); int goodNumber = ScannerNum(); Goods goods = new Goods(goodName, goodPrice, goodNumber); boolean bool = new GoodsDao().addGoods(goods); if (bool) { System.out.println("\n\t!您已成功添加商品到数据库!"); } else { System.out.println("添加商品失败"); } changedInfoNext("addGoodsPage"); } /** * 2.更改商品界面 */ public static void upateGoodsPage() { System.out.println("\t正在执行 更改商品 操作\n"); System.out.println("请输入想要更改的商品名字"); int gid = QueryPrint.Query("upateGoodsPage"); System.out.println("\n--------请选择您要更改的内容\n"); System.out.println("\t1.更改商品-名称"); System.out.println("\t2.更改商品-价格"); System.out.println("\t3.更改商品-数量"); System.out.println("\n请输入选项,或者按0返回上一级菜单."); do { String choice = ScannerInfoString(); String regex = "[0-3]"; if (choice.matches(regex)) { int info = Integer.parseInt(choice); switch (info) { case 0: MainPage.MaintenancePage(); break; case 1: System.out.println("请输入商品-新名称"); String gname = ScannerInfoString(); Goods goodname = new Goods(gid, gname); Boolean boolName = new GoodsDao().updateGoods(1, goodname); if (boolName) { System.out.println("成功"); } else { System.out.println("失败"); } changedInfoNext("upateGoodsPage"); break; case 2: System.out.println("请输入商品-新价格 "); double gPrice = ScannerInfo(); Goods goodsPrice = new Goods(gid, gPrice); boolean boolPrice = new GoodsDao().updateGoods(2, goodsPrice); if (boolPrice) { System.out.println("成功"); } else { System.out.println("失败"); } changedInfoNext("upateGoodsPage"); break; case 3: System.out.println("请输入商品-新数量 "); int num=ScannerNum(); Goods goodsnum=new Goods(gid, num); boolean boolnum=new GoodsDao().updateGoods(3, goodsnum); if(boolnum){ System.out.println("成功"); }else { System.out.println("失败"); } changedInfoNext("upateGoodsPage"); break; default: System.out.println("请输入正确选项:"); break; } } System.out.println("重新输入"); } while (true); } /** * 3.删除商品界面 */ public static void deleteGoodsPage() { System.out.println("\t正在执行 删除商品 操作\n"); System.out.println("请输入想要删除的商品名字"); int gid = QueryPrint.Query("deleteGoodsPage"); do { System.out.println("\n确认删除该商品:Y/N"); String choice = ScannerInfoString(); if ("y".equals(choice) || "Y".equals(choice)) { boolean bool = new GoodsDao().deleteGoods(gid); if (bool) { System.err.println("删了"); } else { System.out.println("你坑了"); } changedInfoNext("deleteGoodsPage"); } else if ("n".equals(choice) || "N".equals("choice")) { MainPage.MaintenancePage(); } System.out.println("输错了"); } while (true); } /** * 查询商品 */ public static void queryGoodsPage(){ System.out.println("\t\t 正在执行查询商品操作\n"); System.out.println("\t\t1.按照商品 数量升序 查询"); System.out.println("\t\t2.按照商品 价格升序 查询"); System.out.println("\t\t3.输入商品 关键字 查询"); System.out.println("\n请输入选项,或者按0返回上一级菜单."); do{ String info=ScannerInfoString(); String regex="[0-3]"; if(info.matches(regex)){ int choice=Integer.parseInt(info); switch (choice) { case 0: MainPage.MaintenancePage(); break; case 1: case 2: case 3: if(choice==3){ System.out.println("\t\t正在执行商品 关键字 查询操作\n"); System.out.println("\n请输入商品关键字"); } ArrayList<Goods> goodList=new GoodsDao().queryGoods(choice); if(goodList==null || goodList.size()<=0){ System.out.println("不存在"); queryGoodsPage(); }else { if(choice==1){ System.out.println("\t\t\t\t\t商品按照 数量升序 列表\n\n"); } else if (choice == 2) { System.out.println("\t\t\t\t\t商品按照 价格升序 列表\n\n"); } else { System.out.println("\t\t\t\t\t您所查找的商品如下\n\n"); } System.out.println("\t商品编号\t\t商品名称\t\t商品价格\t\t商品数量\t\t备注\n"); for (int i = 0 ,length =goodList.size(); i < length; i++) { Goods goods=goodList.get(i); System.out.print("\t" + goods.getGid() + "\t\t" + goods.getGname() + "\t\t" + goods.getGprice() + "\t\t" + goods.getGnum()); int gnum=goods.getGnum(); if(gnum==0){ System.out.println("\t\t该商品已售空!"); }else if(gnum < 10) { System.out.println("\t\t该商品已不足10件"); }else { System.out.println("\t\t-"); } System.out.println("\t"); } System.out.println("---------------------"); do { System.out.println("输入 0 返回上一级菜单"); String choiceNext=ScannerInfoString(); if("0".equals(choiceNext)){ MainPage.MaintenancePage(); } System.err.println("输入有误!"); } while (true); } break; default: break; } break; } System.err.println("输入有误!"); System.out.println("请重新选择,或者按0返回上一级菜单."); }while(true); System.out.println("\n\n输入 0 返回上一级菜单"); boolean boolNext=true; do { String choice=ScannerInfoString(); if("0".equals(choice)){ boolNext=false; queryGoodsPage(); } System.err.println("!输入有误!"); System.out.println("请输入 0 返回上一级菜单"); } while (boolNext); } /** * 查询所有商品 */ public static void displayGoodsPage(){ System.out.println("\t\t\t\t\t所有商品列表\n\n"); ArrayList<Goods> goodlist=new GoodsDao().displayGoods(); if(goodlist.size()<0){ System.err.println("!库存为空!"); MainPage.MaintenancePage(); }else{ System.out.println("\t商品编号\t\t商品名称\t\t商品价格\t\t商品数量\t\t备注\n"); for(int i=0,length=goodlist.size();i<length;i++){ Goods goods=goodlist.get(i); System.out.print("\t" + goods.getGid() + "\t\t" + goods.getGname() + "\t\t" + goods.getGprice() + " $\t\t" + goods.getGnum()); int gnum=goods.getGnum(); if(gnum==0){ System.out.println("\t\t该商品已售空!"); }else if (gnum<0) { System.out.println("\t\t该商品已不足10件"); }else if (gnum<10) { System.out.println("\t\t-"); }else { System.out.println("\t"); } System.out.println("\t"); System.out.println("---------------------"); do{ System.out.println("输入 0 返回上一级菜单"); String choice=ScannerInfoString(); if("0".equals(choice)){ MainPage.MaintenancePage(); } System.out.println("输入有误"); }while(true); } } } }
package per.sww.tools; import java.util.Scanner; import per.sww.page.GoodsPage; import per.sww.page.MainPage; public class ScannerChoice { /** * 获取键盘录入 */ public static String ScannerInfoString(){ Scanner scanner=new Scanner(System.in); System.out.print("请输入:"); return scanner.next(); } /** * 键盘录入价格 * @return */ public static double ScannerInfo(){ double num=0.00; do{ Scanner sc=new Scanner(System.in); System.out.println("小数点后两位"); String info=sc.next(); //验证小数的正则 String regex="(([1-9][0-9]*)\\.([0-9]{2}))|[0]\\.([0-9]{2})"; if(info.matches(regex)){ num=Double.parseDouble(info); }else{ System.out.println("输入有误!"); continue; } break; }while(true); return num; } public static int ScannerNum(){ int num=0; String regex="([1-9])|([1-9][0-9]+)"; do{ Scanner sc=new Scanner(System.in); System.out.println("请输入:"); String nums=sc.next(); if(nums.matches(regex)){ num=Integer.parseInt(nums); }else{ System.out.println("输入有误请重新输入:"); continue; } break; }while(true); return num; } /** * 获取用户-更改完商品-下一步 获取用户-删除完商品-下一步 获取用户-添加完商品-下一步 * * @param 调用者 */ public static void changedInfoNext(String oper){ do{ System.out.println("是否继续进行-当前操作:(Y/N)"); String chioe=ScannerChoice.ScannerInfoString(); if("y".equals(chioe) || "Y".equals(chioe)){ if("upateGoodsPage".equals(oper)){ GoodsPage.upateGoodsPage(); }else if ("deleteGoodsPage".equals(oper)) { GoodsPage.deleteGoodsPage(); }else if ("addGoodsPage".equals(oper)) { GoodsPage.addGoodsPage(); } }else if ("N".equals(oper)) { MainPage.MaintenancePage(); } System.out.println("\n输入有误!请重新输入."); }while(true); } }
package per.sww.entity; public final class Goods { private int gid; private String gname; private double gprice; private int gnum; /** * 添加商品信息 * * @param gname * ,gprice,gum */ public Goods(String gname,double gprice,int gnum){ this.gname=gname; this.gprice=gprice; this.gnum=gnum; } /** * 展示所有商品 * * @param gid * ,gname,gprice,gum */ public Goods(int gid,String gname,double gprice,int gnum){ this.gid=gid; this.gname=gname; this.gprice=gprice; this.gnum=gnum; } /** * 根据编号更改商品信息 * * @param gid * ,gum */ public Goods(int gid, String gname){ this.gid=gid; this.gname=gname; } /** * 根据编号更改商品价格 */ public Goods(int gid,double gprice){ this.gid=gid; this.gprice=gprice; } /** * 根据编号更改商品信息 * * @param gid * ,gprice */ public Goods(int gid,int gnum){ this.gid=gid; this.gnum=gnum; } 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; } }
package per.sww.entity; public final class SalesMan { private int sId; private String sName; private String sPassword; public SalesMan(int sId,String sName){ this.sId=sId; this.sName=sName; } public SalesMan(int sId,String sName,String sPassword){ this.sId=sId; this.sName=sName; this.sPassword=sName; } public SalesMan(String sName,String sPassword){ this.sName=sName; this.sPassword=sPassword; } //封装 public int getsId() { return sId; } public void setsId(int sId) { this.sId = sId; } public String getsName() { return sName; } public void setsName(String sName) { this.sName = sName; } public String getsPassword() { return sPassword; } public void setsPassword(String sPassword) { this.sPassword = sPassword; } }
package per.sww.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import per.sww.db.DbClose; import per.sww.db.DbConn; import per.sww.entity.Goods; import per.sww.tools.ScannerChoice; public final class GoodsDao { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; /** * 1.添加商品到数据库goods表 * * @param goods * 商品对象 * @return boolean */ public boolean addGoods(Goods goods) { boolean bool = false; conn = DbConn.getconn(); String sql = "INSERT INTO GOODS(GNAME,GPRICE,GNUM) VALUES(?,?,?)"; try { ps = conn.prepareStatement(sql); ps.setString(1, goods.getGname()); ps.setDouble(2, goods.getGprice()); ps.setInt(3, goods.getGnum()); int rs = ps.executeUpdate(); if (rs > 0) { bool = true; } } catch (SQLException e) { e.printStackTrace(); } finally { DbClose.addClose(ps, conn); } return bool; } public ArrayList<Goods> queryGoods(int key){ ArrayList<Goods> goodlist=new ArrayList<Goods>(); conn=DbConn.getconn(); switch (key) { case 1: String sqlGnum = "SELECT * FROM GOODS ORDER BY GNUM ASC";//排序查询,升序 try{ ps=conn.prepareStatement(sqlGnum); rs=ps.executeQuery(); while(rs.next()){ int gid=rs.getInt("gid"); String gname=rs.getString(2); double gprice=rs.getDouble(3); int gnum=rs.getInt(4); Goods goods=new Goods(gid, gname, gprice, gnum); goodlist.add(goods); } }catch (SQLException e) { e.printStackTrace(); }finally { DbClose.queryClose(ps, rs, conn); } break; //降序 case 2: String sqlGprice = "SELECT * FROM GOODS ORDER BY GPRICE ASC"; try { ps=conn.prepareStatement(sqlGprice); rs=ps.executeQuery(); while(rs.next()){ int gid=rs.getInt("gid"); String gname=rs.getString(2); double gprice=rs.getDouble(3); int gnum=rs.getInt(4); Goods goods=new Goods(gid, gname, gprice, gnum); goodlist.add(goods); } } catch (SQLException e) { e.printStackTrace(); }finally { DbClose.queryClose(ps, rs, conn); } break; case 3: String nameGet=ScannerChoice.ScannerInfoString(); String sqlGname="SELECT * FROM GOODS WHERE GNAME LIKE '%'||?||'%'"; try { ps=conn.prepareStatement(sqlGname); ps.setString(1, nameGet); rs=ps.executeQuery(); while(rs.next()){ int gid=rs.getInt("gid"); String gname=rs.getString(2); double gprice=rs.getDouble(3); int gnum=rs.getInt(4); Goods goods=new Goods(gid, gname, gprice, gnum); goodlist.add(goods); } } catch (SQLException e) { e.printStackTrace(); }finally { DbClose.queryClose(ps, rs, conn); } break; case 4: break; default: break; } return goodlist; } /** * 3.从数据库goods表中-刪除商品 * * @param gid * 商品编号 * @return boolean */ public boolean deleteGoods(int gid) { boolean bool = false; conn = DbConn.getconn(); String sql = "DELETE FROM GOODS WHERE GID=?"; try { ps = conn.prepareStatement(sql); ps.setInt(1, gid); int rs = ps.executeUpdate(); if (rs > 0) { bool = true; } } catch (SQLException e) { e.printStackTrace(); } finally { DbClose.addClose(ps, conn); } return bool; } /** * 更改商品信息到数据库goods表 * @param key * @param goods * @return */ public boolean updateGoods(int key,Goods goods){ boolean bool=false; conn=DbConn.getconn(); switch (key) { case 1: String sqlNAME="UPDATE GOODS SET GNAME=? WHERE GID=?"; try { ps=conn.prepareStatement(sqlNAME); ps.setString(1,goods.getGname()); ps.setInt(2, goods.getGid()); int rs=ps.executeUpdate(); if(rs>0){ bool=true; } } catch (SQLException e) { e.printStackTrace(); }finally { DbClose.addClose(ps, conn); } break; case 2: String sqlgprice="UPDATE GOODS SET GPRICE=? WHERE GID=?"; try{ ps=conn.prepareStatement(sqlgprice); ps.setDouble(1, goods.getGprice()); ps.setInt(2, goods.getGid()); int rs=ps.executeUpdate(); if(rs>0){ bool=true; } }catch (SQLException e) { e.printStackTrace(); }finally { DbClose.addClose(ps, conn); } break; case 3: String sqlgnum="UPDATE GOODS SET GNUM=? WHERE GID=?"; try { ps=conn.prepareStatement(sqlgnum); ps.setInt(1, goods.getGnum()); ps.setInt(2, goods.getGid()); int rs=ps.executeUpdate(); if(rs>0){ bool=true; } } catch (SQLException e) { e.printStackTrace(); }finally { DbClose.addClose(ps, conn); } default: break; } return bool; } public ArrayList<Goods> displayGoods(){ ArrayList<Goods> goodList=new ArrayList<Goods>(); conn=DbConn.getconn(); String sql="SELECT * FROM GOODS"; try{ ps=conn.prepareStatement(sql); rs=ps.executeQuery(); while (rs.next()) { int gid=rs.getInt(1); String gname=rs.getString(2); double gprice=rs.getDouble(3); int gnum=rs.getInt(4); Goods goods=new Goods(gid, gname, gprice, gnum); goodList.add(goods); } }catch(SQLException e){ e.printStackTrace(); }finally { DbClose.queryClose(ps, rs, conn); } return goodList; } }
package per.sww.db; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public final class DbClose { public static void addClose(PreparedStatement ps, Connection conn) { try { if (ps != null) { ps.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } public static void queryClose(PreparedStatement ps,ResultSet rs, Connection conn){ try{ if (ps!=null) { ps.close(); } }catch(SQLException e){ e.printStackTrace(); } try { if(rs!=null){ rs.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if(conn!=null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } }
package per.sww.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DbConn { public static Connection getconn(){ Connection conn=null; String user="root"; String password="root"; String url="jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8"; try{ Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection(url, user, password); }catch(SQLException e){ e.printStackTrace(); }catch (ClassNotFoundException e) { e.printStackTrace(); } return conn; } }
package per.sww.tools; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import per.sww.db.DbClose; import per.sww.db.DbConn; import per.sww.entity.Goods; public final class QueryPrint { Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; public static int Query(String oper){ int gid=-1; String shopping=ScannerChoice.ScannerInfoString(); ArrayList<Goods> goodlist=new QueryPrint().queryGoodsKey(-1, shopping); if (goodlist==null || goodlist.size()<=0) { System.out.println("\t!!无此商品 !!"); ScannerChoice.changedInfoNext(oper); }else { Goods goods=goodlist.get(0); System.out.println("\t\t\t\t\t商品列表\n\n"); System.out.println("\t商品编号\t\t商品名称\t\t商品价格\t\t商品数量\t\t备注\n"); System.out.print("\t" + goods.getGid() + "\t\t" + goods.getGname() + "\t\t" + goods.getGprice() + "\t\t" + goods.getGnum()); if (goods.getGnum()==0) { System.out.println("\t\t该商品已售空"); }else if(goods.getGnum()<10){ System.out.println("\t\t该商品已不足10件"); }else { System.out.println("\t\t-"); } gid=goods.getGid(); } return gid; } public ArrayList<Goods> queryGoodsKey(int gId,String gName){ ArrayList<Goods> goodlist=new ArrayList<Goods>(); conn=DbConn.getconn(); String sql="SELECT * FROM GOODS WHERE GID=? OR GNAME=?"; try{ ps=conn.prepareStatement(sql); ps.setInt(1, gId); ps.setString(2, gName); rs=ps.executeQuery(); while (rs.next()) { int gid=rs.getInt("gid"); String gname=rs.getString(2); double gprice=rs.getDouble(3); int gnum=rs.getInt(4); Goods goods=new Goods(gid, gname, gprice, gnum); goodlist.add(goods); } }catch(SQLException e){ e.printStackTrace(); }finally { DbClose.addClose(ps, conn); } return goodlist; } }
代码没找到错误,但是运行的时候它就是不进去,
PS:运行以后是这样子的
---------------------------
1.商品维护
2.前台收银
3.商品管理
0.退出程序
---------------------------
请输入选项:
请输入:1
---------------------------
0.退出程序
1.添加商品
2.更改商品
3.删除商品
4.查询商品
5.显示所有商品
---------------------------
请输入选项,或者按 0 返回上一级菜单.
请输入:5
输入有误请重新输入:
请输入:
正常的代码按了4或者5 是会进入的,但是我的不进去也不报错!就一直在这死,什么情况?
package per.sww.page; import java.util.ArrayList; import per.sww.dao.GoodsDao; import per.sww.entity.Goods; import per.sww.tools.QueryPrint; import per.sww.tools.ScannerChoice; public final class GoodsPage extends ScannerChoice { public static void addGoodsPage() { System.out.println("\t正在执行添加商品操作\n"); System.out.println("\n請輸入添加商品-名称"); String goodName = ScannerInfoString(); System.out.println("\n請輸入添加商品-价格"); double goodPrice = ScannerInfo(); System.out.println("\n請輸入添加商品-数量"); int goodNumber = ScannerNum(); Goods goods = new Goods(goodName, goodPrice, goodNumber); boolean bool = new GoodsDao().addGoods(goods); if (bool) { System.out.println("\n\t!您已成功添加商品到数据库!"); } else { System.out.println("添加商品失败"); } changedInfoNext("addGoodsPage"); } /** * 2.更改商品界面 */ public static void upateGoodsPage() { System.out.println("\t正在执行 更改商品 操作\n"); System.out.println("请输入想要更改的商品名字"); int gid = QueryPrint.Query("upateGoodsPage"); System.out.println("\n--------请选择您要更改的内容\n"); System.out.println("\t1.更改商品-名称"); System.out.println("\t2.更改商品-价格"); System.out.println("\t3.更改商品-数量"); System.out.println("\n请输入选项,或者按0返回上一级菜单."); do { String choice = ScannerInfoString(); String regex = "[0-3]"; if (choice.matches(regex)) { int info = Integer.parseInt(choice); switch (info) { case 0: MainPage.MaintenancePage(); break; case 1: System.out.println("请输入商品-新名称"); String gname = ScannerInfoString(); Goods goodname = new Goods(gid, gname); Boolean boolName = new GoodsDao().updateGoods(1, goodname); if (boolName) { System.out.println("成功"); } else { System.out.println("失败"); } changedInfoNext("upateGoodsPage"); break; case 2: System.out.println("请输入商品-新价格 "); double gPrice = ScannerInfo(); Goods goodsPrice = new Goods(gid, gPrice); boolean boolPrice = new GoodsDao().updateGoods(2, goodsPrice); if (boolPrice) { System.out.println("成功"); } else { System.out.println("失败"); } changedInfoNext("upateGoodsPage"); break; case 3: System.out.println("请输入商品-新数量 "); int num=ScannerNum(); Goods goodsnum=new Goods(gid, num); boolean boolnum=new GoodsDao().updateGoods(3, goodsnum); if(boolnum){ System.out.println("成功"); }else { System.out.println("失败"); } changedInfoNext("upateGoodsPage"); break; default: System.out.println("请输入正确选项:"); break; } } System.out.println("重新输入"); } while (true); } /** * 3.删除商品界面 */ public static void deleteGoodsPage() { System.out.println("\t正在执行 删除商品 操作\n"); System.out.println("请输入想要删除的商品名字"); int gid = QueryPrint.Query("deleteGoodsPage"); do { System.out.println("\n确认删除该商品:Y/N"); String choice = ScannerInfoString(); if ("y".equals(choice) || "Y".equals(choice)) { boolean bool = new GoodsDao().deleteGoods(gid); if (bool) { System.err.println("删了"); } else { System.out.println("你坑了"); } changedInfoNext("deleteGoodsPage"); } else if ("n".equals(choice) || "N".equals("choice")) { MainPage.MaintenancePage(); } System.out.println("输错了"); } while (true); } /** * 查询商品 */ public static void queryGoodsPage(){ System.out.println("\t\t 正在执行查询商品操作\n"); System.out.println("\t\t1.按照商品 数量升序 查询"); System.out.println("\t\t2.按照商品 价格升序 查询"); System.out.println("\t\t3.输入商品 关键字 查询"); System.out.println("\n请输入选项,或者按0返回上一级菜单."); do{ //键盘录入记录 String info=ScannerInfoString(); //正则限定输入的只能为0-3 String regex="[0-3]"; //如果输入的符合正则的条件 if(info.matches(regex)){ //把输入的强制转换为int类型 int choice=Integer.parseInt(info); //把转换后的choice放入switch switch (choice) { case 0: MainPage.MaintenancePage(); break; case 1: case 2: case 3: if(choice==3){ System.out.println("\t\t正在执行商品 关键字 查询操作\n"); System.out.println("\n请输入商品关键字"); } // ArrayList<Goods> goodList=new GoodsDao().queryGoods(choice); //如果数组为空或者数组内的值为空 if(goodList==null || goodList.size()<=0){ //输出提示 System.out.println("不存在"); // queryGoodsPage(); }else { if(choice==1){ //执行 System.out.println("\t\t\t\t\t商品按照 数量升序 列表\n\n"); } else if (choice == 2) { System.out.println("\t\t\t\t\t商品按照 价格升序 列表\n\n"); } else { System.out.println("\t\t\t\t\t您所查找的商品如下\n\n"); } System.out.println("\t商品编号\t\t商品名称\t\t商品价格\t\t商品数量\t\t备注\n"); for (int i = 0 ,length =goodList.size(); i < length; i++) { Goods goods=goodList.get(i); System.out.print("\t" + goods.getGid() + "\t\t" + goods.getGname() + "\t\t" + goods.getGprice() + "\t\t" + goods.getGnum()); int gnum=goods.getGnum(); if(gnum==0){ System.out.println("\t\t该商品已售空!"); }else if(gnum < 10) { System.out.println("\t\t该商品已不足10件"); }else { System.out.println("\t\t-"); } System.out.println("\t"); } System.out.println("---------------------"); do { System.out.println("输入 0 返回上一级菜单"); String choiceNext=ScannerInfoString(); if("0".equals(choiceNext)){ MainPage.MaintenancePage(); } System.err.println("输入有误!"); } while (true); } break; default: break; } break; } System.err.println("输入有误!"); System.out.println("请重新选择,或者按0返回上一级菜单."); }while(true); System.out.println("\n\n输入 0 返回上一级菜单"); boolean boolNext=true; do { String choice=ScannerInfoString(); if("0".equals(choice)){ boolNext=false; queryGoodsPage(); } System.err.println("!输入有误!"); System.out.println("请输入 0 返回上一级菜单"); } while (boolNext); } /** * 查询所有商品 */ public static void displayGoodsPage(){ System.out.println("\t\t\t\t\t所有商品列表\n\n"); ArrayList<Goods> goodlist=new GoodsDao().displayGoods(); if(goodlist.size()<0){ System.err.println("!库存为空!"); MainPage.MaintenancePage(); }else{ System.out.println("\t商品编号\t\t商品名称\t\t商品价格\t\t商品数量\t\t备注\n"); for(int i=0,length=goodlist.size();i<length;i++){ Goods goods=goodlist.get(i); System.out.print("\t" + goods.getGid() + "\t\t" + goods.getGname() + "\t\t" + goods.getGprice() + " $\t\t" + goods.getGnum()); int gnum=goods.getGnum(); if(gnum==0){ System.out.println("\t\t该商品已售空!"); }else if (gnum<0) { System.out.println("\t\t该商品已不足10件"); }else if (gnum<10) { System.out.println("\t\t-"); }else { System.out.println("\t"); } System.out.println("\t"); System.out.println("---------------------"); do{ System.out.println("输入 0 返回上一级菜单"); String choice=ScannerInfoString(); if("0".equals(choice)){ MainPage.MaintenancePage(); } System.out.println("输入有误"); }while(true); } } } }
这是 5.显示所有商品
public ArrayList<Goods> displayGoods(){ ArrayList<Goods> goodList=new ArrayList<Goods>(); conn=DbConn.getconn(); String sql="SELECT * FROM GOODS"; try{ ps=conn.prepareStatement(sql); rs=ps.executeQuery(); while (rs.next()) { int gid=rs.getInt(1); String gname=rs.getString(2); double gprice=rs.getDouble(3); int gnum=rs.getInt(4); Goods goods=new Goods(gid, gname, gprice, gnum); goodList.add(goods); } }catch(SQLException e){ e.printStackTrace(); }finally { DbClose.queryClose(ps, rs, conn); } return goodList; }
现在按了5以后它只输出一行- -...丢死人了
MaintenancePage类:
String choice = ScannerInfoString();
String regex = "[0-3]";
把String regex = "[0-3]"; 改成String regex = "[0-5]";
(*@ο@*) 哇~,好腻害,么么哒
j截不了帖...
@眉间剪水泛千愁: 把displayGoodsPage()中的do..while循环放到if else判断之外。
你这个代码量太多,没细看,不过不报错也不进去估计是进入死进程了,应该是你的代码有个地方死循环。你自己在自己查看一下吧
我去。这么长也有人看~~
代码太多,不看~