已解决
使用序列化技术保存数据 改进 IO流完成项目实战水果库存系统
来自网友在路上 166866提问 提问时间:2023-10-24 01:16:12阅读次数: 66
最佳答案 问答题库668位专家为你答疑解惑
- 上一节内容是 使用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;@Overridepublic 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<Fruit> 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 字符转换流:InputStreamReaderBufferedReader 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<Fruit>) 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<Fruit>() {@Overridepublic 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<Fruit>() {@Overridepublic 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<Fruit>() {@Overridepublic 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("谢谢使用!再见!");} }
查看全文
99%的人还看了
相似问题
- 7个最佳开源免费库存/仓库管理系统(WMS)
- 【开源】基于JAVA的服装店库存管理系统
- 电商API:淘宝京东拼多多1688多电商平台的商品销量库存信息获取
- 项目实战:修改水果库存系统特定库存记录
- 项目实战:添加库存时判断水果名称是否已经存在
- 项目实战:通过axios加载水果库存系统的首页数据
- 妙手ERP本期功能更新:TikTok支持自定义SKU规格、Temu支持创建尺码表、仓库库存可同步至Shopee全球产品 ......
- 【2023Mathorcup大数据】B题 电商零售商家需求预测及库存优化问题 python代码解析
- 开源库存管理系统InvenTree的安装
- (四)库存超卖案例实战——优化redis分布式锁
猜你感兴趣
版权申明
本文"使用序列化技术保存数据 改进 IO流完成项目实战水果库存系统":http://eshow365.cn/6-22856-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!