Kbaor_2023_9_28_Java第一次实战项目_ELM_V1_食品的实体类、工具类与实现类
Kbaor_2023_9_28_Java第一次实战项目_ELM_V1_食品的实体类、工具类与实现类
ELM_V1_食品的实体类
package elm_V1; /** * [食品实体类] * * @author 秦帅 * @date 2023-9-25 */ public class Food { private Integer foodId; // 食品编号 private String foodName; //食品名称 private String foodExplain; // 食品介绍 private Double foodPrice; // 食品价格 private Integer businessId; // 所属商家编号 @Override public String toString() { return "\n食品编号:" + this.foodId + "\n食品名称:" + this.foodName + "\n食品介绍:" + this.foodExplain + "\n食品价格:" + this.foodPrice + "\n所属商家:" + this.businessId; } public Integer getFoodId() { return foodId; } public void setFoodId(Integer foodId) { this.foodId = foodId; } public String getFoodName() { return foodName; } public void setFoodName(String foodName) { this.foodName = foodName; } public String getFoodExplain() { return foodExplain; } public void setFoodExplain(String foodExplain) { this.foodExplain = foodExplain; } public Double getFoodPrice() { return foodPrice; } public void setFoodPrice(Double foodPrice) { this.foodPrice = foodPrice; } public Integer getBusinessId() { return businessId; } public void setBusinessId(Integer businessId) { this.businessId = businessId; } }
ELM_V1_食品的工具类
package elm_V1; import java.io.*; import java.util.ArrayList; import java.util.List; /** * [食品实体类] * * @author 秦帅 * @date 2023-9-25 */ public class FoodGJ { // 字符流把集合数据写入文件 public static void listToFile(List list) throws IOException { // 确认文件是utf-8编码 File file = new File("G:\\Idea_hn_ws\\DB\\Food\\Food.txt"); FileWriter fileWriter = null; // 第一个参数是为是否append BufferedWriter bw = null; try { // 第二个参数是表示是否向文件中追加内容 true==追加 false==覆盖 fileWriter = new FileWriter(file, false); bw = new BufferedWriter(fileWriter); // 传统的遍历方式 for (int i = 0; i < list.size(); i++) { Food food = (Food) list.get(i); StringBuilder sbr = new StringBuilder(); sbr.append(food.getFoodId() + " "); sbr.append(food.getFoodName() + " "); sbr.append(food.getFoodExplain() + " "); sbr.append(food.getFoodPrice() + " "); sbr.append(food.getBusinessId() + " "); //容错判断 if(sbr.toString().length()>=7) { bw.write(sbr.toString()); bw.newLine();// 换行 } } } catch (IOException e) { e.printStackTrace(); } finally { // 记着关闭流 // 如果调用了关闭流的方法,就不用手动调bw.flush() bw.close(); fileWriter.close(); } } // 字符流读文件数据到集合中 public static List<Food> fileToList() throws IOException { List<Food> list = new ArrayList<Food>(); File file = new File("G:\\Idea_hn_ws\\DB\\Food\\Food.txt"); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); while (true) { String str = br.readLine();// 一次读取一行文本 if (str != null) { // System.out.println(str); String[] arr = str.split(" "); Food food = new Food(); food.setFoodId(Integer.parseInt(arr[0])); food.setFoodName(arr[1]); food.setFoodExplain(arr[2]); food.setFoodPrice(Double.valueOf(arr[3])); food.setBusinessId(Integer.valueOf(arr[4])); list.add(food); } else { break; } } return list; } // 根据商家ID找食品 public static List<Food> fileToListfindfood(int value) throws IOException { List<Food> list = new ArrayList<Food>(); File file = new File("G:\\Idea_hn_ws\\DB\\Food\\Food.txt"); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); while (true) { String str = br.readLine();// 一次读取一行文本 if (str != null) { // System.out.println(str); String[] arr = str.split(" "); Food food = new Food(); food.setFoodId(Integer.parseInt(arr[0])); food.setFoodName(arr[1]); food.setFoodExplain(arr[2]); food.setFoodPrice(Double.valueOf(arr[3])); food.setBusinessId(Integer.valueOf(arr[4])); if (food.getBusinessId() == value) { list.add(food); } } else { break; } } return list; } // 根据食品ID删除 public static List<Food> fileToListfindfoodbySid(int value) throws IOException { List<Food> list = new ArrayList<Food>(); File file = new File("G:\\Idea_hn_ws\\DB\\Food\\Food.txt"); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); while (true) { String str = br.readLine();// 一次读取一行文本 if (str != null) { // System.out.println(str); String[] arr = str.split(" "); Food food = new Food(); food.setFoodId(Integer.parseInt(arr[0])); food.setFoodName(arr[1]); food.setFoodExplain(arr[2]); food.setFoodPrice(Double.valueOf(arr[3])); food.setBusinessId(Integer.valueOf(arr[4])); if (food.getFoodId() == value) { list.add(food); } } else { break; } } return list; } //获取行 public static int getFileRow() throws IOException { int sum = 0, bid = 0; List<Food> list = fileToList(); if (list.size() == 0) return sum; // 传统的遍历方式 for (int i = 0; i < list.size(); i++) { Food food = (Food) list.get(i); bid = food.getFoodId(); } return bid; } }
ELM_V1_食品的实现类
package elm_V1; import java.io.IOException; import java.util.List; import java.util.Scanner; import static elm_V1.FoodGJ.fileToList; import static elm_V1.FoodGJ.listToFile; import static elm_V1.MenuServiceImp.menu01; import static elm_V1.MenuServiceImp.menu02; /** * [食品业务类] * * @author 秦帅 * @date 2023-9-26 */ public class FoodServiceImp { static BusinessServiceImp bus = new BusinessServiceImp(); static int value = bus.chuancan(); //显示本商户的所有食品菜单 public void showAllFood() throws IOException { List<Food> list = FoodGJ.fileToListfindfood(value); // System.out.println("请输入您的商户ID..."); Scanner sc = new Scanner(System.in); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } // String str = sc.nextLine(); //ID转整形 // int id = Integer.parseInt(str); // Food food = (Food) list.get(value - 1); // System.out.println("您的商户ID为:" + bus.getBusinessId()); // System.out.println("您的商户登录密码为:" + bus.getPassword()); // System.out.println("您的商户地址为:" + bus.getBusinessAddress()); // System.out.println("您的商户描述为:" + bus.getBusinessExplain()); // System.out.println("您的商户起送费为:" + bus.getStarPrice()); // System.out.println("您的商户配送费为:" + bus.getDeliveryPrice()); // System.out.println("按回车键后回到商户管理菜单....."); // 暂停程序 String str1 = sc.nextLine(); menu02(); } //添加菜品 public void saveFood() throws IOException { int num = FoodGJ.getFileRow() + 1; //菜品编号 Scanner sc = new Scanner(System.in); System.out.println("请按如下格式输入添加的菜品信息 空格间隔"); System.out.println("商家ID 食品名称 食品介绍 食品价格"); String str = sc.nextLine(); String[] str_arr = str.split(" "); while (BusinessGJ.isOver(str_arr[1], 50)) { // 50 System.out.println("请重新输入食品名称 长度<=50:"); // 50 str_arr[1] = sc.next(); } while (BusinessGJ.isOver(str_arr[2], 40)) { // 40 System.out.println("请重新输入食品介绍 长度<=40:"); // 40 str_arr[2] = sc.next(); } while (!BusinessGJ.isDouble(str_arr[3])) { System.out.println("请重新输入食品价格:"); str_arr[3] = sc.next(); } Food food = new Food(); food.setFoodId(num); food.setFoodName(str_arr[1]); food.setFoodExplain(str_arr[2]); food.setFoodPrice(Double.valueOf(str_arr[3])); food.setBusinessId(Integer.valueOf(str_arr[0])); List<Food> list = fileToList(); list.add(food); listToFile(list); System.out.println("菜品添加成功!"); // 暂停程序 String str2 = sc.nextLine(); menu02(); } //修改菜品 public void updateFood() throws IOException { List<Food> list = FoodGJ.fileToList(); // System.out.println("请输入您的商户ID..."); Scanner sc = new Scanner(System.in); // String str = sc.nextLine();食品名称 食品介绍 食品价格 System.out.println("请选择您要修改的菜品信息..."); System.out.println("请输入相应的数字,在0-3之间选择..."); System.out.println("1.食品名称"); System.out.println("2.食品介绍"); System.out.println("3.食品价格"); System.out.println("0.返回上一级菜单"); Scanner sc1 = new Scanner(System.in); String str1 = sc1.nextLine(); Food food = (Food) list.get(value - 1); int ca = Integer.parseInt(str1); switch (ca) { case 1: System.out.println("请输入您修改后的食品名称..."); String smc = sc.next(); food.setFoodName(smc); FoodGJ.listToFile(list); System.out.println("食品名称修改成功!"); break; case 2: System.out.println("请输入您修改后的食品介绍..."); String sjs = sc.next(); food.setFoodExplain(sjs); FoodGJ.listToFile(list); System.out.println("食品介绍修改成功!"); break; case 3: System.out.println("请输入您修改后的食品价格..."); String spr = sc.next(); food.setFoodPrice(Double.valueOf(spr)); FoodGJ.listToFile(list); System.out.println("食品价格修改成功!"); break; case 0: System.out.println("按回车键后回到商户管理菜单....."); menu02(); break; default: System.out.println("请选择相应的功能! 在0-5之间选择。"); } // 暂停程序 String str3 = sc.nextLine(); menu02(); } //删除菜品 public void removeFood() throws IOException { List<Food> list = FoodGJ.fileToList(); Scanner sc = new Scanner(System.in); System.out.println("请输入您要删除的菜品ID..."); String str6 = sc.nextLine(); int id = Integer.parseInt(str6); System.out.println("您确定要删除该菜品吗?(Y/N)"); String str = sc.nextLine(); if (str.equals("Y") || str.equals("y")) { Food bus = (Food) list.get(id - 1); list.remove(bus.getFoodId() - 1); FoodGJ.listToFile(list); System.out.println("您的菜品注销成功!"); System.out.println("即将返回主菜单!"); String str5 = sc.nextLine(); menu02(); } else { System.out.println("按回车键后回到商户管理菜单..."); // 暂停程序 String str5 = sc.nextLine(); menu02(); } // Scanner sc = new Scanner(System.in); // System.out.println("请输入您要删除的菜品ID..."); // String str6 = sc.nextLine(); // int id = Integer.parseInt(str6); // System.out.println("您确定要删除该菜品吗?(Y/N)"); // String str = sc.nextLine(); // List<Food> list = FoodGJ.fileToListfindfoodbySid(id); // if (str.equals("Y") || str.equals("y")) { // for (String item : list) { // if (item.equals("3")) { // System.out.println(item); // list.remove(item); // } // } // FoodGJ.listToFile(list); // System.out.println("您的菜品删除成功!"); // System.out.println("即将返回商户管理菜单!"); // String str5 = sc.nextLine(); // menu02(); // } else { // System.out.println("按回车键后回到商户管理菜单..."); // // 暂停程序 // String str5 = sc.nextLine(); // menu02(); // } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本