From 9f6c2729caf754b518bd338418e3ae5ad515254f Mon Sep 17 00:00:00 2001 From: JRNitre Date: Tue, 18 Jun 2024 22:43:58 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=99=E4=BA=9B=E4=BB=A3=E7=A0=81=E5=8F=98?= =?UTF-8?q?=E6=9B=B4=E4=B8=BB=E8=A6=81=E5=8C=85=E6=8B=AC=E5=AF=B9Java?= =?UTF-8?q?=E7=B1=BB=E3=80=81=E6=8E=A5=E5=8F=A3=E4=BB=A5=E5=8F=8A=E8=A7=86?= =?UTF-8?q?=E5=9B=BE=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E7=9A=84=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E3=80=82=E4=B8=8B=E9=9D=A2=E6=98=AF=E5=85=B7=E4=BD=93?= =?UTF-8?q?=E7=9A=84=E5=8F=98=E6=9B=B4=E7=82=B9=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Admin.java文件变更: 1. 添加了注释部分,指明设计者和日期。 2. 对成员变量添加了注释说明。 3. 增加了构造函数:无参构造方法、拷贝构造方法,并对成员变量进行了初始化。4. 增加了getter和setter方法。 ### AdminDao.java文件变更: 1. 添加了注释部分,指明设计者和日期。 2. 删除了原有的`updateAdminPassword`方法声明。 3. 新增了`getAdminByIdByPass`方法声明,接受管理员ID和密码作为参数,用于根据ID和密码获取管理员信息。 4. 新增了`boolean updateAdminPassword`方法声明,用于更新管理员密码,并返回操作结果。 ### AdminDaoImpl.java文件变更: 1. 添加了注释部分,指明设计者和日期。 2. 删除了原有的`updateAdminPassword`实现。 3. 实现了新的`getAdminByIdByPass`方法,使用管理员ID和密码从数据库检索管理员信息。 --- .../CourseExperiment/.idea/encodings.xml | 1 + .../CourseExperiment/src/ElmAdminEntry.java | 61 +- .../src/ElmBusinessEntry.java | 153 +++-- .../CourseExperiment/src/View/AdminView.java | 490 +++++++++++--- .../src/View/BusinessView.java | 607 +++++++++++++++++- .../CourseExperiment/src/View/Display.java | 183 ++++-- .../CourseExperiment/src/dao/AdminDao.java | 11 +- .../src/dao/AdminDaoImpl.java | 129 +++- .../CourseExperiment/src/dao/BusinessDao.java | 24 +- .../src/dao/BusinessDaoImpl.java | 416 +++++++++++- .../CourseExperiment/src/jdbc/JDBC.java | 18 +- .../CourseExperiment/src/po/Admin.java | 27 +- .../CourseExperiment/src/po/Business.java | 38 +- .../CourseExperiment/src/po/Food.java | 49 ++ 14 files changed, 1900 insertions(+), 307 deletions(-) create mode 100644 02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/po/Food.java diff --git a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/.idea/encodings.xml b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/.idea/encodings.xml index 5bbab86..0d9ee09 100644 --- a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/.idea/encodings.xml +++ b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/.idea/encodings.xml @@ -4,6 +4,7 @@ + diff --git a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/ElmAdminEntry.java b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/ElmAdminEntry.java index 825dca0..3461777 100644 --- a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/ElmAdminEntry.java +++ b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/ElmAdminEntry.java @@ -1,12 +1,11 @@ +/* + * Date: 2024-06-18 + * Design By JRNitre + * */ import View.AdminView; import View.Display; import po.Admin; -import po.Business; - -import java.io.IOException; -import java.util.Scanner; -import java.util.stream.StreamSupport; public class ElmAdminEntry { public static void main(String[] args){ @@ -16,25 +15,20 @@ public class ElmAdminEntry { private static final int exitCode = 10; public void work(){ - - Scanner sca = new Scanner(System.in); Display dis = new Display('+', '-', 30); AdminView adv = new AdminView(); - // ʵԱ, õ¼ʼ -// Admin admin = new AdminView().login(); +// Admin admin = new Admin(); +// admin.setAdminName("jrnitre"); +// admin.setAdminId(1); +// admin.setAdminPassword("1234"); - Admin admin = new Admin(); - admin.setAdminName("jrnitre"); - admin.setAdminId(1); - admin.setAdminPassword("1234"); + // ʵԱ, õ¼ʼ + Admin admin = new AdminView().login(); if (admin != null){ - - int key = -1; - + int key; while (true){ - dis.titleLine("Admin - " + admin.getAdminName(), true); dis.showItem("[0] ̼б", true); dis.showItem("[1] ̼", true); @@ -42,7 +36,8 @@ public class ElmAdminEntry { dis.showItem("[3] ɾ̼", true); dis.showItem("[4] 鿴ԱϢ", true); dis.showItem("[5] ùԱ", true); - dis.showItem("[6] 鿴Աб(ع)", true); + dis.showItem("[6] 鿴Աб (DEBUG)", true); + dis.showItem("[7] 鿴ʳƷб (DEBUG)", true); dis.showItem("[" + exitCode + "] ˳ϵͳ", true); dis.showLine(true); @@ -55,38 +50,35 @@ public class ElmAdminEntry { case 0: adv.printAllBusinessInfo(); break; + // ̼ case 1: adv.searchBusiness(); break; + // ½̼ case 2: adv.adminAddBusiness(); break; + // ɾ̼ case 3: adv.deleteBusiness(); break; + // ʾԱϢ case 4: - dis.titleLine("ԱϢ - " + admin.getAdminName(), true); - System.out.println("+ Ա ID: " + admin.getAdminId()); - System.out.println("+ Ա: " + admin.getAdminName()); - System.out.println("+ Ա: " + admin.getAdminPassword()); + adv.printAdminInfo(admin); break; + // ùԱ case 5: - dis.titleLine("ùԱ", true); - System.out.print("+ 뵱ǰ: "); - String newPassword = sca.next(); - if (!(newPassword.equals(admin.getAdminPassword()))){ - dis.titleLine("ERROR", true); - System.out.println("벻һ!"); - break; - } - System.out.println("+ : "); - newPassword = sca.next(); - admin.setAdminPassword(newPassword); - new AdminView().updatePassword(admin); + adv.updatePassword(admin); break; + // 鿴Աб case 6: adv.printAllAdminInfo(); break; + // 鿴ʳƷб + case 7: + adv.printAdminAllFoodInfo(); + break; + // Exit system case exitCode: if (dis.MessageYesOrNo("ʾ", "Ƿȷ˳ϵͳ?")) { dis.titleLine("ʾ", true); @@ -94,6 +86,7 @@ public class ElmAdminEntry { System.exit(0); } break; + // :-) default: dis.titleLine("ERROR", true); System.out.println("+ Ϸ!"); diff --git a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/ElmBusinessEntry.java b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/ElmBusinessEntry.java index 8cc306d..986f6fa 100644 --- a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/ElmBusinessEntry.java +++ b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/ElmBusinessEntry.java @@ -1,73 +1,130 @@ +/* + * Date: 2024-06-18 + * Design By JRNitre + * */ + import View.Display; import View.BusinessView; import po.Business; -import java.util.Scanner; - public class ElmBusinessEntry { public static void main (String[] args){ new ElmBusinessEntry().work(); } + private static final int exitCode = 10; + public void work(){ Display dis = new Display('+', '-', 30); - Scanner sca = new Scanner(System.in); + BusinessView buv = new BusinessView(); - int mistake = 0; - int maxMistake = 3; - int errorCode = 0; - int mainExitCode = 0; + // Login ¼ϵͳ + Business business = new BusinessView().login(); - while (mistake < maxMistake && mainExitCode == 0){ - dis.titleLine("¼ ô ̼ ϵͳ", true); - if (mistake != 0){ - System.out.println("+ ǰ: " + mistake + " ʣ: " + (maxMistake - mistake)); - } - System.out.print("+ ˻: "); - String businessName = sca.next(); - System.out.print("+ : "); - String businessPassword = sca.next(); +// Business business = new Business(); +// business.setBusinessPassword("1234"); +// business.setBusinessName("business"); +// business.setBusinessId(1); +// business.setBusinessAddress("null"); +// business.setBusinessExplain("null"); +// business.setBusinessStarPrice(5); +// business.setBusinessDeliveryPrice(1.5); - Business business = new BusinessView().login(businessName, businessPassword); + if (business != null){ + int key; + while (true){ + dis.titleLine("Business - " + dis.deleteSpaceToString(business.getBusinessName()), true); + dis.showItem("[0] 鿴̼Ϣ", true); + dis.showItem("[1] ޸̼Ϣ", true); + dis.showItem("[2] ޸̼", true); + dis.showItem("[3] ʳƷϢ", true); + dis.showItem("[" + exitCode + "] ˳ϵͳ", true); + dis.showLine(true); - if (business != null){ - int key = -1; - while (key != 10){ - dis.titleLine("Business - " + business.getBusinessName(), true); + key = dis.getKeyNumber(); - System.out.print("+ ӦIJ: "); - key = sca.nextInt(); - - switch (key){ - case 10: - errorCode = 2; - mainExitCode = 1; - break; - default: - dis.titleLine("ERROR", true); - System.out.println("+ Ϸ!"); - break; - } + switch (key){ + // 鿴̼Ϣ + case 0: + buv.printBusinessInfo(business); + break; + // ޸̼Ϣ + case 1: + buv.updateBusinessInfo(business); + break; + // ޸̼ + case 2: + buv.updatePassword(business); + break; + // ʳƷϢ + case 3: + food_work(business); + break; + case exitCode: + if (dis.MessageYesOrNo("ʾ", "Ƿȷ˳ϵͳ?")) { + dis.titleLine("ʾ", true); + System.out.println("+ ϵͳ˳"); + System.exit(0); + } + break; + default: + dis.titleLine("ERROR", true); + System.out.println("+ Ϸ!"); + break; } - } else { - mistake++; - } - if (mistake >= maxMistake){ - errorCode = 1; - } - } - if (errorCode == 1){ + } + } else { dis.titleLine("ERROR", true); - System.out.println("+ ࣬˳ϵͳ!"); - System.exit(0); - } - if (errorCode == 2){ - dis.titleLine("ʾ", true); - System.out.println("+ ϵͳ˳"); + System.out.println("+ ! ˳ϵͳ"); System.exit(0); } } + + public void food_work(Business business){ + Display dis = new Display('+', '-', 30); + BusinessView buv = new BusinessView(); + + int key; + while (true){ + dis.titleLine("̼ʳƷ", true); + dis.showItem("[0] 鿴ʳƷб", true); + dis.showItem("[1] ʳƷ", true); + dis.showItem("[2] ޸ʳƷϢ", true); + dis.showItem("[3] ɾʳƷ", true); + dis.showItem("[" + exitCode + "] ϼ˵", true); + + key = dis.getKeyNumber(); + + switch ( key ){ + // 鿴ʳƷб + case 0: + buv.printAllBusinessFoodInfo(business); + break; + // ʳƷ + case 1: + buv.addBusinessNewFood(business); + break; + // ޸ʳƷϢ + case 2: + buv.updateBusinessFoodInfo(business); + break; + // ɾʳƷ + case 3: + buv.deleteFood(business); + break; + case exitCode: + if (dis.MessageYesOrNo("ʾ", "ȷϼ˵?")) { + return; + } + break; + default: + dis.titleLine("ERROR", true); + System.out.println("+ Ϸ!"); + break; + } + } + } } diff --git a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/View/AdminView.java b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/View/AdminView.java index 4d5bd21..ab3cbe9 100644 --- a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/View/AdminView.java +++ b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/View/AdminView.java @@ -1,32 +1,45 @@ +/* +* Date: 2024-06-18 +* Design By JRNitre +* */ + package View; import dao.AdminDaoImpl; import dao.BusinessDaoImpl; + import po.Admin; import po.Business; import java.sql.ResultSet; + +import java.util.Objects; import java.util.Scanner; +import java.util.regex.Pattern; public class AdminView { Display dis = new Display('+', '-', 30); - Scanner sca = new Scanner(System.in); - // Ա¼ + /** + * Ա¼ + * ͨ Scanner ӱ׼ȡԱûID룬Խе¼ + * ԱûIDƥɹ򷵻ضӦĹԱ󣻷ԱԶε¼ֱԴþ + * + * @return ɹ¼ʱعԱ󣬷򷵻 null + */ public Admin login(){ + Scanner sca = new Scanner(System.in); // ǰ int mistake = 0; // ûԵ int maxMistake = 3; - - String tempName; - String tempPassword; + // ¼״̬ + int loginCode; Admin admin = null; while (mistake < maxMistake){ - dis.titleLine("¼ [ô] Ա̨", true); // 1 û @@ -34,16 +47,35 @@ public class AdminView { System.out.println("+ " + mistake + " ʣɳԴ " + (maxMistake - mistake) + " "); } - // TODO ûϷж - System.out.print("+ ˻: "); - tempName = sca.next(); + // String ʽû Id + String inputAdminNameOrId; + // ¼ Id ֵ -1 + int inputAdminId = -1; + System.out.print("+ ˻&Id: "); + inputAdminNameOrId = sca.nextLine(); + // жûַǴ + // ֱòͬķִе¼ + try{ + inputAdminId = Integer.parseInt(inputAdminNameOrId); + loginCode = 0; + } catch (NumberFormatException e){ + loginCode = 1; + } + + // ȡû + String inputAdminPassword; System.out.print("+ : "); - tempPassword = sca.next(); + inputAdminPassword = sca.nextLine(); System.out.println("+ ¼ϵͳ..."); - admin = new AdminDaoImpl().getAdminByNameByPass(tempName, tempPassword); + if (loginCode == 0){ + admin = new AdminDaoImpl().getAdminByIdByPass(inputAdminId, inputAdminPassword); + } + if (loginCode == 1){ + admin = new AdminDaoImpl().getAdminByNameByPass(inputAdminNameOrId, inputAdminPassword); + } if (admin != null){ return admin; @@ -54,70 +86,240 @@ public class AdminView { return null; } + /** + * ¹Ա롣 + * ÷ṩһʽĽ棬ڹԱ֤ǰ벢롣 + * ȷȷԺͰȫԣΪ10ַ + * + * @param admin Ա󣬰ǰԱϢ + */ public void updatePassword(Admin admin){ - System.out.println("+ ..."); + // ʱԱոڼû + Admin tempAdmin = new Admin(admin); + dis.titleLine("", true); - new AdminDaoImpl().updateAdminPassword(admin); + while (true){ + // ûԭ + Scanner sca = new Scanner(System.in); + String afterAdminPassword; + System.out.print("+ ԭ: "); + afterAdminPassword = sca.nextLine(); + + // ж¾Ƿ + if (afterAdminPassword.equals(tempAdmin.getAdminPassword())){ + System.out.println("+ ȷ"); + + while (true){ + String newAdminPassword; + + // ȷû + while (true){ + String againNewAdminPassword; + System.out.print("+ (10ַ): "); + newAdminPassword = sca.nextLine(); + System.out.print("+ ٴ (10ַ): "); + againNewAdminPassword = sca.nextLine(); + + if (newAdminPassword.equals(againNewAdminPassword)){ + break; + } + + System.out.println("+ ǰ벻һ"); + } + + // ж볤, С 10 + if (newAdminPassword.length() <= 10){ + System.out.println("+ ..."); + // û洢ʱ + tempAdmin.setAdminPassword(newAdminPassword); + + // ݿ + if (new AdminDaoImpl().updateAdminPassword(admin)){ + // ʱе븳ֵû + admin.setAdminPassword(tempAdmin.getAdminPassword()); + System.out.println("+ ³ɹ!"); + } else { + System.out.println("+ ʧ!"); + } + break; + } + System.out.println("+ !"); + } + dis.titleLine("˵", true); + sca.nextLine(); + return; + } else { + System.out.println("+ "); + System.out.print("+ [y] : "); + String exitKey = sca.next(); + if (!Objects.equals(exitKey, "y") && !Objects.equals(exitKey, "Y")){ + return; + } + } + } } + /** + * Ա̼ҹܡ + * ÷̨̼ͨϢ̼ID̼ơ̼롢̼ҵַ̼ҽܡͷѺͷѡ + * Ϣ֤󣬽̼ϢӵϵͳС + */ public void adminAddBusiness(){ Business business = new Business(); - Scanner sca = new Scanner(System.in); Display dis = new Display('+', '-', 30); - int tempBusinessId; - String tempBusinessName; - String tempBusinessPassword; - dis.titleLine("̼", true); - System.out.print("+ ̼ ID: "); - tempBusinessId = sca.nextInt(); - sca.nextLine(); + // ̼ Id + while(true){ + String inputBusinessId; + Scanner sca = new Scanner(System.in); - System.out.print("+ ̼: "); - business.setBusinessName(sca.nextLine()); + System.out.print("+ ̼λ ID: "); + inputBusinessId = sca.nextLine(); - System.out.print("+ ̼: "); - tempBusinessPassword = sca.nextLine(); - - System.out.print("+ ̼ҵַ: "); - business.setBusinessAddress(sca.nextLine()); - - System.out.print("+ ̼ҽ: "); - business.setBusinessExplain(sca.nextLine()); - - System.out.print("+ ͷ: "); - while (!sca.hasNextDouble()) { - System.out.println("һЧΪͷѣ"); - sca.next(); // + // ʽжǷΪλ + if (Pattern.matches("^\\d{4}$", inputBusinessId)) { + int businessId = Integer.parseInt(inputBusinessId); + business.setBusinessId(businessId); + break; + } else { + if (!inputBusinessId.matches("\\d+")){ + System.out.println("+ ַ!"); + } + System.out.println("+ ̼ ID λȷ!"); + } } - business.setBusinessStarPrice(sca.nextDouble()); - System.out.print("+ ͷ: "); - while (!sca.hasNextDouble()) { - System.out.println("+ һЧΪͷѣ"); - sca.next(); // + // ̼ + while (true){ + String inputBusinessName; + int maxDataLength = 10; + System.out.print("+ ̼ (" + maxDataLength + "ַ): "); + + inputBusinessName = dis.getStandString(maxDataLength); + + if (inputBusinessName != null){ + business.setBusinessName(dis.appendSpaceToString(inputBusinessName, maxDataLength)); + break; + } + System.out.println("+ ̼Ϸ!"); } - business.setBusinessDeliveryPrice(sca.nextDouble()); - sca.nextLine(); + // ̼ + while (true){ + String inputBusinessPassword; + int maxDataLength = 10; + System.out.print("+ ̼ (" + maxDataLength + "ַ): "); - System.out.println("+ ̻..."); + inputBusinessPassword = dis.getStandString(maxDataLength); - business.setBusinessId(tempBusinessId); - business.setBusinessPassword(tempBusinessPassword); + if (inputBusinessPassword != null){ + business.setBusinessPassword(inputBusinessPassword); + break; + } + System.out.println("+ 벻Ϸ!"); + } - new BusinessDaoImpl().addNewBusiness(business); + // ̼ҵַ + while (true){ + String inputBusinessAddress; + int maxDataLength = 20; + System.out.print("+ ̼ҵַ (" + maxDataLength + "ַ): "); + + inputBusinessAddress = dis.getStandString(maxDataLength); + + if (inputBusinessAddress != null){ + business.setBusinessAddress(inputBusinessAddress); + break; + } + System.out.println("+ ̼ҵַϷ!"); + } + + // ̼ҽ + while (true){ + String inputBusinessExplain; + int maxDataLength = 25; + System.out.print("+ ̼ҽ (" + maxDataLength + "ַ): "); + + inputBusinessExplain = dis.getStandString(maxDataLength); + + if (inputBusinessExplain != null){ + business.setBusinessExplain(inputBusinessExplain); + break; + } + System.out.println("+ ̼ҽܲϷ!"); + } + + // ͷ + while (true){ + double inputBusinessStartPrice; + Scanner sca = new Scanner(System.in); + System.out.print("+ ͷ: "); + if (!sca.hasNextDouble()){ + System.out.println("+ һЧ"); + sca.next(); + } else { + inputBusinessStartPrice = sca.nextDouble(); + if (inputBusinessStartPrice > 0 && inputBusinessStartPrice <= 50){ + business.setBusinessStarPrice(inputBusinessStartPrice); + break; + } else { + System.out.println("+ ݷΧ (0 ~ 50)"); + } + } + } + + // ͷ + while (true){ + double inputBusinessDeliveryPrice; + Scanner sca = new Scanner(System.in); + System.out.print("+ ͷ: "); + if (!sca.hasNextDouble()){ + System.out.println("+ һЧ"); + sca.next(); + } else { + inputBusinessDeliveryPrice = sca.nextDouble(); + if (inputBusinessDeliveryPrice > 0 && inputBusinessDeliveryPrice <= 50){ + business.setBusinessDeliveryPrice(inputBusinessDeliveryPrice); + break; + } else { + System.out.println("+ ݷΧ (0 ~ 50)"); + } + } + } + + System.out.println("+ ! ̻..."); + + if (new BusinessDaoImpl().addNewBusiness(business)){ + System.out.println("+ ӳɹ!"); + } else { + System.out.println("+ ʧ!"); + } + + dis.pressEnterToContinue(); } - public int printBusinessInfo(ResultSet rs) { + /** + * ӡҵϢ + * ResultSetȡҵϢԸʽķʽӡ + * ÿҵϢҵIDҵơ롢ҵַҵ˵Ǽ۸ͼ۸ + * ҵIDʹλָʽ + * + * @param rs ResultSet󣬰ҵϢݡ + * @return ӡҵϢ + */ + public int printInputBusinessInfo(ResultSet rs) { + // û¼˶Ϣ int i = 0; try { while (rs.next()){ StringBuilder str = new StringBuilder(); str.append("| "); - str.append(rs.getInt("businessId")); + + String formattedNumber = String.format("%04d", rs.getInt("businessId")); + str.append(formattedNumber); + str.append("\t| "); str.append(rs.getString("businessName")); str.append("\t| "); @@ -132,7 +334,7 @@ public class AdminView { str.append(rs.getDouble("deliveryPrice")); str.append("\t|"); - System.out.println(str.toString()); + System.out.println(str); i++; } } catch (Exception e) { @@ -141,6 +343,13 @@ public class AdminView { return i; } + /** + * ӡԱϢ + * ResultSetȡԱIDƺ룬Ըʽʽӡ + * + * @param rs ResultSet󣬰ԱϢݡ + * @return ӡĹԱ + */ public int printAdminInfo(ResultSet rs) { int i = 0; try { @@ -154,7 +363,8 @@ public class AdminView { str.append(rs.getString("adminPassword")); str.append("\t|"); - System.out.println(str.toString()); + String finalString = str.toString(); + System.out.println(finalString); i++; } } catch (Exception e) { @@ -163,92 +373,133 @@ public class AdminView { return i; } + /** + * ӡйԱϢ + * ˷ȴӡһУȻйԱϢӡʾԱ + * ʹStringBuilderַԼResultSetݿȡĹԱݡ + * ڻȡӡݵĹз쳣ӡ쳣ջ١ + */ public void printAllAdminInfo(){ dis.titleLine("ԱϢ", true); - StringBuilder str = new StringBuilder(); - System.out.println("| ԱID\t| Ա\t| Ա\t|"); try { // ӡ ResultSet rs = new AdminDaoImpl().getAllAdmin(); - this.printAdminInfo(rs); + int num; + num = this.printAdminInfo(rs); + + System.out.println("+ [" + num + "] Ŀ"); } catch (Exception e){ e.printStackTrace(); } } - // г̼Ϣ + /** + * ӡԱϢҪûȷϡ + * ÷ԸʽķʽʾԱϸϢIDƺ롣 + * ϢʾҪûȷϣͨɨʵ֡ + * + * @param admin Ա󣬰ҪӡϢ + */ + public void printAdminInfo(Admin admin){ + dis.titleLine("Ա [" + admin.getAdminName() + "]" + " Ϣ", true); + System.out.println("+ Ա ID: " + admin.getAdminId()); + System.out.println("+ Ա : " + admin.getAdminName()); + System.out.println("+ Ա: " + admin.getAdminPassword()); + dis.showLine(true); + Scanner sca = new Scanner(System.in); + sca.nextLine(); + } + + /** + * ӡ̼Ϣ + * ˷ڲѯչʾϵͳе̼Ϣ̼ҵĻϢ + * ʹResultSetѯͨprintInputBusinessInfoӡ̼Ϣ + * 󣬴ӡ̼ҵ + */ public void printAllBusinessInfo(){ + int num; + dis.titleLine("̼Ϣ", true); - - StringBuilder str = new StringBuilder(); - int businessNumber = new BusinessDaoImpl().getBusinessNumber(); - - if (businessNumber == -1){ - System.out.println("+ ̼: Error"); - } else { - System.out.println("+ ̼: " + businessNumber); - } - - dis.titleLine("̼б", true); - System.out.println("| ̼ID\t| ̼\t| ̼\t| ̼ҵַ\t| ̼ҽ\t| ͷ\t| ͷ\t|"); + dis.printInfoTitle(); try { - // ӡ ResultSet rs = new BusinessDaoImpl().printAllBusiness(); - - this.printBusinessInfo(rs); - + num = this.printInputBusinessInfo(rs); + System.out.println("+ ϼ̼Ŀ [" + num + "]"); } catch (Exception e){ e.printStackTrace(); } } + // ̼ + // TODO ߼Ҫ + // TODO ѡĿ public void searchBusiness(){ - int key, num; + Scanner sca = new Scanner(System.in); - dis.titleLine("̼", true); - dis.showItem("[0] ̼ѯ", true); - dis.showLine(true); + int key; + // ڽִϺ󷵻ص + ResultSet rs; - key = dis.getKeyNumber(); + while ( true ) { + dis.titleLine("̼", true); + dis.showItem("[0] ̼ѯ", true); + dis.showLine(true); - switch (key){ - case 0: - dis.titleLine("̼ѯ", true); - System.out.print("+ Ҫѯ̼: "); - String businessName = sca.next(); + key = dis.getKeyNumber(); - try { - System.out.println("+ ѯ..."); - ResultSet rs = new AdminDaoImpl().fuzzyQueryExampleForName(businessName); - // жǷΪ - if (rs == null){ - dis.titleLine("Error", true); - System.out.println("+ δѯй!"); - return; + switch (key){ + case 0: + dis.titleLine("̼ѯ", true); + System.out.print("+ Ҫѯ̼: "); + String businessName = sca.next(); + + try { + System.out.println("+ ѯ..."); + rs = new AdminDaoImpl().fuzzyQueryExampleForName(businessName); + // жǷΪ + if (rs == null){ + dis.titleLine("Error", true); + System.out.println("+ δѯй!"); + return; + } + + dis.printInfoTitle(); + int num; + num = this.printInputBusinessInfo(rs); + + System.out.println("+ ѯ " + num + ""); + } catch (Exception e){ + e.printStackTrace(); } - num = this.printBusinessInfo(rs); - - System.out.println("+ ѯ " + num + ""); - - } catch (Exception e){ - e.printStackTrace(); - } + return; + default: + dis.titleLine("ERROR", true); + System.out.println("+ Ϸ!"); + break; + } } } + /** + * ɾ̼ҹܵʵ֡ + * ͨû̼IDƣݿɾӦ̼Ϣ + * ȳԽûΪΪ̼IDɾ + * ʧܣΪû̼ƣͨƽɾ + */ public void deleteBusiness(){ + Scanner sca = new Scanner(System.in); + dis.titleLine("ɾ̼", true); System.out.print("+ Ҫɾ̼ҵ Id : "); String uin = sca.nextLine(); - try{ int uinToNumber = Integer.parseInt(uin); System.out.println("+ ѯɾ Id Ϊ: " + uin + " ̼..."); @@ -257,6 +508,51 @@ public class AdminView { System.out.println("+ ѯɾΪ: " + uin + " ̼..."); new BusinessDaoImpl().deleteName(uin); } + } + /** + * ӡʳƷϢ + * ÷ڹԱ棬չʾݿʳƷϸϢ + * ͨѯݿȡʳƷϢӡЩϢ + */ + public void printAdminAllFoodInfo(){ + int num = 0; + dis.titleLine("ʳƷб", true); + + try { + System.out.println("+ ѯ..."); + ResultSet rs = new BusinessDaoImpl().getAllFoodInfo(); + // жǷΪ + if (rs == null){ + dis.titleLine("Error", true); + System.out.println("+ δѯй!"); + return; + } + + while ( rs.next() ){ + StringBuilder str = new StringBuilder(); + + str.append("| "); + str.append(rs.getString("foodId")); + str.append("\t| "); + str.append(rs.getString("foodName")); + str.append("\t| "); + str.append(rs.getString("foodExplain")); + str.append("\t| "); + str.append(rs.getDouble("foodPrice")); + str.append("\t| "); + str.append(rs.getInt("businessId")); + str.append("\t|"); + + String finalString = str.toString(); + System.out.println(finalString); + num++; + } + + System.out.println("+ ѯ " + num + " "); + + } catch (Exception e){ + e.printStackTrace(); + } } } diff --git a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/View/BusinessView.java b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/View/BusinessView.java index cf57beb..8b44dfc 100644 --- a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/View/BusinessView.java +++ b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/View/BusinessView.java @@ -1,16 +1,615 @@ +/* + * Date: 2024-06-18 + * Design By JRNitre + * */ + package View; import dao.BusinessDaoImpl; import po.Business; +import po.Food; + +import java.sql.ResultSet; +import java.util.Objects; +import java.util.Scanner; +import java.util.regex.Pattern; public class BusinessView { + Display dis = new Display('+', '-', 30); + + /** + * ̻¼ + * ͨ Scanner ӱ׼ȡ̻ûID룬Խе¼ + * ̻ûIDƥɹ򷵻ضӦ̻󣻷̻Զε¼ֱԴþ + * + * @return ɹ¼ʱ̻󣬷򷵻 null + */ + public Business login () { + Scanner sca = new Scanner(System.in); + + // ǰ + int mistake = 0; + // ûԵ + int maxMistake = 3; + + int loginCode; - public Business login (String name, String password){ Business business = null; - business = new BusinessDaoImpl().getBusinessByNameByPass(name, password); + while ( mistake < maxMistake ) { - return business; + dis.titleLine("¼ [ô] ̼Һ̨", true); + + // 1 û + if ( mistake != 0 ) { + System.out.println("+ " + mistake + " ʣɳԴ " + (maxMistake - mistake) + " "); + } + + String inputBusinessNameOrId; + int inputBusinessId = - 1; + System.out.print("+ ˻&Id: "); + inputBusinessNameOrId = sca.nextLine(); + + try { + inputBusinessId = Integer.parseInt(inputBusinessNameOrId); + loginCode = 0; + } catch ( NumberFormatException e ) { + loginCode = 1; + } + + String inputBusinessPassword; + System.out.print("+ : "); + inputBusinessPassword = sca.nextLine(); + + System.out.println("+ ¼ϵͳ..."); + + if ( loginCode == 0 ) { + business = new BusinessDaoImpl().getBusinessByIdByPass(inputBusinessId, inputBusinessPassword); + } + if ( loginCode == 1 ) { + business = new BusinessDaoImpl().getBusinessByNameByPass(inputBusinessNameOrId, inputBusinessPassword); + } + + if ( business != null ) { + return business; + } else { + mistake++; + } + } + return null; } -} + /** + * ӡ̼Ϣ + * ÷ϸӡ̼ҵĸϢIDơ롢ܡַͷѺͷѡ + * ּΪ̼ҹṩӻϢչʾû鿴͹̼顣 + * + * @param business ̼Ҷ󣬰Ҫӡ̼Ϣ + */ + public void printBusinessInfo (Business business) { + String businessName = dis.deleteSpaceToString(business.getBusinessName()); + String businessPassword = dis.deleteSpaceToString(business.getBusinessPassword()); + String businessExplain = dis.deleteSpaceToString(business.getBusinessExplain()); + String businessAddress = dis.deleteSpaceToString(business.getBusinessAddress()); + + dis.titleLine("̼ [" + businessName + "]" + " Ϣ", true); + + String formattedNumber = String.format("%04d", business.getBusinessId()); + System.out.println("+ ̼ ID: " + formattedNumber); + + System.out.println("+ ̼ :" + businessName); + System.out.println("+ ̼:" + businessPassword); + System.out.println("+ ̼ҽ:" + businessExplain); + System.out.println("+ ̼ҵַ:" + businessAddress); + System.out.println("+ ͷ: " + business.getBusinessStarPrice()); + System.out.println("+ ͷ: " + business.getBusinessDeliveryPrice()); + + dis.showLine(true); + dis.pressEnterToContinue(); + } + + /** + * ӡʳƷϢб + * ÷ ResultSet лȡʳƷϢдӡ + * ּṩӻչʾʳƷϢбû鿴͹ʳƷϢ + * + * @param rs ResultSet 󣬰ҪӡʳƷϢ + * @return ӡʳƷϢб + */ + public int printFoodInfoList (ResultSet rs) { + int num = 0; + try { + while ( rs.next() ) { + StringBuilder str = new StringBuilder(); + + str.append("| "); + String formattedNumber = String.format("%04d", rs.getInt("foodId")); + str.append(formattedNumber); + str.append("\t| "); + str.append(rs.getString("foodName")); + str.append("\t| "); + str.append(rs.getString("foodExplain")); + str.append("\t| "); + str.append(rs.getDouble("foodPrice")); + str.append("\t|"); + + String finalStr = str.toString(); + System.out.println(finalStr); + num++; + } + return num; + } catch ( Exception e ) { + e.printStackTrace(); + } + return num; + } + + /** + * ӡʳƷϢбı⡣ + * ÷ӡʳƷϢбı⣬IDʳƷʳƷܡ۸ֶΡ + * ּṩӻչʾʳƷϢбı⣬û鿴͹ʳƷϢ + */ + public void printFoodInfoListTitle () { + System.out.println("| ID\t| ʳƷ\t\t\t| ʳƷ\t\t\t\t\t| ۸\t|"); + } + + /** + * ӡ̼ʳƷϢ + * ÷ ResultSet лȡ̼ʳƷϢдӡ + * ּṩӻչʾ̼ʳƷϢû鿴͹ʳƷϢ + * + * @param business ̼Ҷ󣬰Ҫӡ̼Ϣ + */ + public void printAllBusinessFoodInfo (Business business) { + dis.titleLine("̼ [" + dis.deleteSpaceToString(business.getBusinessName()) + "] Ʒ", true); + try { + System.out.println("+ ѯ..."); + ResultSet rs = new BusinessDaoImpl().getBusinessFoodInfo(business); + // жǷΪ + if ( rs == null ) { + dis.titleLine("Error", true); + System.out.println("+ δѯй!"); + return; + } + + this.printFoodInfoListTitle(); + int num = this.printFoodInfoList(rs); + + System.out.println("+ ѯ " + num + " "); + } catch ( Exception e ) { + e.printStackTrace(); + } + dis.showLine(true); + dis.pressEnterToContinue(); + } + + /** + * ̼Ϣ + * ÷û̼Ϣ̼̼ҽܡ̼ҵַͷѺͷѵֶΡ + * ּṩӻĸ̼Ϣû̼Ϣ + * + * @param business ̼Ҷ󣬰Ҫµ̼Ϣ + */ + public void updateBusinessInfo (Business business) { + Business newBusiness = new Business(business); + int key; + boolean exitStatus = false; + while ( true ) { + dis.titleLine("̼Ϣ", true); + System.out.println("[0] ̼"); + System.out.println("[1] ̼ҽ"); + System.out.println("[2] ̼ҵַ"); + System.out.println("[3] ̼ͷ"); + System.out.println("[4] ̼ͷ"); + System.out.println("[5] ˳"); + dis.showLine(true); + + key = dis.getKeyNumber(); + + switch ( key ) { + case 0: + while ( true ) { + String newBusinessName; + int maxDataLength = 10; + dis.titleLine("̼", true); + System.out.print("+ ̼ (" + maxDataLength + "ַ): "); + + newBusinessName = dis.getStandString(maxDataLength); + + if (newBusinessName != null){ + newBusiness.setBusinessName(newBusinessName); + break; + } + System.out.println("+ ̼ƲϷ"); + } + break; + case 5: + return; + default: + dis.titleLine("ERROR", true); + System.out.println("+ Ϸ!"); + break; + } + + System.out.println("+ , ǷҪѡĵĿ"); + System.out.println("+ [y] - ȷϸĸ̼"); + System.out.println("+ [n] - Ҫĵ"); + System.out.println("+ [e] - ˳, Ҫĵ"); + System.out.print("+ ҪIJ: "); + + while ( true ) { + Scanner sca = new Scanner(System.in); + String str_key = sca.nextLine(); + if ( str_key.equals("y") || str_key.equals("Y") ) { + System.out.println("+ ..."); + if ( new BusinessDaoImpl().updateBusinessInfo(newBusiness) ) { + System.out.println("+ ..."); + } else { + System.out.println("+ ʧ..."); + } + exitStatus = true; + break; + } + if ( str_key.equals("n") || str_key.equals("N") ) { break; } + if ( str_key.equals("e") || str_key.equals("E") ) { + System.out.println("+ ˳"); + exitStatus = true; + break; + } + System.out.println("+ 벻Ϸ!"); + } + + if ( exitStatus ) { break; } + } + + dis.showLine(true); + dis.pressEnterToContinue(); + } + + /** + * ̼롣 + * ÷ṩһʽĽ棬̼֤ǰ벢롣 + * ȷȷԺͰȫԣΪ10ַ + * + * @param business Ա󣬰ǰԱϢ + */ + public void updatePassword (Business business) { + // ʱ̻ոڼû + Business tempBusiness = new Business(business); + dis.titleLine("", true); + + while (true){ + // ûԭ + Scanner sca = new Scanner(System.in); + String afterBusinessPassword; + System.out.print("+ ԭ: "); + afterBusinessPassword = sca.nextLine(); + + // ж¾Ƿ + if (afterBusinessPassword.equals(tempBusiness.getBusinessPassword())){ + System.out.println("+ ȷ"); + + while (true){ + String newBusinessPassword; + + // ȷû + while (true){ + String againNewBusinessPassword; + System.out.print("+ (10ַ): "); + newBusinessPassword = sca.nextLine(); + System.out.print("+ ٴ (10ַ): "); + againNewBusinessPassword = sca.nextLine(); + + if (newBusinessPassword.equals(againNewBusinessPassword)){ + break; + } + + System.out.println("+ ǰ벻һ"); + } + + // ж볤, С 10 + if (newBusinessPassword.length() <= 10){ + System.out.println("+ ..."); + // û洢ʱ + tempBusiness.setBusinessPassword(newBusinessPassword); + + // ݿ + if (new BusinessDaoImpl().updateBusinessPassword(tempBusiness)){ + // ʱе븳ֵû + business.setBusinessPassword(tempBusiness.getBusinessPassword()); + System.out.println("+ ³ɹ!"); + } else { + System.out.println("+ ʧ!"); + } + break; + } + System.out.println("+ !"); + } + dis.titleLine("˵", true); + sca.nextLine(); + return; + } else { + System.out.println("+ "); + System.out.print("+ [y] : "); + String exitKey = sca.next(); + if (!Objects.equals(exitKey, "y") && !Objects.equals(exitKey, "Y")){ + return; + } + } + } + } + + /** + * µʳƷ + * ÷ṩһʽĽ棬µʳƷ + * ȷʳƷƵȷԺͰȫԣΪ10ַ + * + * @param business Ա󣬰ǰԱϢ + */ + public void addBusinessNewFood (Business business) { + Food food = new Food(); + Display dis = new Display('+', '-', 50); + dis.titleLine("̼ʳƷ", true); + + while ( true ) { + String foodId; + Scanner sca = new Scanner(System.in); + System.out.print("+ ʳƷ (10ַ): "); + + foodId = sca.nextLine(); + + if ( Pattern.matches("^\\d{4}$", foodId) ) { + int foodIdForInt = Integer.parseInt(foodId); + food.setFoodId(foodIdForInt); + break; + } else { + if ( ! foodId.matches("\\d+") ) { + System.out.println("+ ַ!"); + } else { + System.out.println("+ ID λȷ!"); + } + } + } + + while ( true ) { + String foodName; + int max_length = 10; + System.out.print("+ ʳƷ (" + max_length + "ַ): "); + + foodName = dis.getStandString(max_length); + + if (foodName != null){ + food.setFoodName(foodName); + break; + } + System.out.println("+ ʳƷϷ!"); + } + + while ( true ) { + String foodExplain; + int max_length = 25; + System.out.print("+ ʳƷ (" + max_length + "ַ): "); + + foodExplain = dis.getStandString(max_length); + + if (foodExplain != null){ + food.setFoodExplain(foodExplain); + break; + } + System.out.println("+ ʳƷܲϷ!"); + } + + while ( true ) { + double foodPrice; + Scanner sca = new Scanner(System.in); + System.out.print("+ ʳƷ۸: "); + + if ( ! sca.hasNextDouble() ) { + System.out.println("+ 벻Ϸ"); + sca.next(); + } else { + foodPrice = sca.nextDouble(); + if ( foodPrice > 0 && foodPrice <= 9999 ) { + food.setFoodPrice(foodPrice); + break; + } else { + System.out.println("+ ݷΧ (0 ~ 9999)"); + } + } + } + + // ͬ ID + food.setBusinessId(business.getBusinessId()); + + while ( true ) { + dis.showLine(true); + System.out.println("+ [y] ȷʳƷ"); + System.out.println("+ [n] ȡʳƷ "); + + Scanner sca = new Scanner(System.in); + String exitKey = sca.next(); + + if ( exitKey.equals("y") || exitKey.equals("Y") ) { + if ( new BusinessDaoImpl().addNewFood(food) ) { + System.out.println("+ ӳɹ!"); + } else { + System.out.println("+ ʧ!"); + } + return; + } else if ( exitKey.equals("n") || exitKey.equals("N") ) { + System.out.println("+ ˳"); + return; + } + } + } + + /** + * ʳƷϢ + * ÷ṩһֽʽĽ棬ڸʳƷϢ + * ȷʳƷƵȷԺͰȫԣΪ10ַ + * + * @param business Ա󣬰ǰԱϢ + */ + public void updateBusinessFoodInfo (Business business) { + Display dis = new Display('+', '-', 30); + Food updateFood; + + dis.titleLine("޸ʳƷϢ", true); + System.out.println("+ û [" + business.getBusinessName() + "] ʳƷ±"); + + ResultSet rs = new BusinessDaoImpl().getBusinessFoodInfo(business); + this.printFoodInfoListTitle(); + this.printFoodInfoList(rs); + dis.showLine(true); + + int updateFoodId; + while ( true ){ + Scanner sca = new Scanner(System.in); + System.out.print("+ Ҫ޸ĵʳƷĿ ID: "); + + while ( ! sca.hasNextInt() ) { + System.out.println("+ ID"); + sca.next(); + System.out.print("+ Ҫ޸ĵʳƷĿ ID: "); + } + updateFoodId = sca.nextInt(); + updateFood = new BusinessDaoImpl().getFoodById(updateFoodId, business.getBusinessId()); + + if ( updateFood != null ){ + System.out.println("+ ɹ!"); + break; + } + System.out.println("+ δܲѯָƷ"); + } + + int key; + while ( true ) { + dis.titleLine("ѡҪ޸ĵĿ", true); + dis.showItem("[0] Ʒ", true); + dis.showItem("[1] Ʒ", true); + dis.showItem("[2] Ʒ۸", true); + dis.showItem("[3] ʳƷϢ", true); + dis.showItem("[4] ϼ", true); + dis.showLine(true); + + key = dis.getKeyNumber(); + + switch ( key ) { + case 0: + while ( true ) { + String foodName; + int maxDataLength = 10; + System.out.print("+ ʳƷ (" + maxDataLength + "ַ): "); + + foodName = dis.getStandString(maxDataLength); + + if (foodName != null){ + updateFood.setFoodName(foodName); + break; + } + System.out.println("+ ʳƷϷ"); + } + break; + case 1: + while ( true ) { + String foodExplain; + int maxDataLength = 10; + System.out.print("+ ʳƷ (" + maxDataLength + "ַ): "); + + foodExplain = dis.getStandString(maxDataLength); + + if (foodExplain != null){ + updateFood.setFoodExplain(foodExplain); + break; + } + System.out.println("+ ʳƷܲϷ"); + } + break; + case 2: + while ( true ) { + double foodPrice; + Scanner case2_sca = new Scanner(System.in); + System.out.print("+ ʳƷ۸: "); + + if ( ! case2_sca.hasNextDouble() ) { + System.out.println("+ һЧΪʳƷ۸"); + case2_sca.next(); + } else { + foodPrice = case2_sca.nextDouble(); + if ( foodPrice > 0 && foodPrice <= 9999 ) { + updateFood.setFoodPrice(foodPrice); + break; + } else { + System.out.println("+ ݷΧ! Ҫ (0 ~ 9999)"); + } + } + } + case 3: + while (true){ + dis.titleLine("ȷϸʳƷϢ", true); + System.out.println("+ [y] ȷϸʳƷϢ"); + System.out.print("+ [n] ȡʳƷϢ: "); + + Scanner case3_sca = new Scanner(System.in); + String case3_Key = case3_sca.next(); + + if (case3_Key.equals("y") || case3_Key.equals("Y")) { + updateFood.setFoodId(updateFoodId); + updateFood.setBusinessId(business.getBusinessId()); + if (new BusinessDaoImpl().updateFoodInfo(updateFood, business)){ + System.out.println("+ ³ɹ!"); + break; + } + System.out.println("+ ʧ"); + } else if (case3_Key.equals("n") || case3_Key.equals("N")) { break; } + } + break; + case 4: + if (dis.MessageYesOrNo("ʾ", "ȷϼ˵?")) { + return; + } + break; + default: + dis.titleLine("ERROR", true); + System.out.println("+ Ϸ!"); + break; + } + } + } + + /** + * ɾʳƷϢ + * ͨʳƷIDƣɾӦʳƷ¼ + * ȳԽΪIDʧܣΪʳƷơ + * + * @param business ҵִɾ + */ + public void deleteFood(Business business){ + Scanner sca = new Scanner(System.in); + + dis.titleLine("ɾʳƷ", true); + System.out.print("+ ҪɾʳƷ ID : "); + String uin = sca.nextLine(); + + try{ + int uinToNumber = Integer.parseInt(uin); + System.out.println("+ ѯɾ ID Ϊ: " + uin + " ʳƷ..."); + + if (new BusinessDaoImpl().deleteFoodId(uinToNumber, business)){ + System.out.println("+ ID ɾɹ"); + return; + } + + System.out.println("+ ID ɾʧ"); + } catch (NumberFormatException e){ + System.out.println("+ ѯɾΪ: " + uin + " ʳƷ..."); + + if (new BusinessDaoImpl().deleteFoodName(dis.appendSpaceToString(uin, 10), business)){ + System.out.println("+ NAME ɾɹ"); + return; + } + + System.out.println("+ NAME ɾʧ"); + } + } +} \ No newline at end of file diff --git a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/View/Display.java b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/View/Display.java index 9a25048..3b2f64b 100644 --- a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/View/Display.java +++ b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/View/Display.java @@ -1,76 +1,92 @@ +/* + * Date: 2024-06-18 + * Design By JRNitre + * */ + package View; import java.util.Scanner; public class Display { - private char displayNode; - private char displayLine; - - private Integer displayWidth; + // Ա + private char displayNode; // ַ + private char displayLine; // ָ ַ + private Integer displayWidth; // ʾ Scanner sca = new Scanner(System.in); + // 캯 public Display (char node, char line, Integer width){ displayNode = node; displayLine = line; displayWidth = width; } - public void setWidth(Integer width){ - displayWidth = width; - } - + /** + * ڿ̨Աʽַ + * ݿ̨ȣԶҿհ׿ȣʹʾ + * + * @param title Ҫıַ + * @param newLine ǷСΪtrueУΪfalse򲻻С + */ public void titleLine (String title, boolean newLine){ StringBuilder str = new StringBuilder(); - Integer space = (displayWidth - title.length() - 2) / 2; + int space = (displayWidth - title.length() - 2) / 2; str.append(displayNode); - for (int i = 0; i < space; i++){ - str.append(displayLine); - } + str.append(String.valueOf(displayLine).repeat(Math.max(0, space))); str.append(title); - for (int i = 0; i < space; i++){ - str.append(displayLine); - } + str.append(String.valueOf(displayLine).repeat(Math.max(0, space))); str.append(displayNode); - System.out.print(str.toString()); - if (newLine){ - System.out.println(); - } + String finalStr = str.toString(); + System.out.print(finalStr); + if (newLine){ System.out.println(); } } + /** + * ڿ̨һĿԶĿҿհ׿ȣʹʾ + * + * @param item ҪĿַ + * @param newLine ǷСΪtrueĿУΪfalse򲻻С + */ public void showItem(String item, boolean newLine){ StringBuilder str = new StringBuilder(); str.append(displayNode).append(" "); str.append(item); - Integer space = displayWidth - 1 - ("+ ".length() + item.length()); - - System.out.print(str.toString()); - if (newLine){ - System.out.println(); - } + String finalStr = str.toString(); + System.out.print(finalStr); + if (newLine){ System.out.println(); } } + /** + * ڿ̨һָߡ + * + * @param newLine ǷСΪtrueָߺУΪfalse򲻻С + */ public void showLine(boolean newLine){ StringBuilder str = new StringBuilder(); str.append(displayNode); - for (int i = 0; i < displayWidth - 2; i++){ - str.append(displayLine); - } + str.append(String.valueOf(displayLine).repeat(Math.max(0, displayWidth - 2))); str.append(displayNode); - System.out.print(str.toString()); - if (newLine){ - System.out.println(); - } + String finalStr = str.toString(); + System.out.print(finalStr); + if (newLine){ System.out.println(); } } + /** + * ڿ̨һϢѯûǷȷϻȡ + * + * @param title Ϣı⡣ + * @param data Ϣݡ + * @return ûȷϣ򷵻trueûȡ򷵻false + */ public boolean MessageYesOrNo(String title, String data){ this.titleLine(title, true); System.out.println("+ " + data); @@ -78,12 +94,15 @@ public class Display { String key = sca.next(); - if (key.equals("y") || key.equals("Y")){ - return true; - } - return false; + return key.equals("y") || key.equals("Y"); } + /** + * ûлȡһ + * ÷ʾûһֱûȷΪֹ + * + * @return ûֵ + */ public int getKeyNumber(){ int userInput; @@ -96,8 +115,98 @@ public class Display { } userInput = sca.nextInt(); - return userInput; } + /** + * ַӿոֱַܳȴﵽָ󳤶ȡ + * ԭʼַijѾڻ򳬹󳤶ȣԭظַ + * + * @param data ԭʼַ + * @param max_length ָ󳤶 + * @return ӿոַ + */ + public String appendSpaceToString(String data, int max_length){ + StringBuilder str = new StringBuilder(); + int dataLength = data.length(); + + if (dataLength < max_length){ + str.append(data); + + int space = max_length - dataLength; + + str.append(" ".repeat(space)); + + return str.toString(); + } else { + return data; + } + + } + + /** + * ɾַĩβĿո + * ַΪջַ򷵻ԭַ + * + * @param data ԭʼַ + * @return ɾĩβոַ + */ + public String deleteSpaceToString(String data){ + // жǷΪ + if (data == null || data.isEmpty()){ + return data; + } + + int length = data.length(); + + while (length > 0 && data.charAt(length - 1) == ' '){ + length--; + } + + return data.substring(0, length); + } + + /** + * ӡϢ⡣ + * ÷ӡһбıʾ̼Ϣ + */ + public void printInfoTitle(){ + System.out.println("| ̼ID\t| ̼\t\t\t| ̼\t\t| ̼ҵַ\t\t\t\t| ̼ҽ\t\t\t\t\t| ͷ\t| ͷ\t|"); + } + + /** + * ȡ׼ַ + * ÷ûлȡһַɾĩβĿո + * ַȳָ󳤶ȣ򷵻null + * + * @param max_length ָ󳤶 + * @return ׼ַַȳָ󳤶ȣ򷵻null + */ + public String getStandString(int max_length){ + StringBuilder str = new StringBuilder(); + Scanner sca = new Scanner(System.in); + + String input = sca.nextLine(); + int inputLength = input.length(); + + // ׷ӿո + if (inputLength < max_length){ + str.append(input); + int space = max_length - inputLength; + str.append(" ".repeat(space)); + return str.toString(); + } + if (inputLength == max_length){ + return input; + } + return null; + } + + /** + * ʾûس + */ + public void pressEnterToContinue(){ + System.out.println("+ س..."); + sca.nextLine(); + } } diff --git a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/dao/AdminDao.java b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/dao/AdminDao.java index 8673eca..393351a 100644 --- a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/dao/AdminDao.java +++ b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/dao/AdminDao.java @@ -1,3 +1,8 @@ +/* + * Date: 2024-06-18 + * Design By JRNitre + * */ + package dao; import po.Admin; @@ -6,10 +11,8 @@ import java.sql.ResultSet; public interface AdminDao { Admin getAdminByNameByPass(String adminName, String password); - - void updateAdminPassword(Admin admin); - + Admin getAdminByIdByPass(int adminId, String password); + boolean updateAdminPassword(Admin admin); ResultSet fuzzyQueryExampleForName(String name); - ResultSet getAllAdmin(); } diff --git a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/dao/AdminDaoImpl.java b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/dao/AdminDaoImpl.java index 53148b1..d9b0834 100644 --- a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/dao/AdminDaoImpl.java +++ b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/dao/AdminDaoImpl.java @@ -1,30 +1,27 @@ +/* + * Date: 2024-06-18 + * Design By JRNitre + * */ + package dao; -import View.Display; import jdbc.JDBC; import po.Admin; import java.sql.*; public class AdminDaoImpl implements AdminDao{ -// @Override -// public Admin getAdminByNameByPass(String adminName, String password){ -// Map map = AdminMap.ADMIN_MAP;//ȡйԱ -// Collection values = map.values();//ȡmapֵļ -// -// //ֵļϣжǷƺƥ䣬Уʾ¼ɹ -// Admin admin = null; -// for(Admin tmp : values ) { -// if(tmp.getAdminName().equals(adminName) && tmp.getAdminPassworld().equals(password)) { -// admin = tmp; -// break; -// } -// } -// -// return admin; -// } - // ݿѯָû + /** + * ͨûȡԱϢ + * ˷ͨṩûݿмԱ˻Ϣ + * ҵƥĹԱһԱϸϢAdmin + * ûҵƥݿ򷵻null + * + * @param adminName Աû + * @param password Ա롣 + * @return ƥĹԱûҵΪnull + */ @Override public Admin getAdminByNameByPass(String adminName, String password) { Admin admin = null; @@ -55,27 +52,95 @@ public class AdminDaoImpl implements AdminDao{ return admin; } + /** + * ͨԱIDȡԱϢ + * ˷ͨṩĹԱIDݿмԱ˻Ϣ + * ҵƥĹԱһԱϸϢAdmin + * ûҵƥݿ򷵻null + * + * @param adminId ԱID + * @param password Ա롣 + * @return ƥĹԱûҵΪnull + */ @Override - public void updateAdminPassword(Admin admin) { - // TODO ¹ + public Admin getAdminByIdByPass(int adminId, String password) { + Admin admin = null; + try { + // ݿ + Connection conn = JDBC.getConnection(); + Statement stmt = conn.createStatement(); + + String sql = "SELECT * FROM admin WHERE adminId = '" + adminId + "' AND adminPassword = '" + password + "'"; + + ResultSet rs = stmt.executeQuery(sql); + + if (rs.next()) { + admin = new Admin(); + admin.setAdminId(rs.getInt("adminId")); + admin.setAdminName(rs.getString("adminName")); + admin.setAdminPassword(rs.getString("adminPassword")); + } + + conn.close(); + stmt.close(); + rs.close(); + } catch (SQLException e) { + System.err.println("Database error occurred: " + e.getMessage()); + } catch (Exception e) { + System.err.println("Unexpected error: " + e.getMessage()); + } + return admin; + } + + /** + * ¹Ա롣 + * ˷Ը¹Ա롣 + * ³ɹ򷵻true + * ʧܻݿ򷵻false + * + * @param admin Ա󣬰ҪµĹԱϢ + * @return ³ɹtrue򷵻false + */ + @Override + public boolean updateAdminPassword(Admin admin) { try { Connection conn = JDBC.getConnection(); Statement stmt = conn.createStatement(); System.out.println("+ ӵݿ"); - String sql = "UPDATE admin SET adminPassword = ?"; + String sql = "UPDATE admin SET adminPassword = ? WHERE adminId = ?"; PreparedStatement pst = conn.prepareStatement(sql); - pst.setString(1, admin.getAdminPassword()); - pst.executeUpdate(); - System.out.println("+ "); + + // ҪµûId + int adminIdToUpdate = admin.getAdminId(); + // Ҫ³ɵ + String newAdminPassword = admin.getAdminPassword(); + + pst.setString(1, newAdminPassword); + pst.setInt(2, adminIdToUpdate); + + int rowsAffected = pst.executeUpdate(); stmt.close(); conn.close(); + pst.close(); + + return rowsAffected > 0; } catch (Exception e) { System.err.println("Unexpected error: " + e.getMessage()); } + return false; } + /** + * ͨģѯȡйԱϢ + * ˷ͨģѯݿмйԱϢ + * ҵƥĹԱһԱϸϢResultSet + * ûҵƥݿ򷵻null + * + * @param name Աơ + * @return ƥĹԱûҵΪnull + */ @Override public ResultSet fuzzyQueryExampleForName(String name) { try{ @@ -86,9 +151,7 @@ public class AdminDaoImpl implements AdminDao{ PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, name + "%"); - ResultSet rs = pstmt.executeQuery(); - - return rs; + return pstmt.executeQuery(); } catch (Exception e){ e.printStackTrace(); @@ -96,6 +159,14 @@ public class AdminDaoImpl implements AdminDao{ return null; } + /** + * ȡйԱϢ + * ˷ԴݿмйԱϢ + * ҵƥĹԱһԱϸϢResultSet + * ûҵƥݿ򷵻null + * + * @return йԱûҵΪnull + */ @Override public ResultSet getAllAdmin() { try{ @@ -104,9 +175,7 @@ public class AdminDaoImpl implements AdminDao{ String sql = "SELECT * FROM admin"; - ResultSet rs = stmt.executeQuery(sql); - - return rs; + return stmt.executeQuery(sql); } catch (Exception e){ e.printStackTrace(); } diff --git a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/dao/BusinessDao.java b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/dao/BusinessDao.java index 299969a..7cfc27d 100644 --- a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/dao/BusinessDao.java +++ b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/dao/BusinessDao.java @@ -1,19 +1,29 @@ +/* + * Date: 2024-06-18 + * Design By JRNitre + * */ + package dao; import po.Business; +import po.Food; import java.sql.ResultSet; public interface BusinessDao { - void addNewBusiness(Business business); - - public int getBusinessNumber(); - + boolean addNewBusiness(Business business); ResultSet printAllBusiness(); - Business getBusinessByNameByPass(String name, String password); - + Business getBusinessByIdByPass(Integer id, String password); void deleteId(int id); - void deleteName(String Name); + boolean updateBusinessInfo(Business business); + boolean updateBusinessPassword(Business business); + boolean addNewFood(Food food); + ResultSet getAllFoodInfo(); + boolean updateFoodInfo(Food food, Business business); + Food getFoodById(int foodId, int businessId); + boolean deleteFoodId(int foodId, Business business); + boolean deleteFoodName(String foodName, Business business); + ResultSet getBusinessFoodInfo(Business business); } diff --git a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/dao/BusinessDaoImpl.java b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/dao/BusinessDaoImpl.java index 286dc01..c776e3b 100644 --- a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/dao/BusinessDaoImpl.java +++ b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/dao/BusinessDaoImpl.java @@ -1,15 +1,27 @@ +/* + * Date: 2024-06-18 + * Design By JRNitre + * */ + package dao; import View.Display; import jdbc.JDBC; -import po.Admin; import po.Business; +import po.Food; import java.sql.*; public class BusinessDaoImpl implements BusinessDao{ + + /** + * µ̼Ϣݿ⡣ + * + * @param business ӵ̼Ҷ󣬰̼ҵϢ + * @return ɹ̼Ϣ򷵻true򷵻false + */ @Override - public void addNewBusiness(Business business){ + public boolean addNewBusiness(Business business){ try { Connection conn = JDBC.getConnection(); @@ -25,55 +37,46 @@ public class BusinessDaoImpl implements BusinessDao{ pstmt.setDouble(7, business.getBusinessDeliveryPrice()); pstmt.executeUpdate(); - System.out.println("+ ̼!"); - pstmt.close(); conn.close(); + return true; } catch (Exception e){ e.printStackTrace(); } + return false; } - // ӡԪ + /** + * ѯҵϢؽ + * ͨݿӣStatementִSQLѯ䣬 + * ԻȡҵϢĽڲз쳣ӡ쳣ջ١ + * + * @return ResultSet ҵϢĽ쳣null + */ @Override public ResultSet printAllBusiness() { try{ Connection conn = JDBC.getConnection(); Statement stmt = conn.createStatement(); - Display dis = new Display('+', '-', 30); String sql = "SELECT * FROM business"; - ResultSet rs = stmt.executeQuery(sql); - - return rs; + return stmt.executeQuery(sql); } catch (Exception e){ e.printStackTrace(); } return null; } - public int getBusinessNumber(){ - try{ - Connection conn = JDBC.getConnection(); - - String sql = "SELECT COUNT(*) FROM business"; - PreparedStatement prs = conn.prepareStatement(sql); - - ResultSet res = prs.executeQuery(); - - if (res.next()){ - return res.getInt(1); - } else { - return 0; - } - } catch (Exception e){ - e.printStackTrace(); - } - return -1; - } - + /** + * ƺȡ̼Ϣ + * ͨݿѯƥƺ̼ңϸϢ + * + * @param name ̼ + * @param password ̼ + * @return ƥɹBusiness󣬷򷵻null + */ @Override public Business getBusinessByNameByPass(String name, String password) { Business business = null; @@ -91,6 +94,10 @@ public class BusinessDaoImpl implements BusinessDao{ business.setBusinessId(rs.getInt("businessId")); business.setBusinessName(rs.getString("businessName")); business.setBusinessPassword(rs.getString("password")); + business.setBusinessExplain(rs.getString("businessExplain")); + business.setBusinessAddress(rs.getString("businessAddress")); + business.setBusinessStarPrice(rs.getDouble("starPrice")); + business.setBusinessDeliveryPrice(rs.getDouble("deliveryPrice")); } conn.close(); @@ -104,6 +111,78 @@ public class BusinessDaoImpl implements BusinessDao{ return business; } + /** + * IDȡ̼Ϣ + * ͨݿѯƥID̼ңϸϢ + * + * @param id ̼ID + * @param password ̼ + * @return ƥɹBusiness󣬷򷵻null + */ + @Override + public Business getBusinessByIdByPass(Integer id, String password) { + Business business = null; + try { + // ݿ + Connection conn = JDBC.getConnection(); + Statement stmt = conn.createStatement(); + + String sql = "SELECT * FROM business WHERE businessId = '" + id + "' AND password = '" + password + "'"; + + ResultSet rs = stmt.executeQuery(sql); + + if (rs.next()) { + business = new Business(); + business.setBusinessId(rs.getInt("businessId")); + business.setBusinessName(rs.getString("businessName")); + business.setBusinessPassword(rs.getString("password")); + business.setBusinessExplain(rs.getString("businessExplain")); + business.setBusinessAddress(rs.getString("businessAddress")); + business.setBusinessStarPrice(rs.getDouble("starPrice")); + business.setBusinessDeliveryPrice(rs.getDouble("deliveryPrice")); + } + + conn.close(); + stmt.close(); + rs.close(); + } catch (SQLException e) { + System.err.println("Database error occurred: " + e.getMessage()); + } catch (Exception e) { + System.err.println("Unexpected error: " + e.getMessage()); + } + return business; + } + + /** + * ȡָ̼ҵʳƷϢ + * ͨݿѯָ̼ҵʳƷϢؽ + * + * @param business ̼Ҷ + * @return ResultSet ָ̼ҵʳƷϢĽ쳣null + */ + @Override + public ResultSet getBusinessFoodInfo(Business business){ + try{ + Connection conn = JDBC.getConnection(); + + String sql = "SELECT * FROM food WHERE businessId = ?"; + + PreparedStatement pstmt = conn.prepareStatement(sql); + pstmt.setInt(1, business.getBusinessId()); + + return pstmt.executeQuery(); + } catch (Exception e){ + e.printStackTrace(); + } + return null; + } + + /** + * IDɾ̼ҡ + * ͨݿɾָID̼ҡ + * + * @param id ̼ID + */ @Override public void deleteId(int id) { try{ @@ -129,6 +208,12 @@ public class BusinessDaoImpl implements BusinessDao{ } } + /** + * ɾ̼ҡ + * ͨݿɾָƵ̼ҡ + * + * @param Name ̼ + */ @Override public void deleteName(String Name) { try{ @@ -153,4 +238,277 @@ public class BusinessDaoImpl implements BusinessDao{ e.printStackTrace(); } } + + /** + * ̼Ϣ + * ͨݿָID̼Ϣ + * + * @param business ̼Ҷ + * @return true ³ɹfalse ʧ + */ + @Override + public boolean updateBusinessInfo (Business business) { + try { + Connection conn = JDBC.getConnection(); + Statement stmt = conn.createStatement(); + + String sql = "UPDATE business SET businessName = ?, businessAddress = ?, businessExplain = ?, starPrice = ?, deliveryPrice = ? WHERE businessId = ?"; + PreparedStatement pst = conn.prepareStatement(sql); + + int businessIdToUpdate = business.getBusinessId(); + + pst.setString(1, business.getBusinessName()); + pst.setString(2, business.getBusinessAddress()); + pst.setString(3, business.getBusinessExplain()); + pst.setDouble(4, business.getBusinessStarPrice()); + pst.setDouble(5, business.getBusinessDeliveryPrice()); + pst.setInt(6, businessIdToUpdate); + + int rowsAffected = pst.executeUpdate(); + + stmt.close(); + conn.close(); + pst.close(); + + return rowsAffected > 0; + } catch ( Exception e ){ + e.printStackTrace(); + } + return false; + } + + /** + * ̼롣 + * ͨݿָID̼롣 + * + * @param business ̼Ҷ + * @return true ³ɹfalse ʧ + */ + @Override + public boolean updateBusinessPassword (Business business) { + try { + Connection conn = JDBC.getConnection(); + Statement stmt = conn.createStatement(); + System.out.println("+ ӵݿ"); + + String sql = "UPDATE business SET password = ? WHERE businessId = ?"; + PreparedStatement pst = conn.prepareStatement(sql); + + // ҪµûId + int newBusinessIdToUpdate = business.getBusinessId(); + // Ҫ³ɵ + String newBusinessPassword = business.getBusinessPassword(); + + pst.setString(1, newBusinessPassword); + pst.setInt(2, newBusinessIdToUpdate); + + int rowsAffected = pst.executeUpdate(); + + stmt.close(); + conn.close(); + pst.close(); + + return rowsAffected > 0; + } catch (Exception e) { + System.err.println("Unexpected error: " + e.getMessage()); + } + return false; + } + + /** + * µʳƷ + * ͨݿµʳƷ + * + * @param food ʳƷ + * @return true ӳɹfalse ʧ + */ + @Override + public boolean addNewFood (Food food) { + try { + Connection conn = JDBC.getConnection(); + + String sql = "INSERT INTO food (foodId, foodName, foodExplain, foodPrice, businessId) VALUES (?, ?, ?, ?, ?)"; + PreparedStatement pstmt = conn.prepareStatement(sql); + + pstmt.setInt(1, food.getFoodId()); + pstmt.setString(2, food.getFoodName()); + pstmt.setString(3, food.getFoodExplain()); + pstmt.setDouble(4, food.getFoodPrice()); + pstmt.setDouble(5, food.getFoodId()); + + pstmt.executeUpdate(); + pstmt.close(); + conn.close(); + + return true; + } catch (Exception e){ + e.printStackTrace(); + } + return false; + } + + /** + * ȡʳƷϢ + * ͨݿȡʳƷϢ + * + * @return ResultSet ʳƷϢ + */ + @Override + public ResultSet getAllFoodInfo () { + try{ + Connection conn = JDBC.getConnection(); + Statement stmt = conn.createStatement(); + + String sql = "SELECT * FROM food"; + + return stmt.executeQuery(sql); + } catch (Exception e){ + e.printStackTrace(); + } + return null; + } + + /** + * ʳƷϢ + * ͨݿָIDʳƷϢ + * + * @param food ʳƷ + * @param business ̼Ҷ + * @return true ³ɹfalse ʧ + */ + @Override + public boolean updateFoodInfo (Food food, Business business) { + try { + Connection conn = JDBC.getConnection(); + + // 3. ׼SQL + String sql = "UPDATE food SET foodName = ?, foodExplain = ?, foodPrice = ? WHERE businessId = ? AND foodId = ?"; + PreparedStatement pstmt = conn.prepareStatement(sql); + pstmt.setString(1, food.getFoodName()); + pstmt.setString(2, food.getFoodExplain()); + pstmt.setDouble(3, food.getFoodPrice()); + pstmt.setInt(4, food.getBusinessId()); + pstmt.setInt(5, food.getFoodId()); + + int rowsAffected = pstmt.executeUpdate(); + + pstmt.close(); + conn.close(); + + return rowsAffected > 0; + + + } catch ( Exception e ) { + e.printStackTrace(); + } + return false; + } + + /** + * ʳƷID̼IDȡʳƷϢ + * ͨѯݿȡضIDʳƷϸϢ + * + * @param foodId ʳƷΨһʶID + * @param businessId ̼ҵΨһʶIDȷʳƷĸ̼ҡ + * @return һFood󣬰ѯʳƷϢûҵƥʳƷ򷵻null + */ + @Override + public Food getFoodById (int foodId, int businessId) { + Food food = null; + + try { + // ݿ + Connection conn = JDBC.getConnection(); + Statement stmt = conn.createStatement(); + + String sql = "SELECT * FROM food WHERE foodId = '" + foodId + "' AND businessId = '" + businessId + "'"; + + ResultSet rs = stmt.executeQuery(sql); + + if (rs.next()) { + food = new Food(); + food.setFoodName(rs.getString("foodName")); + food.setFoodExplain(rs.getString("foodExplain")); + food.setFoodPrice(rs.getDouble("foodPrice")); + food.setFoodId(rs.getInt("foodId")); + food.setBusinessId(rs.getInt("businessId")); + } + + conn.close(); + stmt.close(); + rs.close(); + + return food; + } catch (SQLException e) { + System.err.println("Database error occurred: " + e.getMessage()); + } catch (Exception e) { + System.err.println("Unexpected error: " + e.getMessage()); + } + + return food; + } + + /** + * ʳƷID̼IDɾʳƷ + * ͨɾݿеضʳƷ¼ɾʳƷ + * + * @param foodId ʳƷΨһʶID + * @param business ̼ҵΨһʶIDȷʳƷĸ̼ҡ + * @return ɾɹ򷵻true򷵻false + */ + @Override + public boolean deleteFoodId (int foodId, Business business) { + try{ + Connection conn = JDBC.getConnection(); + + String sql = "DELETE FROM food WHERE businessId = ? AND foodId = ?"; + PreparedStatement prep = conn.prepareStatement(sql); + + prep.setInt(1, business.getBusinessId()); + prep.setInt(2, foodId); + + int rowsAffected = prep.executeUpdate(); + + prep.close(); + conn.close(); + + return rowsAffected > 0; + + } catch (Exception e){ + e.printStackTrace(); + } + return false; + } + + /** + * ʳƷƺ̼IDɾʳƷ + * ͨɾݿеضʳƷ¼ɾʳƷ + * + * @param foodName ʳƷơ + * @param business ̼ҵΨһʶIDȷʳƷĸ̼ҡ + * @return ɾɹ򷵻true򷵻false + */ + @Override + public boolean deleteFoodName (String foodName, Business business) { + try{ + Connection conn = JDBC.getConnection(); + + String sql = "DELETE FROM food WHERE businessId = ? AND foodName = ?"; + PreparedStatement prep = conn.prepareStatement(sql); + + prep.setInt(1, business.getBusinessId()); + prep.setString(2, foodName); + + int rowsAffected = prep.executeUpdate(); + + prep.close(); + conn.close(); + + return rowsAffected > 0; + + } catch (Exception e){ + e.printStackTrace(); + } + return false; + } } diff --git a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/jdbc/JDBC.java b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/jdbc/JDBC.java index 001d6e0..af7e017 100644 --- a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/jdbc/JDBC.java +++ b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/jdbc/JDBC.java @@ -1,16 +1,26 @@ +/* + * Date: 2024-06-18 + * Design By JRNitre + * */ + package jdbc; -import po.Admin; - -import java.beans.Expression; import java.sql.*; public class JDBC { - + // Ա private static final String URL = "jdbc:mysql://124.220.110.170:3306/jrnitre"; private static final String user = "jrnitre"; private static final String Password = "npgt1365"; + /** + * ȡݿӡ + * ԼMySQL JDBCݿӡʧܻ޷ + * ӡ쳣ջٲ׳һʱ쳣ָʾݿʧܡ + * + * @return Connection ݿ⽨ӡ + * @throws RuntimeException ʧܻ޷ӣ׳쳣 + */ public static Connection getConnection() { try { Class.forName("com.mysql.cj.jdbc.Driver"); diff --git a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/po/Admin.java b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/po/Admin.java index d3529ea..1d5e2af 100644 --- a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/po/Admin.java +++ b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/po/Admin.java @@ -1,13 +1,18 @@ +/* + * Date: 2024-06-18 + * Design By JRNitre + * */ + package po; public class Admin { - // 管理员 ID - private Integer adminId; - // 管理员名 - private String adminName; - // 管理员密码 - private String adminPassword; + // 成员变量 + private Integer adminId; // 管理员 ID + private String adminName; // 管理员名 + private String adminPassword; // 管理员密码 + // 有参构造方法 + // 初始化管理员 public Admin (Integer id, String name, String passworld){ super(); adminId = id; @@ -15,10 +20,19 @@ public class Admin { adminPassword = passworld; } + // 无参构造方法 public Admin (){ super(); } + // 拷贝构造方法 + public Admin (Admin cpAdmin){ + adminId = cpAdmin.adminId; + adminName = cpAdmin.adminName; + adminPassword = cpAdmin.adminPassword; + } + + // getter public Integer getAdminId(){ return adminId; } @@ -31,6 +45,7 @@ public class Admin { return adminPassword; } + // setter public void setAdminId(Integer id){ adminId = id; } diff --git a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/po/Business.java b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/po/Business.java index 2befae8..97e26a1 100644 --- a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/po/Business.java +++ b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/po/Business.java @@ -1,14 +1,36 @@ +/* + * Date: 2024-06-18 + * Design By JRNitre + * */ + package po; public class Business { - private Integer businessId; - private String businessName; - private String businessPassword; - private String businessAddress; - private String businessExplain; - private double businessStarPrice; - private double businessDeliveryPrice; + // 成员变量 + private Integer businessId; // 商家 ID + private String businessName; // 商家名称 + private String businessPassword; // 商家密码 + private String businessAddress; // 商家地址 + private String businessExplain; // 商家介绍 + private double businessStarPrice; // 商家 起送费 + private double businessDeliveryPrice; // 商家 配送费 + // 拷贝构造方法 + public Business (Business cpBusiness){ + this.businessId = cpBusiness.getBusinessId(); + this.businessName = cpBusiness.getBusinessName(); + this.businessPassword = cpBusiness.getBusinessPassword(); + this.businessAddress = cpBusiness.getBusinessAddress(); + this.businessExplain = cpBusiness.getBusinessExplain(); + this.businessStarPrice = cpBusiness.getBusinessStarPrice(); + this.businessDeliveryPrice = cpBusiness.getBusinessDeliveryPrice(); + } + + // 无参构造方法 + public Business () { super(); } + + + // setter public void setBusinessId(Integer id){ businessId = id; } @@ -37,6 +59,7 @@ public class Business { businessDeliveryPrice = deliveryPrice; } + // getter public Integer getBusinessId(){ return businessId; } @@ -65,6 +88,7 @@ public class Business { return businessDeliveryPrice; } + // TODO tototototoString !!! @Override public String toString() { return super.toString(); diff --git a/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/po/Food.java b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/po/Food.java new file mode 100644 index 0000000..8efd12e --- /dev/null +++ b/02_Intellij_Java_CourseExperiment_20240611/CourseExperiment/src/po/Food.java @@ -0,0 +1,49 @@ +/* + * Date: 2024-06-18 + * Design By JRNitre + * */ + +package po; + +public class Food { + // 成员变量 + private int foodId; // 食品编号 + private String foodName; // 食品名称 + private String foodExplain; // 食品说明 + private double foodPrice; // 食品价格 + private int businessId; // 所属商家编号 + + // setter + public void setFoodId(int id){ + this.foodId = id; + } + public void setFoodName(String name){ + this.foodName = name; + } + public void setFoodExplain(String explain){ + this.foodExplain = explain; + } + public void setFoodPrice(double price){ + this.foodPrice = price; + } + public void setBusinessId(int id){ + this.businessId = id; + } + + // getter + public int getFoodId(){ + return this.foodId; + } + public String getFoodName(){ + return this.foodName; + } + public String getFoodExplain(){ + return this.foodExplain; + } + public double getFoodPrice(){ + return this.foodPrice; + } + public int getBusinessId(){ + return this.businessId; + } +} \ No newline at end of file