1 package per.sww.page; 2 3 import per.sww.tools.ScannerChoice; 4 5 public class MainPage extends ScannerChoice { 6 7 public static void main(String[] args) { 8 MainPage.mainPage(); 9 } 10 11 /** 12 * 首界面 13 */ 14 public final static void mainPage() { 15 System.out.println("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n"); 16 System.out.println("\t 1.商品维护\n"); 17 System.out.println("\t 2.前台收银\n"); 18 System.out.println("\t 3.商品管理\n"); 19 System.out.println("\t 0.退出程序\n"); 20 System.out.println("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n"); 21 System.out.println("\n请输入选项:"); 22 do { 23 // 记录键盘录入 24 String chorce = ScannerInfoString(); 25 // 正则限制输入为0-3 26 String regex = "[0-3]"; 27 // 验证输入是否符合正则 28 if (chorce.matches(regex)) { 29 int info = Integer.parseInt(chorce); 30 switch (info) { 31 case 0: 32 System.out.println("按0退出"); 33 // 自我结束 34 System.exit(1); 35 break; 36 case 1: 37 MaintenancePage(); 38 break; 39 case 2: 40 checkstandLogPage(); 41 break; 42 case 3: 43 commodityManagementPage(); 44 break; 45 default: 46 break; 47 } 48 49 } 50 51 } while (true); 52 } 53 54 private static void commodityManagementPage() { 55 System.out.println("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n"); 56 System.out.println("\t 1.售货员管理\n"); 57 System.out.println("\t 2.列出当日卖出列表\n"); 58 System.out.println("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n"); 59 System.out.println("\n请输入选项,或者按 0 返回上一级菜单."); 60 } 61 62 /** 63 * 主页面中[商品维护]选项类 64 */ 65 public static void MaintenancePage() { 66 System.out.println("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n"); 67 System.out.println("\t 0.退出程序\n"); 68 System.out.println("\t 1.添加商品\n"); 69 System.out.println("\t 2.更改商品\n"); 70 System.out.println("\t 3.删除商品\n"); 71 System.out.println("\t 4.查询商品\n"); 72 System.out.println("\t 5.显示所有商品\n"); 73 System.out.println("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n"); 74 System.out.println("\n请输入选项,或者按 0 返回上一级菜单."); 75 do { 76 String choice = ScannerInfoString(); 77 String regex = "[0-5]"; 78 if (choice.matches(regex)) { 79 int info = Integer.parseInt(choice); 80 switch (info) { 81 case 0: 82 System.out.println("按0退出"); 83 System.exit(1); 84 break; 85 case 1: 86 GoodsPage.addGoodsPage(); 87 break; 88 case 3: 89 //删除商品----- 90 GoodsPage.deleteGoodsPage(); 91 break; 92 default: 93 break; 94 } 95 } 96 System.out.println("有错需重输"); 97 } while (true); 98 } 99 100 /** 101 * 主页面中[前台收银]选项类 102 */ 103 public static void checkstandLogPage() { 104 System.out.println("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n"); 105 System.out.println("■■■■■■■■■■■■■■■■■欢迎购物■■■■■■■■■■■■■■■■■"); 106 System.out.println("\t 1.登录系统\n"); 107 System.out.println("\t 2.退出\n"); 108 System.out.println("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n"); 109 } 110 }
1 package per.sww.page; 2 3 4 import per.sww.dao.GoodsDao; 5 import per.sww.entity.Goods; 6 import per.sww.tools.QueryPrint; 7 import per.sww.tools.ScannerChoice; 8 9 public final class GoodsPage extends ScannerChoice{ 10 /** 11 * 添加商品界面 12 */ 13 public static void addGoodsPage(){ 14 System.out.println("\t正在执行添加商品操作\n"); 15 System.out.println("\n請輸入添加商品-名称"); 16 String goodName = ScannerInfoString(); 17 System.out.println("\n請輸入添加商品-价格"); 18 double goodPrice = ScannerInfo(); 19 System.out.println("\n請輸入添加商品-数量"); 20 int goodNumber = ScannerNum(); 21 22 Goods goods=new Goods(goodName, goodPrice, goodNumber); 23 boolean bool=new GoodsDao().addGoods(goods); 24 if(bool){ 25 System.out.println("\n\t!您已成功添加商品到数据库!"); 26 } else { 27 System.out.println("添加商品失败"); 28 } 29 changedInfoNext("addGoodsPage"); 30 } 31 32 33 /** 34 * 更改商品界面 35 */ 36 public static void upateGoodsPage(){ 37 } 38 39 /** 40 * 删除商品界面 41 */ 42 public static void deleteGoodsPage(){ 43 System.out.println("\t正在执行 删除商品 操作\n"); 44 System.out.println("请输入想要删除的商品名字"); 45 // 46 int gid=QueryPrint.query("deleteGoodsPage"); 47 do { 48 System.out.println("\n确认删除该商品:Y/N"); 49 String choice=ScannerInfoString(); 50 if("y".equals(choice) || "Y".equals(choice)){ 51 //接收获取到gid 52 boolean booldelete=new GoodsDao().deleteGoods(gid); 53 if(booldelete){ 54 System.out.println("删除成功!"); 55 }else { 56 System.out.println("删除失败!"); 57 } 58 changedInfoNext("deleteGoodsPage"); 59 }else if("n".equals(choice) || "N".equals(choice)){ 60 MainPage.MaintenancePage(); 61 } 62 System.out.println("\t!!输入有误,请重新输入!!\n"); 63 } while (true); 64 } 65 }
1 package per.sww.dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 9 import per.sww.db.DbClose; 10 import per.sww.db.DbConn; 11 import per.sww.entity.Goods; 12 13 public final class GoodsDao { 14 Connection conn=null; 15 PreparedStatement ps=null; 16 ResultSet rs=null; 17 18 /** 19 * 添加商品 20 * @param goods 21 * @return 22 */ 23 public boolean addGoods(Goods goods){ 24 boolean bool=false; 25 conn=DbConn.getconnection(); 26 String sql="INSERT INTO GOODS(GNAME,GPRICE,GNUM) VALUES(?,?,?)"; 27 28 try { 29 ps=conn.prepareStatement(sql); 30 ps.setString(1, goods.getGname()); 31 ps.setDouble(2, goods.getGprice()); 32 ps.setInt(3, goods.getGnum()); 33 int rs=ps.executeUpdate(); 34 if(rs>0){ 35 bool=true; 36 } 37 } catch (SQLException e) { 38 e.printStackTrace(); 39 }finally { 40 DbClose.addClose(ps, conn); 41 } 42 return bool; 43 } 44 45 /** 46 * 删除商品 47 */ 48 49 public boolean deleteGoods(int gid){ 50 boolean bool=false; 51 conn=DbConn.getconnection(); 52 String sql = "DELETE FROM GOODS WHERE GID=?"; 53 try{ 54 ps=conn.prepareStatement(sql); 55 ps.setInt(1, gid); 56 int rs=ps.executeUpdate(); 57 if(rs>0){ 58 bool=true; 59 } 60 }catch(SQLException e){ 61 e.printStackTrace(); 62 }finally { 63 DbClose.addClose(ps, conn); 64 } 65 return bool; 66 } 67 68 // public ArrayList<Goods> queryGoods(){ 69 // 70 // } 71 }
1 package per.sww.tools; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 9 import per.sww.db.DbClose; 10 import per.sww.db.DbConn; 11 import per.sww.entity.Goods; 12 13 public final class QueryPrint { 14 Connection conn=null; 15 PreparedStatement ps=null; 16 ResultSet rs=null; 17 18 19 // 20 public static int query(String oper){ 21 int gid=-1; 22 //录入商品 23 String shopping=ScannerChoice.ScannerInfoString(); 24 25 ArrayList<Goods> goodsList=new QueryPrint().queryGoodsKey(-1, shopping); 26 if(goodsList==null || goodsList.size()<=0){ 27 System.out.println("无此商品!"); 28 ScannerChoice.changedInfoNext(oper); 29 }else { 30 Goods goods=goodsList.get(0); 31 32 System.out.println("\t\t\t\t\t商品列表\n\n"); 33 System.out.println("\t商品编号\t\t商品名称\t\t商品价格\t\t商品数量\t\t备注\n"); 34 System.out.print("\t" + goods.getGid() + "\t\t" + goods.getGname() 35 + "\t\t" + goods.getGprice() + "\t\t" + goods.getGnum()); 36 37 if(goods.getGnum()==0){ 38 System.out.println("卖光了"); 39 }else if (goods.getGnum()<10) { 40 System.out.println("不到十瓶"); 41 }else { 42 System.out.println("\t\t-"); 43 } 44 gid=goods.getGid(); 45 } 46 return gid; 47 } 48 49 // 50 public ArrayList<Goods> queryGoodsKey(int gId,String gName){ 51 //创建一个arraylist数组里面放入商品信息 52 ArrayList<Goods> arrayList=new ArrayList<Goods>(); 53 //数据库打开 54 conn=DbConn.getconnection(); 55 String sql = "SELECT * FROM GOODS WHERE GID=? OR GNAME=?"; 56 try { 57 ps=conn.prepareStatement(sql); 58 ps.setInt(1, gId); 59 ps.setString(2, gName); 60 rs=ps.executeQuery(); 61 62 while (rs.next()) { 63 int gid=rs.getInt("gid"); 64 String gname=rs.getString(2); 65 double gprice=rs.getDouble(3); 66 int gnum=rs.getInt(4); 67 68 Goods goods=new Goods(gid, gname, gprice, gnum); 69 arrayList.add(goods); 70 } 71 } catch (SQLException e) { 72 e.printStackTrace(); 73 }finally { 74 DbClose.queryClose(ps, conn, rs); 75 } 76 return arrayList; 77 } 78 }
1 package per.sww.tools; 2 3 import java.util.Scanner; 4 5 import per.sww.entity.Goods; 6 import per.sww.page.GoodsPage; 7 import per.sww.page.MainPage; 8 9 public class ScannerChoice { 10 11 /** 12 * 键盘录入读取工具类 13 * @return 14 */ 15 public static String ScannerInfoString(){ 16 Scanner scanner=new Scanner(System.in); 17 System.out.println("请输入:"); 18 return scanner.next(); 19 } 20 /** 21 * 价格为double类型,必须有小数点后两位 22 * @return 23 */ 24 public static double ScannerInfo(){ 25 double num=0.00; 26 do{ 27 Scanner sc=new Scanner(System.in); 28 System.out.println("小数点后两位"); 29 String info=sc.next(); 30 String regex="(([1-9][0-9]*)\\.([0-9]{2}))|[0]\\.([0-9]{2})"; 31 if(info.matches(regex)){ 32 num=Double.parseDouble(info); 33 }else { 34 System.out.println("输入有误!"); 35 continue; 36 } 37 break; 38 }while(true); 39 return num; 40 } 41 42 43 //键盘获取商品 44 public static int ScannerNum(){ 45 int num=0; 46 String regex="([1-9])|([1-9][0-9]+)"; 47 do{ 48 Scanner sc=new Scanner(System.in); 49 System.out.println("请输入:"); 50 String nums=sc.nextLine(); 51 if(nums.matches(regex)){ 52 num=Integer.parseInt(nums); 53 }else { 54 System.out.println("输入有误!"); 55 continue; 56 } 57 break; 58 }while(true); 59 return num; 60 } 61 62 public static void changedInfoNext(String oper){ 63 do { 64 System.out.println("是否继续进行-当前操作:(Y/N)"); 65 String choice=ScannerChoice.ScannerInfoString(); 66 if("y".equals(choice) || "Y".equals(choice)){ 67 if("upateGoodsPage".equals(oper)){ 68 GoodsPage.upateGoodsPage(); 69 }else if ("deleteGoodsPage".equals(oper)) { 70 GoodsPage.deleteGoodsPage(); 71 }else if ("addGoodsPage".equals(oper)) { 72 GoodsPage.addGoodsPage(); 73 } 74 75 }else if ("N".equals(choice) || "n".equals(choice)) { 76 MainPage.MaintenancePage(); 77 } 78 System.out.println("输入有误"); 79 } while (true); 80 } 81 82 public static void choiceSalesManNext(){ 83 84 } 85 }
1 package per.sww.db; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 7 public class DbConn { 8 public static Connection getconnection(){ 9 Connection conn=null; 10 String user="root"; 11 String password="root"; 12 String url="jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8"; 13 14 try { 15 Class.forName("com.mysql.jdbc.Driver"); 16 conn=DriverManager.getConnection(url, user, password); 17 } catch (SQLException e) { 18 e.printStackTrace(); 19 }catch (ClassNotFoundException e) { 20 e.printStackTrace(); 21 } 22 return conn; 23 } 24 }
1 package per.sww.db; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 8 public final class DbClose { 9 public static void addClose(PreparedStatement ps,Connection conn){ 10 try { 11 if(ps!=null){ 12 ps.close(); 13 } 14 } catch (SQLException e) { 15 e.printStackTrace(); 16 } 17 try{ 18 if(conn!=null){ 19 conn.close(); 20 } 21 }catch(SQLException e){ 22 e.printStackTrace(); 23 } 24 25 } 26 27 28 public static void queryClose(PreparedStatement ps,Connection conn,ResultSet rs){ 29 try { 30 if(ps!=null){ 31 ps.close(); 32 } 33 } catch (SQLException e) { 34 e.printStackTrace(); 35 } 36 try { 37 if(conn!=null){ 38 conn.close(); 39 } 40 } catch (SQLException e) { 41 e.printStackTrace(); 42 } 43 try { 44 if(rs!=null){ 45 rs.close(); 46 } 47 } catch (SQLException e) { 48 e.printStackTrace(); 49 } 50 } 51 }
嗯,它现在的问题是删除的时候提示不存在,但是数据库中是有的,我在发愁怎么把数据库传上来
2园豆到手~
啊啊啊啊,你怎么能这么坏!
@眉间剪水泛千愁: 。。。你调试一下,看看gid是什么,不会调试,就在获得gid后输出到控制台看一下
@博客园乄小光: - -你不知道我笨么
@眉间剪水泛千愁: 。。。
int gid=QueryPrint.query("deleteGoodsPage");
后面加上:
systom.out.println(gid+"");
把结果给我
@博客园乄小光: 没看到结果在哪
@眉间剪水泛千愁: gid=5
@眉间剪水泛千愁: 删除以后gid=5的商品还存在么
@博客园乄小光: 有时候在有时候删除不掉
@眉间剪水泛千愁: 这个异常应该是你读你输入的商品名的问题,一般读取都是在输入之后,你是在调用的另一个方法中再读取的,可能会有问题,你把读取商品名放在你输入商品名之后
@我是一道光: 我头乱..理不清
你的删除商品这里deleteGoodsPage,商品名还未输入,gid就已经取出来了,这里没有逻辑错误吗?
木有..运行跑的正常,偶尔会乱输出