首页 > 编程语言 >Kbaor_2023_9_28_Java第一次实战项目_ELM_V1_食品的实体类、工具类与实现类

Kbaor_2023_9_28_Java第一次实战项目_ELM_V1_食品的实体类、工具类与实现类

时间:2023-09-28 16:15:58浏览次数:45  
标签:实体类 Java String ELM food list System println out

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

    }


}

标签:实体类,Java,String,ELM,food,list,System,println,out
From: https://www.cnblogs.com/Qinyyds/p/17735982.html

相关文章

  • JAVA导入工程遇见Could not transfer artifact io.rest-assured:rest-assured:pom:4.2
    问题:用idea导入已有的工程,操作File->InvalidateCaches/Restart后,点击右上角的Run,报以下异常:“Couldnottransferartifactio.rest-assured:rest-assured:pom:4.2.0”如下图所示:解决办法:因为Maven目录配置的问题,打开File->NewProjectsSettings->SettingsforNewProjects......
  • JavaScript 中的类型、值和变量
     JavaScript类型可以分为两类:原始类型和对象类型。JavaScript的基本类型包括数字、文本字符串(称为字符串)和布尔真值(称为布尔值)。特殊的JavaScript值null和undefined是原始值,但它们不是数字、字符串或布尔值。每个值通常被认为是其自身特殊类型的唯一成员。ES6添加了一种新......
  • java: Sorting Algorithms
     /***encoding:utf-8*版权所有2023©涂聚文有限公司*许可信息查看:https://www.geeksforgeeks.org/sorting-algorithms/*描述:https://www.geeksforgeeks.org/sorting-algorithms/*#Author:geovindu,GeovinDu涂聚文.**#IDE:IntelliJID......
  • 安装解压版activemq(版本太高,java不支持)
    1、上传压缩包apache-activemq-5.16.5-bin.tar.gz到/usr/local目录2、解压tar-xzvfapache-activemq-5.16.5-bin.tar.gz3、测试启动,进入/usr/local/apache-activemq-5.16.5/bin目录,启动./activemqstart4、测试访问activemq,访问http://localhost:8161/admin5、修改网页登......
  • 牛客网刷Java记录第一天
    第一题下列程序输出啥?publicclassStringDemo{privatestaticfinalStringMESSAGE="taobao";publicstaticvoidmain(String[]args){Stringa="tao"+"bao";Stringb="tao";Stringc="bao";Sys......
  • helm安装 ingress-nginx
    目录1.下载ingress-nginx-4.2.5.tgz2.解压,修改文件3.安装ingress4.测试网页5.windows测试helm3安装1.下载ingress-nginx-4.2.5.tgzhelmfetchingress-nginx/ingress-nginx--version4.2.5#或者curl-LOhttps://github.com/kubernetes/ingress-nginx/releases/download/helm-c......
  • helm3安装部署三、执行helm警告kube/config文件不安全问题
    目录一、安装篇二、配置仓库篇三、执行helm警告kube/config文件不安全问题四、helm自动补全命令五、安装、卸载软件HELM是k8的包管理工具,像linux系统的包管理器,如yum,apt等,很方便的把yaml文件部署到k8s上面!一、安装篇1.helm包下载地址:wgethttps://get.helm.sh/helm-v3.6.1-l......
  • 基于Java开发的企事业移动培训考学系统
    一、前言:随着移动技术的飞速发展,企事业培训考试系统也面临着不断的升级和改进。为了更好地满足用户的需求,本文将介绍一款企事业移动培训考学系统,并围绕该系统的功能、特点、应用场景等方面进行详细阐述。二、系统功能企事业移动培训考学系统具有丰富多样的功能,可以满足不同用......
  • JavaScript——小数精度丢失问题
    JavaScript小数进行数值运算时出现精度丢失问题1.原因:JavaScript的number类型在进行运算时都先将十进制转二进制,此时,小数点后面的数字转二进制时会出现无限循环的问题。为了避免这一个情况,要舍0进1,此时就会导致精度丢失问题。2.如何解决:(1)保留小数位数toFixed()constnumObj=......
  • java用Stream一行代码实现数据分组统计、排序、最大值、最小值、平均值、总数、合计
    getAverage():它返回所有接受值的平均值。getCount():它计算所有元素的总数。getMax():它返回最大值。getMin():它返回最小值。getSum():它返回所有元素的总和。示例:@GetMapping("/list")publicvoidlist(){List<InputForm>inputForms=inputFormMapper.se......