使用序列化技术保存数据 改进 IO流完成项目实战水果库存系统

  •  上一节内容是 使用IO流完成项目实战水果库存系统
  • https://blog.csdn.net/m0_65152767/article/details/133999972?spm=1001.2014.3001.5501
package com.csdn.fruit.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Fruit implements Serializable {
    private String fname;
    private Integer price;
    private Integer fcount;
    private String remark;
    @Override
    public String toString() {
        return fname + "\t\t" + price + "\t\t" + fcount + "\t\t" + remark;
    }
}
  • 只改动 加载数据  保存数据  代码的IO操作,改进成  序列化对象(ObjectOutputStream,ObjectInputStream)知识
  • 参看  https://blog.csdn.net/m0_65152767/article/details/133280223
package com.csdn.fruit.view;
import com.csdn.fruit.pojo.Fruit;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.function.Consumer;
import java.util.function.Predicate;
public class Menu {
    Scanner input = new Scanner(System.in);
    List fruitList = new ArrayList<>();
    {
        try {
            loadData();
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    //加载数据
    public void loadData() throws IOException, ClassNotFoundException {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("fruit.dat");
        /*if (inputStream != null) {
            //字节流转换成字符流
            //字节流:InputStream    字符流:Reader  字符转换流:InputStreamReader
            BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
            String str = null;
            while ((str = bf.readLine()) != null) {
                String[] arr = str.split(" ");
                String fname = arr[0];
                Integer price = Integer.parseInt(arr[1]);
                Integer fcount = Integer.parseInt(arr[2]);
                String remark = arr[3];

                Fruit fruit = new Fruit(fname, price, fcount, remark);

                fruitList.add(fruit);
        }
        }*/
        if (inputStream!=null) {
            ObjectInputStream ois = new ObjectInputStream(inputStream);
            fruitList = (List) ois.readObject();
        }
    }
    //显示主菜单
    public int showMainMenu() {
        System.out.println("================欢迎使用水果库存系统===================");
        System.out.println("1.显示库存列表");
        System.out.println("2.添加库存记录");
        System.out.println("3.查看特定库存");
        System.out.println("4.水果下架");
        System.out.println("5.退出");
        System.out.println("====================================================");
        System.out.print("请选择:");


        return input.nextInt();
    }
    //显示库存列表
    public void showFruitList() {
        System.out.println("----------------------------------------------------");
        System.out.println("名称\t\t单价\t\t库存\t\t备注");
        if (fruitList == null || fruitList.size() <= 0) {
            System.out.println("对不起,库存为空!");
        } else {
            /*fruitList.forEach(new Consumer() {
                @Override
                public void accept(Fruit fruit) {
                    System.out.println(fruit);
                }
            });*/
//            fruitList.forEach(fruit -> System.out.println(fruit));
            fruitList.forEach(System.out::println);
        }
        System.out.println("----------------------------------------------------");
    }
    //根据名称查询指定的库存记录
    public Fruit getFruitByFname(String fname) {
        //判断型接口    有参数,返回值是布尔类型
      /*  fruitList.stream().filter(new Predicate() {
            @Override
            public boolean test(Fruit fruit) {
                return fname.equals(fruit.getFname());
            }
        });*/
        return fruitList.stream().filter(fruit -> fname.equals(fruit.getFname())).findFirst().orElseGet(() -> null);

      /*  for (Fruit fruit : fruitList) {
            if (fname.equals(fruit.getFname())) {
                return fruit;
            }
        }
        return null;*/
    }
    //添加库存记录
    public void addFruit() {
        System.out.print("请输入水果名称:");
        String fname = input.next();

        Fruit fruit = getFruitByFname(fname);

        if (fruit == null) {
            System.out.print("请输入水果单价:");
            Integer price = input.nextInt();
            System.out.print("请输入水果库存:");
            Integer fcount = input.nextInt();
            System.out.print("请输入水果备注:");
            String remark = input.next();

            fruit = new Fruit(fname, price, fcount, remark);
            fruitList.add(fruit);
        } else {
            System.out.print("请输入追加的库存量:");
            Integer fcount = input.nextInt();
            fruit.setFcount(fruit.getFcount() + fcount);
        }
        System.out.println("添加成功!");
    }
    //查看特定库存记录
    public void showFruitInfo() {
        System.out.print("请输入水果名称:");
        String fname = input.next();
        Fruit fruit = getFruitByFname(fname);

        if (fruit == null) {
            System.out.println("对不起,没有找到对应的库存记录!");
        } else {
            System.out.println("----------------------------------------------------");
            System.out.println("名称\t\t单价\t\t库存\t\t备注");
            System.out.println(fruit);
            System.out.println("----------------------------------------------------");
        }
    }
    //水果下架
    public void delFruit() {
        System.out.print("请输入水果名称:");
        String fname = input.next();

        Fruit fruit = getFruitByFname(fname);
        if (fruit == null) {
            System.out.println("对不起,没有找到需要下架的库存记录!");
        } else {
            System.out.print("是否确认下架?(Y/N)");
            String confirm = input.next();
            if ("y".equalsIgnoreCase(confirm)) {
                fruitList.remove(fruit);
            }

        }
    }
    //退出
    public boolean exit() {
        System.out.print("是否确认退出?(Y/N)");
        String confirm = input.next();
        boolean flag= !"y".equalsIgnoreCase(confirm);
        if (!flag) {
            try {
                saveData();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return flag;
    }
    //保存数据
    public void saveData() throws IOException {

//        PrintWriter pw = new PrintWriter(new FileWriter("pro02-fruit-Serializable/src/main/resources/fruit.dat"));

      /*  fruitList.forEach(new Consumer() {
            @Override
            public void accept(Fruit fruit) {
                pw.println(fruit.getFname() + " " + fruit.getPrice() + " " + fruit.getFcount() + " " + fruit.getRemark());
            }
        });*/

       /* fruitList.forEach(fruit -> pw.println(fruit.getFname() + " " + fruit.getPrice() + " " + fruit.getFcount() + " " + fruit.getRemark()));

        pw.flush();*/

        File file = new File("pro02-fruit-Serializable/src/main/resources/fruit.dat");
        System.out.println(file.exists());
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
        oos.writeObject(fruitList);
        oos.flush();
    }
}
package com.csdn.fruit.view;
public class Client {
    public static void main(String[] args) {
        Menu m = new Menu();
        boolean flag = true;
        while (flag) {
            int slt = m.showMainMenu();
            switch (slt) {
                case 1:
                    m.showFruitList();
                    break;
                case 2:
                    m.addFruit();
                    break;
                case 3:
                    m.showFruitInfo();
                    break;
                case 4:
                    m.delFruit();
                    break;
                case 5:
                    //方法设计时是否需要返回值,依据是:是否在调用的地方需要留下一些值用于再运算
                    flag = m.exit();
                    break;
                default:
                    System.out.println("你不按套路出牌!");
                    break;
            }
        }
        System.out.println("谢谢使用!再见!");
    }
}

使用序列化技术保存数据 改进 IO流完成项目实战水果库存系统_第1张图片

你可能感兴趣的:(#,JDBC,java,水果系统,IO操作,反射)