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();
// } } }

Kbaor_2023_9_28_Java第一次实战项目_ELM_V1_食品的实体类、工具类与实现类的更多相关文章

  1. .NET Core实战项目之CMS 第十一章 开发篇-数据库生成及实体代码生成器开发

    上篇给大家从零开始搭建了一个我们的ASP.NET Core CMS系统的开发框架,具体为什么那样设计我也已经在第十篇文章中进行了说明.不过文章发布后很多人都说了这样的分层不是很合理,什么数据库实体应该 ...

  2. .NET Core实战项目之CMS 第二章 入门篇-快速入门ASP.NET Core看这篇就够了

    作者:依乐祝 原文链接:https://www.cnblogs.com/yilezhu/p/9985451.html 本来这篇只是想简单介绍下ASP.NET Core MVC项目的(毕竟要照顾到很多新 ...

  3. 前后端分离之vue2.0+webpack2 实战项目 -- webpack介绍

    webpack的一点介绍 Webpack 把任何一个文件都看成一个模块,模块间可以互相依赖(require or import),webpack 的功能是把相互依赖的文件打包在一起.webpack 本 ...

  4. .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划

    作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9977862.html 写在前面 千呼万唤始出来,首先,请允许我长吸一口气!真没想到一份来自28岁老程序员 ...

  5. .NET Core实战项目之CMS 第五章 入门篇-Dapper的快速入门看这篇就够了

    写在前面 上篇文章我们讲了如在在实际项目开发中使用Git来进行代码的版本控制,当然介绍的都是比较常用的功能.今天我再带着大家一起熟悉下一个ORM框架Dapper,实例代码的演示编写完成后我会通过Git ...

  6. .NET Core实战项目之CMS 第八章 设计篇-内容管理极简设计全过程

    写在前面 上一篇文章中我带着大家进行了权限部分的极简设计,也仅仅是一个基本的权限设计.不过你完全可以基于这套权限系统设计你的更复杂的权限系统,当然更复杂的权限系统要根据你的业务来进行,因为任何脱离实际 ...

  7. .NET Core实战项目之CMS 第九章 设计篇-白话架构设计

    前面两篇文章给大家介绍了我们实战的CMS系统的数据库设计,源码也已经上传到服务器上了.今天我们就好聊聊架构设计,在开始之前先给大家分享一下这几天我一直在听的<从零开始学架构>里面关于架构设 ...

  8. .NET Core实战项目之CMS 第十章 设计篇-系统开发框架设计

    这两天比较忙,周末也在加班,所以更新的就慢了一点,不过没关系,今天我们就进行千呼万唤的系统开发框架的设计.不知道上篇关于架构设计的文章大家有没有阅读,如果阅读后相信一定对架构设计有了更近一部的理解,如 ...

  9. .NET Core实战项目之CMS 第十二章 开发篇-Dapper封装CURD及仓储代码生成器实现

    本篇我将带着大家一起来对Dapper进行下封装并实现基本的增删改查.分页操作的同步异步方法的实现(已实现MSSQL,MySql,PgSQL).同时我们再实现一下仓储层的代码生成器,这样的话,我们只需要 ...

  10. .NET Core实战项目之CMS 第十三章 开发篇-在MVC项目结构介绍及应用第三方UI

    作为后端开发的我来说,前端表示真心玩不转,你如果让我微调一个位置的样式的话还行,但是让我写一个很漂亮的后台的话,真心做不到,所以我一般会选择套用一些开源UI模板来进行系统UI的设计.那如何套用呢?今天 ...

随机推荐

  1. Python3.7源码编译

    1.下载Python3.7.0源码 git clone https://github.com/python/cpython.gitgit checkout v3.7.0 wget https://ww ...

  2. Visual Studio2019如何添加引用

    ​ 同一解决方案中添加引用 比如我们想在Test项目中添加Queue项目的引用 1.鼠标右击引用-->添加引用 2."引用管理器"-->项目-->解决方案--&g ...

  3. APP中Web容器的核心实现

      现在的业务型APP中,采用纯原生开发策略的已经很少了,大部分都使用的混合开发.如原生,H5,ReactNative,Flutter,Weex它们之间任意的组合就构成了混合开发. 其中原生+H5是出 ...

  4. Lifecycle解决了什么问题,以及它的基本用法

    1.为何要引入Lifecycle? 我首先来举个大家都比较常见的例子:我们在android开发的时候,经常需要在页面的onCreate()方法中对组件进行初始化,在onPause()方法中停止组件,而 ...

  5. LAL v0.36.7发布,Customize Sub,我有的都给你

    Go语言流媒体开源项目 LAL 今天发布了v0.36.7版本. LAL 项目地址:https://github.com/q191201771/lal 老规矩,简单介绍一下: ▦ Customize S ...

  6. 进程相关API

    ID与句柄 如果我们成功创建一个进程,CreateProcess函数会给我们返回一个结构体,包括四个数 据:进程编号(ID).进程句柄.线程编号(ID).线程句柄. 进程ID其实我们早见过了,通常我们 ...

  7. 万字长文浅析配置对MySQL服务器的影响

    有很多的服务器选项会影响这MySQL服务器的性能,比如内存中临时表的大小.排序缓冲区等.有些针对特定存储引擎(如InnoDB)的选项,也会对查询优化很有用. 调整服务器的配置从某种程度来说是一个影响全 ...

  8. 2021-7-29 MySql多表查询详解

    多表连接 左连接:返回第一张表的所有数据项然后拼接第二张表(左表全有,右表对应左表才有) 右连接:返回第二张表的所有数据项然后拼接第一张表(右表全有,左表对应右表才有) 内连接:返回两张表数据相等的数 ...

  9. 【go语言】3.1.2 接口的定义和实现

    在 Go 中,接口是一种抽象类型,用来描述其他类型应该有哪些方法.它定义了一组方法,但没有实现.这些方法由其他类型实现. 接口的定义 接口定义的格式如下: type InterfaceName int ...

  10. asp.net core之路由

    在 ASP.NET Core 中,路由是一个非常重要的概念,它决定了如何将传入的请求映射到相应的处理程序.本文将详细介绍 ASP.NET Core 中的路由系统,包括路由的基本原理.路由模板.路由参数 ...