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


    1. package com.csdn.fruit.pojo;
    2. import lombok.AllArgsConstructor;
    3. import lombok.Data;
    4. import lombok.NoArgsConstructor;
    5. import java.io.Serializable;
    6. @Data
    7. @NoArgsConstructor
    8. @AllArgsConstructor
    9. public class Fruit implements Serializable {
    10. private String fname;
    11. private Integer price;
    12. private Integer fcount;
    13. private String remark;
    14. @Override
    15. public String toString() {
    16. return fname + "\t\t" + price + "\t\t" + fcount + "\t\t" + remark;
    17. }
    18. }
    1. package com.csdn.fruit.view;
    2. import com.csdn.fruit.pojo.Fruit;
    3. import java.io.*;
    4. import java.util.ArrayList;
    5. import java.util.List;
    6. import java.util.Scanner;
    7. import java.util.function.Consumer;
    8. import java.util.function.Predicate;
    9. public class Menu {
    10. Scanner input = new Scanner(System.in);
    11. List fruitList = new ArrayList<>();
    12. {
    13. try {
    14. loadData();
    15. } catch (IOException | ClassNotFoundException e) {
    16. throw new RuntimeException(e);
    17. }
    18. }
    19. //加载数据
    20. public void loadData() throws IOException, ClassNotFoundException {
    21. InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("fruit.dat");
    22. /*if (inputStream != null) {
    23. //字节流转换成字符流
    24. //字节流:InputStream 字符流:Reader 字符转换流:InputStreamReader
    25. BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
    26. String str = null;
    27. while ((str = bf.readLine()) != null) {
    28. String[] arr = str.split(" ");
    29. String fname = arr[0];
    30. Integer price = Integer.parseInt(arr[1]);
    31. Integer fcount = Integer.parseInt(arr[2]);
    32. String remark = arr[3];
    33. Fruit fruit = new Fruit(fname, price, fcount, remark);
    34. fruitList.add(fruit);
    35. }
    36. }*/
    37. if (inputStream!=null) {
    38. ObjectInputStream ois = new ObjectInputStream(inputStream);
    39. fruitList = (List) ois.readObject();
    40. }
    41. }
    42. //显示主菜单
    43. public int showMainMenu() {
    44. System.out.println("================欢迎使用水果库存系统===================");
    45. System.out.println("1.显示库存列表");
    46. System.out.println("2.添加库存记录");
    47. System.out.println("3.查看特定库存");
    48. System.out.println("4.水果下架");
    49. System.out.println("5.退出");
    50. System.out.println("====================================================");
    51. System.out.print("请选择:");
    52. return input.nextInt();
    53. }
    54. //显示库存列表
    55. public void showFruitList() {
    56. System.out.println("----------------------------------------------------");
    57. System.out.println("名称\t\t单价\t\t库存\t\t备注");
    58. if (fruitList == null || fruitList.size() <= 0) {
    59. System.out.println("对不起,库存为空!");
    60. } else {
    61. /*fruitList.forEach(new Consumer() {
    62. @Override
    63. public void accept(Fruit fruit) {
    64. System.out.println(fruit);
    65. }
    66. });*/
    67. // fruitList.forEach(fruit -> System.out.println(fruit));
    68. fruitList.forEach(System.out::println);
    69. }
    70. System.out.println("----------------------------------------------------");
    71. }
    72. //根据名称查询指定的库存记录
    73. public Fruit getFruitByFname(String fname) {
    74. //判断型接口 有参数,返回值是布尔类型
    75. /* fruitList.stream().filter(new Predicate() {
    76. @Override
    77. public boolean test(Fruit fruit) {
    78. return fname.equals(fruit.getFname());
    79. }
    80. });*/
    81. return fruitList.stream().filter(fruit -> fname.equals(fruit.getFname())).findFirst().orElseGet(() -> null);
    82. /* for (Fruit fruit : fruitList) {
    83. if (fname.equals(fruit.getFname())) {
    84. return fruit;
    85. }
    86. }
    87. return null;*/
    88. }
    89. //添加库存记录
    90. public void addFruit() {
    91. System.out.print("请输入水果名称:");
    92. String fname = input.next();
    93. Fruit fruit = getFruitByFname(fname);
    94. if (fruit == null) {
    95. System.out.print("请输入水果单价:");
    96. Integer price = input.nextInt();
    97. System.out.print("请输入水果库存:");
    98. Integer fcount = input.nextInt();
    99. System.out.print("请输入水果备注:");
    100. String remark = input.next();
    101. fruit = new Fruit(fname, price, fcount, remark);
    102. fruitList.add(fruit);
    103. } else {
    104. System.out.print("请输入追加的库存量:");
    105. Integer fcount = input.nextInt();
    106. fruit.setFcount(fruit.getFcount() + fcount);
    107. }
    108. System.out.println("添加成功!");
    109. }
    110. //查看特定库存记录
    111. public void showFruitInfo() {
    112. System.out.print("请输入水果名称:");
    113. String fname = input.next();
    114. Fruit fruit = getFruitByFname(fname);
    115. if (fruit == null) {
    116. System.out.println("对不起,没有找到对应的库存记录!");
    117. } else {
    118. System.out.println("----------------------------------------------------");
    119. System.out.println("名称\t\t单价\t\t库存\t\t备注");
    120. System.out.println(fruit);
    121. System.out.println("----------------------------------------------------");
    122. }
    123. }
    124. //水果下架
    125. public void delFruit() {
    126. System.out.print("请输入水果名称:");
    127. String fname = input.next();
    128. Fruit fruit = getFruitByFname(fname);
    129. if (fruit == null) {
    130. System.out.println("对不起,没有找到需要下架的库存记录!");
    131. } else {
    132. System.out.print("是否确认下架?(Y/N)");
    133. String confirm = input.next();
    134. if ("y".equalsIgnoreCase(confirm)) {
    135. fruitList.remove(fruit);
    136. }
    137. }
    138. }
    139. //退出
    140. public boolean exit() {
    141. System.out.print("是否确认退出?(Y/N)");
    142. String confirm = input.next();
    143. boolean flag= !"y".equalsIgnoreCase(confirm);
    144. if (!flag) {
    145. try {
    146. saveData();
    147. } catch (IOException e) {
    148. throw new RuntimeException(e);
    149. }
    150. }
    151. return flag;
    152. }
    153. //保存数据
    154. public void saveData() throws IOException {
    155. // PrintWriter pw = new PrintWriter(new FileWriter("pro02-fruit-Serializable/src/main/resources/fruit.dat"));
    156. /* fruitList.forEach(new Consumer() {
    157. @Override
    158. public void accept(Fruit fruit) {
    159. pw.println(fruit.getFname() + " " + fruit.getPrice() + " " + fruit.getFcount() + " " + fruit.getRemark());
    160. }
    161. });*/
    162. /* fruitList.forEach(fruit -> pw.println(fruit.getFname() + " " + fruit.getPrice() + " " + fruit.getFcount() + " " + fruit.getRemark()));
    163. pw.flush();*/
    164. File file = new File("pro02-fruit-Serializable/src/main/resources/fruit.dat");
    165. System.out.println(file.exists());
    166. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
    167. oos.writeObject(fruitList);
    168. oos.flush();
    169. }
    170. }
    1. package com.csdn.fruit.view;
    2. public class Client {
    3. public static void main(String[] args) {
    4. Menu m = new Menu();
    5. boolean flag = true;
    6. while (flag) {
    7. int slt = m.showMainMenu();
    8. switch (slt) {
    9. case 1:
    10. m.showFruitList();
    11. break;
    12. case 2:
    13. m.addFruit();
    14. break;
    15. case 3:
    16. m.showFruitInfo();
    17. break;
    18. case 4:
    19. m.delFruit();
    20. break;
    21. case 5:
    22. //方法设计时是否需要返回值,依据是:是否在调用的地方需要留下一些值用于再运算
    23. flag = m.exit();
    24. break;
    25. default:
    26. System.out.println("你不按套路出牌!");
    27. break;
    28. }
    29. }
    30. System.out.println("谢谢使用!再见!");
    31. }
    32. }

  • 相关阅读:
    温故而知新:IIR滤波器设计的方法,幅频计算和参数理解
    哈工大李治军老师操作系统笔记【27】:从生磁盘到文件(Learning OS Concepts By Coding Them !)
    04-java数据结构之栈的学习
    【前端笔记】记录一个能优化Echarts Geo JSON大小的网站
    Proxyless Mesh 在 Dubbo 中的实践
    第二证券|汽车板块现涨停潮!多只地产债再涨到临停
    【Java基础系列】循环与迭代
    Arduino UNO驱动MPR121接近电容式触摸传感器控制WS2812彩灯
    Linux安装tomcat(附带安装包)
    【面试题-007】说一下JVM原理
  • 原文地址:https://blog.csdn.net/m0_65152767/article/details/134001023