用户商店管理
本身是有目的的做成這個功能,做了一天做完了之后發現代碼寫得太多了,也許是我是剛剛學習吧(小白一個),有機會我會看看自己的代碼,該封裝成方法的封裝成方法,盡可能的簡化一些吧。
主要的功能:
有對象方法兩個:用戶和商店
用戶中需要注冊和登陸,同時也有管理員和普通用戶的區分(這里因為怕麻煩,就將管理員和普通用戶的分配寫成了隨機,畢竟只是個假數據,當然,也正因為是假數據,所以用戶的注冊是支持同時插入多個的,不過我并沒有使用批處理的操作。-。-),只有登陸成功后才能進入商店頁面。(登入與否實際上只是對數據庫的查詢,在類方法中有一個布爾類型的變量,依靠它的true和false來判斷是否在登陸)
商店中登陸后也會有管理員和普通用戶的區分,普通用戶會被限制一定的功能,管理員則沒有任何限制,商店的功能會有添加商品,顯示所有商品,刪除商品,查詢商品等。
首先是對象方法:
user 用戶的方法
store 商店的方法
import java.util.Random;public class Commodity {private int id;private String name;private String price;private String typea;private String typeb;private String info;{name=random("name","","",5);price=random("price","","",3);typea=random("typea","","",1); // typeb=random("typeb",typea,"",1); // info=random("",typea,typeb,1);}public Commodity(){this(random("name","","",5),random("price","","",3),random("typea","","",1),"typeb","info",0);}public Commodity(String name, String price, String typea, String typeb, String info,int i) {this.name = name;this.price = price;this.typea = typea;this.typeb = typeb;this.info = info;if (i==0){this.typeb=random("typeb",this.typea,"",1);this.info=random("",this.typea,this.typeb,1);}}public static String random(String string ,String stri,String strj, int number) {Random random = new Random();String Str = "";if ("name".equals(string)) {String str = "abcdefghizklmnopqrstuvwxyz0123456789";String[] s = str.split("");for (int i = 0; i < number; i++) {int n = random.nextInt(str.length());Str = Str + s[n];}return Str;}else if ("price".equals(string)) {String str = "1,2,3,4,5,6,7,8,9";String[] s = str.split(",");for (int i = 0; i < number; i++) {int n = random.nextInt(s.length);Str = Str + s[n];}return Str;}else if ("typeb".equals(string) && stri.equals("服裝")) {String str = "tkz,南極人,花花公子,語克,帝坤,恒源祥,雅鹿,亂步,波司登,森馬,英爵倫,太平鳥";String[] s = str.split(",");for (int i = 0; i < number; i++) {int n = random.nextInt(s.length);Str = Str + s[n];}return Str;}else if ("typeb".equals(string) && stri.equals("電腦")) {String str = "Intel,聯想,Apple/蘋果,華碩,金河田,寧美國度,HP,Dell,alienware,IPASON/攀升,ENVINDA/恩維達,華為,AMD,pccooler/超頻三,honor/榮耀,Xiaomi/小米";String[] s = str.split(",");for (int i = 0; i < number; i++) {int n = random.nextInt(s.length);Str = Str + s[n];}return Str;}else if ("typeb".equals(string) && stri.equals("美食")) {String str = "其妙,徽味和,千絲,口口妙,LAO XIAN SHENG FOOD/老先生食品,知味觀,BE&CHEERY/百草味,歐貝拉,稻香村,沈大成,Three Squirrels/三只松鼠,零趣,友臣,良品鋪子,襄遇二陽";String[] s = str.split(",");for (int i = 0; i < number; i++) {int n = random.nextInt(s.length);Str = Str + s[n];}return Str;}else if ("typeb".equals(string) && stri.equals("犬種")) {String str = "泰迪,貴賓,比熊,吉娃娃,柯基,薩摩耶,雪納瑞,約克夏,西施犬,巴哥犬,金毛,拉布拉多,邊境牧羊犬,阿拉斯加,哈士奇,德國牧羊犬";String[] s = str.split(",");for (int i = 0; i < number; i++) {int n = random.nextInt(s.length);Str = Str + s[n];}return Str;}else if ("typeb".equals(string) && stri.equals("手機")) {String str = "華為,honor/榮耀,Xiaomi/小米,vivo,Apple/蘋果,OPPO,VOVG,三星,VJVJ,realme,魅族,SANNAING,紐曼,DOOV/朵唯,nubia/努比亞,OnePlus/一加,諾基亞";String[] s = str.split(",");for (int i = 0; i < number; i++) {int n = random.nextInt(s.length);Str = Str + s[n];}return Str;}else if ("typea".equals(string)) {String str = "電腦,服裝,美食,犬種,手機";String[] s = str.split(",");for (int i = 0; i < number; i++) {int n = random.nextInt(s.length);Str = Str + s[n];}return Str;}else if (stri.equals("電腦")){String str = "配置好,性能強,許多人的選擇,游戲必備,適合各種游戲大作";String[] s = str.split(",");for (int i = 0; i < number; i++) {int n = random.nextInt(s.length);Str = Str + s[n];}return Str;}else if (stri.equals("服裝")){String str = "端莊大氣,修身,許多人的選擇,20歲左右首選,賣的衣服可繞地球一圈";String[] s = str.split(",");for (int i = 0; i < number; i++) {int n = random.nextInt(s.length);Str = Str + s[n];}return Str;}else if (stri.equals("美食")){String str = "美味,素食,不長胖,發胖首選,超級辣";String[] s = str.split(",");for (int i = 0; i < number; i++) {int n = random.nextInt(s.length);Str = Str + s[n];}return Str;}else if (stri.equals("犬種")){String str = "乖巧,有趣,許多人的選擇,你人生的伴侶之一,可進行作死操作";String[] s = str.split(",");for (int i = 0; i < number; i++) {int n = random.nextInt(s.length);Str = Str + s[n];}return Str;}else if (stri.equals("手機")){String str = "配置好,性能強,許多人的選擇,游戲必備,適合各種游戲大作";String[] s = str.split(",");for (int i = 0; i < number; i++) {int n = random.nextInt(s.length);Str = Str + s[n];}return Str;}return "";}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPrice() {return price;}public void setPrice(String price) {this.price = price;}public String getTypea() {return typea;}public void setTypea(String typea) {this.typea = typea;}public String getTypeb() {return typeb;}public void setTypeb(String typeb) {this.typeb = typeb;}public String getInfo() {return info;}public void setInfo(String info) {this.info = info;}@Overridepublic String toString() {return "commodity{" +"name='" + name + '\'' +", price='" + price + '\'' +", typea='" + typea + '\'' +", typeb='" + typeb + '\'' +", info='" + info + '\'' +'}';} }操作界面
import java.util.ArrayList; import java.util.Scanner;public class StoreManager {private static ArrayList<User> users=new ArrayList<>();private static ArrayList<Commodity> commodities=new ArrayList<>();private static boolean boo=false;private static String gamer;public static void FuntionOne(){System.out.println("功能菜單如下(請輸入相對應的序號):");System.out.println("1.用戶注冊");System.out.println("2.用戶登陸");System.out.println("3.商品管理");System.out.println("4.重新創建表 注:謹慎使用,重新創建的表不會保存任何內容");System.out.println("5.退出");System.out.println("6.用戶登出");Scanner scanner=new Scanner(System.in);switch (scanner.nextInt()){case 1:QuestionOne();break;case 2:QuestionTwo();break;case 3:if (boo){FuntionTwo();}else {System.out.println("請登陸用戶(若無帳號可在功能菜單下選擇用戶注冊,進行注冊用戶)");System.out.println("輸入任意值繼續");scanner.next();FuntionOne();}break;case 4:Premise();break;case 5:return;case 6:if (boo){boo=false;System.out.println("已退出用戶……");FuntionOne();}else {System.out.println("并沒有用戶在登陸");FuntionOne();}break;default:System.out.println("輸入有誤,請按照正確的序號輸入");FuntionOne();break;}}private static void FuntionTwo(){System.out.println("************"+gamer+"************");System.out.println("商品菜單管理如下(請輸入相對應的序號):");System.out.println("1.添加商品");System.out.println("2.顯示所有商品");System.out.println("3.刪除商品");System.out.println("4.查詢商品");System.out.println("5.退出到功能菜單");System.out.println("6.退出");Scanner scanner=new Scanner(System.in);switch (scanner.nextInt()){case 1:if (gamer.equals("管理員")){One();}else {System.out.println("用戶權限不足");FuntionTwo();}break;case 2:Two();break;case 3:if (gamer.equals("管理員")){Three();}else {System.out.println("用戶權限不足");FuntionTwo();}break;case 4:Four();break;case 5:FuntionOne();break;case 6:return;default:System.out.println("輸入有誤,請重新輸入");FuntionTwo();break;}}private static void Premise(){System.out.println("需要創建新的表么");System.out.println("輸入 *0或用戶* 建立用戶表,輸入 *1或商品* 建立商品表,輸入 *2* 則同時建立兩表");Scanner scanner=new Scanner(System.in);String string=scanner.next();if ("0".equals(string) || "用戶".equals(string)) {MyJDBC.executeUpdate("drop table if exists User",null);MyJDBC.executeUpdate("CREATE TABLE User (`id` int NOT NULL AUTO_INCREMENT ,`name` varchar(50) NULL ,`password` varchar(50) NULL ,`level` varchar(20) NULL ,PRIMARY KEY (`id`))", null);System.out.println("創建結束!!");System.out.println("******************");users=new ArrayList<>();}else if ("1".equals(string) || "商品".equals(string)){MyJDBC.executeUpdate("drop table if exists Commodity",null);MyJDBC.executeUpdate("CREATE TABLE Commodity (`id` int NOT NULL AUTO_INCREMENT ,`name` varchar(50) NULL ,`price` varchar(50) NULL ,`typea` varchar(50) NULL ,`typeb` varchar(50) NULL ,`info` varchar(100) NULL ,PRIMARY KEY (`id`))",null);System.out.println("創建結束!!");System.out.println("******************");users=new ArrayList<>();}else if ("2".equals(string)){MyJDBC.executeUpdate("drop table if exists User",null);MyJDBC.executeUpdate("CREATE TABLE User (`id` int NOT NULL AUTO_INCREMENT ,`name` varchar(50) NULL ,`password` varchar(50) NULL ,`level` varchar(20) NULL ,PRIMARY KEY (`id`))", null);MyJDBC.executeUpdate("drop table if exists Commodity",null);MyJDBC.executeUpdate("CREATE TABLE Commodity (`id` int NOT NULL AUTO_INCREMENT ,`name` varchar(50) NULL ,`price` varchar(50) NULL ,`typea` varchar(50) NULL ,`typeb` varchar(50) NULL ,`info` varchar(100) NULL ,PRIMARY KEY (`id`))",null);System.out.println("創建結束!!");System.out.println("******************");users=new ArrayList<>();}FuntionOne();}private static void QuestionTwo() {Scanner scanner=new Scanner(System.in);System.out.println("******歡迎來到用戶登陸頁面******");System.out.print("請輸入您的用戶名:");String name=scanner.next();System.out.print("請輸入您的密碼:");String password=scanner.next();System.out.println("正在登陸中……");int number=MyJDBC.executeSelect("select * from user where password= \""+password+"\" and name=\""+name+"\"");if (number<0){System.out.println("登陸成功!!");gamer=MyJDBC.level(name);boo=true;System.out.println("***************************");}else {System.out.println("登陸失敗,請輸入正確的密碼或用戶名");}FuntionOne();}private static void QuestionOne() {System.out.println("******歡迎來到用戶注冊頁面******");System.out.println("如果想要自行創建新用戶則輸入0,否則輸入任意值隨機生成新用戶");Scanner scanner=new Scanner(System.in);if (scanner.next().equals("0")){System.out.println("**建立新用戶**");System.out.println("*************");System.out.print("用戶名:");String name=scanner.next();System.out.print("密碼:");String password=scanner.next();User user=new User(name,password);users.add(user);}else{System.out.println("*請輸入需要產生用戶的數量*");int number=scanner.nextInt();System.out.println("產生的用戶數量:"+number);for (int i = 0; i < number; i++) {User user=new User();users.add(user);}for (int i = 0; i < users.size(); i++) {System.out.println(users.get(i));}}printUserSQL();users=new ArrayList<>();FuntionOne();}private static int printUserSQL(){String s1;int num=0;if (num>=0) {s1 = "insert into user (name,password,level) values (?,?,?)";for (int i = 0; i < users.size(); i++) {String name = users.get(i).getName();String password = users.get(i).getPassword();String level = users.get(i).getLevel();num = MyJDBC.executeUserUpdate(s1, name, password, level);if (num<0){System.out.println("用戶名或密碼已被注冊。。。。");}else {System.out.println("用戶注冊成功。。。。");}}}return num;}private static void One() {System.out.println("******歡迎來到添加商品頁面******");System.out.println("如果想要自行添加新商品則輸入0,否則輸入任意值隨機生成新商品");Scanner scanner=new Scanner(System.in);if (scanner.next().equals("0")){System.out.println("**添加新商品**");System.out.println("*************");System.out.print("商品名稱:");String name=scanner.next();System.out.print("商品價格:");String price=scanner.next();System.out.print("商品一級分類:");String typea=scanner.next();System.out.print("商品二級分類:");String typeb=scanner.next();System.out.print("商品說明:");String info=scanner.next();Commodity commodity=new Commodity(name,price,typea,typeb,info,1);commodities.add(commodity);}else{System.out.println("*請輸入需要產生新產品的數量*");int number=scanner.nextInt();System.out.println("產生的新產品的數量:"+number);for (int i = 0; i < number; i++) {Commodity commodity=new Commodity();commodities.add(commodity);}for (int i = 0; i < commodities.size(); i++) {System.out.println(commodities.get(i));}}printCommoditySQL();commodities=new ArrayList<>();FuntionTwo();}private static int printCommoditySQL(){String s1;int num=0;if (num>=0) {s1 = "insert into commodity (name,price,typea,typeb,info) values (?,?,?,?,?)";for (int i = 0; i < commodities.size(); i++) {String name=commodities.get(i).getName();String price=commodities.get(i).getPrice();String typea=commodities.get(i).getTypea();String typeb=commodities.get(i).getTypeb();String info=commodities.get(i).getInfo();num = MyJDBC.executeCommodityUpdate(s1, name, price, typea,typeb, info);if (num<0){System.out.println("該商品"+name+"已被注冊");}else {System.out.println("該商品成功已成功添加");}}}return num;}private static void Two() {System.out.println("******歡迎來到商品頁面()******");ArrayList<String> strings=MyJDBC.executeCommoditySelect(null,null);for (int i = 0; i < strings.size(); i++) {System.out.println("**************************");System.out.println("name:"+strings.get(i));System.out.println("price:"+strings.get(++i));System.out.println("typea:"+strings.get(++i));System.out.println("typeb:"+strings.get(++i));System.out.println("info:"+strings.get(++i));}FuntionTwo();}private static void Three() {System.out.println("******歡迎來到刪除商品頁面******");System.out.println("以下商品屬性,希望以以下哪條屬性進行刪除");System.out.println("1.商品名稱");System.out.println("2.商品價格");System.out.println("3.商品一級分類");System.out.println("4.商品二級分類");System.out.println("5.商品說明");System.out.println("6.退出");Scanner scanner=new Scanner(System.in);switch (scanner.nextInt()){case 1:System.out.println("請輸入要刪除的內容");deleteCommoditySQL(scanner.next(),"name");break;case 2:System.out.println("請輸入要刪除的內容");deleteCommoditySQL(scanner.next(),"price");break;case 3:System.out.println("請輸入要刪除的內容");deleteCommoditySQL(scanner.next(),"typea");break;case 4:System.out.println("請輸入要刪除的內容");deleteCommoditySQL(scanner.next(),"typeb");break;case 5:System.out.println("請輸入要刪除的內容");deleteCommoditySQL(scanner.next(),"info");break;case 6:FuntionTwo();break;default:System.out.println("輸入有誤");}FuntionTwo();}private static int deleteCommoditySQL(String str,String string){String s1;int num=0;if ("name".equals(string)){s1 = "delete from commodity where name=?";num = MyJDBC.executeCommodityUpdate(s1, str);}else if ("price".equals(string)){s1 = "delete from commodity where price=?";num = MyJDBC.executeCommodityUpdate(s1, str);}else if ("typea".equals(string)){s1 = "delete from commodity where typea=?";num = MyJDBC.executeCommodityUpdate(s1, str);}else if ("typeb".equals(string)){s1 = "delete from commodity where typeb=?";num = MyJDBC.executeCommodityUpdate(s1, str);}else if ("info".equals(string)){s1 = "delete from commodity where info=?";num = MyJDBC.executeCommodityUpdate(s1, str);}if (num<= 0){System.out.println("刪除失敗");}else {System.out.println("刪除成功");}return num;}private static void Four() {System.out.println("******歡迎來到查詢商品頁面******");ArrayList<String> strings=new ArrayList<>();Scanner scanner=new Scanner(System.in);ArrayList<String> str1=new ArrayList<>();ArrayList<String> str2=new ArrayList<>();int num=0;do {System.out.println("請選擇所要查詢的屬性");str1.add(scanner.next());System.out.println("請輸入所要查詢的內容");str2.add(scanner.next());System.out.println("還需要其他的條件么(0不需要,1需要)");if ("0".equals(scanner.next())){num=5;}}while (num<5);strings=MyJDBC.executeCommoditySelect(str1,str2);for (int i = 0; i < strings.size(); i++) {System.out.println("**************************");System.out.println("name:"+strings.get(i));System.out.println("price:"+strings.get(++i));System.out.println("typea:"+strings.get(++i));System.out.println("typeb:"+strings.get(++i));System.out.println("info:"+strings.get(++i));}FuntionTwo();} }數據庫操作界面
package com.java1904.HomeWork;import java.sql.*; import java.util.ArrayList;public class MyJDBC {private static String dirver="com.mysql.cj.jdbc.Driver";private static String url ="jdbc:mysql://localhost:3306/java1904";private static String username="root";private static String password="123456";static {try {Class.forName(dirver);} catch (ClassNotFoundException e) {System.out.println("注冊驅動失敗 =>"+e.getMessage());}}public static Connection connection(){Connection connection=null;try {connection= DriverManager.getConnection(url,username,password);}catch (Exception e){System.out.println("連接錯誤 =>" + e.getMessage());}finally {return connection;}}public static int executeSelect(String str){Connection connection=null;PreparedStatement ps=null;ResultSet rs=null;int rest=0;try{connection=connection();ps=connection.prepareStatement(str);rs=ps.executeQuery();while (rs.next()){rest=-1;break;}}catch (Exception e){}return rest;}public static int executeUpdate(String str,Object...objects){Connection connection=null;PreparedStatement ps=null;ResultSet rs=null;int rest=0;try{connection=connection();ps=connection.prepareStatement(str);int i=1;if (connection!=null){if (objects!=null) {for (Object o : objects) {ps.setObject(i++, o);}}}rest=ps.executeUpdate();return rest;}catch (Exception e){System.out.println("出錯 =>"+e.getMessage());}finally {close(connection,ps,rs);}return -1;}public static int executeUserUpdate(String str,Object...objects){Connection connection=null;PreparedStatement ps=null;ResultSet rs=null;int rest=0;int number=0;number=executeSelect("select * from user where name=\""+objects[0]+"\"");if (number<0){return -1;}number=executeSelect("select * from user where password=\""+objects[1]+"\"");if (number<0){return -1;}try{connection=connection();ps=connection.prepareStatement(str);int i=1;if (connection!=null){if (objects!=null) {for (Object o : objects) {ps.setObject(i++, o);}}}rest=ps.executeUpdate();return rest;}catch (Exception e){System.out.println("出錯 =>"+e.getMessage());}finally {close(connection,ps,rs);}return -1;}public static int executeCommodityUpdate(String str,Object...objects){Connection connection=null;PreparedStatement ps=null;ResultSet rs=null;int rest=0;int number=0;number=executeSelect("select * from commodity where name="+objects[0]);if (number<0){return -1;}//s1, name, price, typea,typeb, infotry{connection=connection();ps=connection.prepareStatement(str);int i=1;if (connection!=null){if (objects!=null) {for (Object o : objects) {ps.setObject(i++, o);}}}rest=ps.executeUpdate();return rest;}catch (Exception e){System.out.println("出錯 =>"+e.getMessage());}finally {close(connection,ps,rs);}return -1;}public static ArrayList<String> executeCommoditySelect(ArrayList<String> str,ArrayList<String> objects){ArrayList<String> strings=new ArrayList<>();if (objects==null){ResultSet resultSet=null;Statement statement = null;Connection connection=connection();try {statement=connection.createStatement();resultSet = statement.executeQuery("select name, price, typea, typeb ,info from commodity");while (resultSet.next()){String Name=resultSet.getString("name");strings.add(Name);String Price=resultSet.getString("price");strings.add(Price);String typea=resultSet.getString("typea");strings.add(typea);String typeb=resultSet.getString("typeb");strings.add(typeb);String info=resultSet.getString("info");strings.add(info);}}catch (Exception e){System.out.println("全體查詢出錯=>"+e.getMessage());}}else{ResultSet resultSet=null;Statement statement = null;Connection connection=connection();try {statement=connection.createStatement();String string="select name, price, typea, typeb ,info from commodity where ";for (int i = 0; i < str.size(); i++) {if (!(str.get(i) ==null)){if (i>0){string +=" and ";}string +=str.get(i)+"= \""+objects.get(i)+"\"";}else {break;}}resultSet = statement.executeQuery(string);while (resultSet.next()){String Name=resultSet.getString("name");strings.add(Name);String Price=resultSet.getString("price");strings.add(Price);String typea=resultSet.getString("typea");strings.add(typea);String typeb=resultSet.getString("typeb");strings.add(typeb);String info=resultSet.getString("info");strings.add(info);}}catch (Exception e){System.out.println("全體查詢出錯=>"+e.getMessage());}}return strings;}public static String level(String username){Connection connection=connection();Statement statement=null;ResultSet resultSet=null;String level="";try{statement=connection.createStatement();String str="select level from user where name="+"\""+username+" \"";resultSet = statement.executeQuery(str);while (resultSet.next()){level=resultSet.getString("level");return level;}}catch (Exception e){close(connection,null,resultSet);}return level;}public static void close(Connection connection,PreparedStatement ps,ResultSet rs){try {if (connection!=null){connection.close();}else if (ps!=null){ps.close();}else if (rs!=null){ps.close();}}catch (Exception e){System.out.println("釋放資源出錯 =>"+e.getMessage());}finally {}} }總結
- 上一篇: HDU 6203 ping ping p
- 下一篇: Java 笔试:常见题目总结,html5