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: checkstandLogPage(); break; case 3: commodityManagementPage(); break; case 4: commodityManagementPage(); break; case 5: commodityManagementPage(); break; default: break; } } System.out.println("输入有误请重新输入:"); }while(true); } }
package per.sww.page; import per.sww.GoodsDao; import per.sww.entity.Goods; 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"); } }
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; } /** * 根据编号更改商品信息 * * @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; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import per.sww.db.DbClose; import per.sww.db.DbConn; import per.sww.entity.Goods; 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=false; } }catch(SQLException e){ e.printStackTrace(); }finally{ DbClose.addClose(ps, conn); } return bool; } }
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.tools; import java.util.Scanner; import per.sww.entity.Goods; import per.sww.page.GoodsPage; import sun.management.OperatingSystemImpl; 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; } // public static void changedInfoNext(String conn){ // do{ // System.out.println("是否继续进行-当前操作:(Y/N)"); // String chioe=ScannerChoice.ScannerInfoString(); // if("y".equals(chioe) || "Y".equals(chioe)){ // // } // }while(true); // } }
package per.sww.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import per.sww.db.DbClose; import per.sww.db.DbConn; import per.sww.entity.Goods; 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=false; } }catch(SQLException e){ e.printStackTrace(); }finally{ DbClose.addClose(ps, conn); } return bool; } }
然后给你们报错的样子
--------------------------- 1.商品维护 2.前台收银 3.商品管理 0.退出程序 --------------------------- 请输入选项: 请输入:1 --------------------------- 0.退出程序 1.添加商品 2.更改商品 3.删除商品 4.查询商品 5.显示所有商品 --------------------------- 请输入选项,或者按 0 返回上一级菜单. 请输入:1 正在执行添加商品操作 請輸入添加商品-名称 请输入:西瓜 請輸入添加商品-价格 小数点后两位 1.32 請輸入添加商品-数量 请输入: 3 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at per.sww.db.DbConn.getconn(DbConn.java:15) at per.sww.dao.GoodsDao.addGoods(GoodsDao.java:27) at per.sww.page.GoodsPage.addGoodsPage(GoodsPage.java:20) at per.sww.page.MainPage.MaintenancePage(MainPage.java:100) at per.sww.page.MainPage.mianPage(MainPage.java:37) at per.sww.page.MainPage.main(MainPage.java:7)
conn=DbConn.getconn();看你这个在哪里
好尴尬啦..没放架包- -,不要凶我
@眉间剪水泛千愁: ···