1. package practise;
    2. import java.io.*;
    3. import java.util.ArrayList;
    4. import java.util.Scanner;
    5. /**
    6. * @auther Devil(丁杨维)
    7. * @create 2021-10-21-17:04
    8. * 完成一个仓储系统:
    9. * 能够显示当前仓库中每件商品的id,商品名称,当前数量,总数量,要求能够添加商品,借出商品
    10. * 能够根据商品名称模糊查询商品的全部信息
    11. */
    12. public class E_28 {
    13. public static void main(String[] args) {
    14. int i;
    15. Scanner sc = new Scanner(System.in);
    16. try {
    17. //读取文件中的数据
    18. ArrayList<Product> products = inputData();
    19. //创建一个文件管理对象
    20. ProductManagement productManagement = new ProductManagement(products);
    21. System.out.println("--------------------欢迎使用仓库管理系统----------------------");
    22. while (true) {
    23. //简易的菜单
    24. System.out.println("**************1.模糊查询商品的信息****************");
    25. System.out.println("**************2.添加商品*************************");
    26. System.out.println("**************3.借出商品*************************");
    27. System.out.println("**************4.查询商品全部信息******************");
    28. System.out.println("**************0.退出*****************************");
    29. System.out.print("请输入数字编号以操作:");
    30. i = sc.nextInt();
    31. for (int i1 = 0; i1 < 50; i1++) {
    32. System.out.println();
    33. }
    34. switch (i) {
    35. case 1:
    36. System.out.print("1.模糊查询商品的信息\n请输入商品名称关键字查找:");
    37. if (!productManagement.queryProducts(sc.next())) {
    38. System.out.println("查找失败,可能没有该商品,请重新操作");
    39. }
    40. break;
    41. case 2:
    42. System.out.print("添加商品\n请输入要添加的位置和商品的信息(商品id、商品名称、商品当前数量、总数量,用空格隔开):");
    43. if (!productManagement.add(new Product(sc.next(), sc.next(), sc.nextInt(), sc.nextInt()))) {
    44. System.out.println("添加失败");
    45. } else {
    46. System.out.println("添加成功");
    47. }
    48. break;
    49. case 3:
    50. System.out.print("借出商品\n请输入商品名称和借出的数量(用空格隔开):");
    51. if (!productManagement.lend(sc.next(), sc.nextInt())) {
    52. System.out.println("借出商品失败,可能时没有该商品也可能时借出的数量超过商品当前数量,请重新操作");
    53. }
    54. break;
    55. case 4:
    56. System.out.println("全部商品信息:");
    57. productManagement.queryProducts();
    58. break;
    59. case 0:
    60. //将修改后的数据存入文件
    61. outputData(products);
    62. System.out.print("退出成功!");
    63. System.exit(0);
    64. default:
    65. break;
    66. }
    67. }
    68. } catch (Exception e) {
    69. e.printStackTrace();
    70. }
    71. }
    72. /**
    73. * 从文件中读取数据到内存
    74. *
    75. * @return ArrayList<Product>
    76. */
    77. public static ArrayList<Product> inputData() throws Exception {
    78. ArrayList<Product> p = new ArrayList<>();
    79. String str;
    80. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    81. FileReader fileReader = new FileReader("chanpin.txt");
    82. BufferedReader bufferedReader = new BufferedReader(fileReader);
    83. while ((str = bufferedReader.readLine()) != null) {
    84. String[] data = str.split(" +");
    85. p.add(new Product(data[0], data[1], Integer.parseInt(data[2]), Integer.parseInt(data[3])));
    86. }
    87. bufferedReader.close();
    88. fileReader.close();
    89. return p;
    90. }
    91. /**
    92. * 将内存中的数据输出到文件
    93. */
    94. public static void outputData(ArrayList<Product> products) throws Exception {
    95. String str;
    96. FileWriter fileWriter = new FileWriter("chanpin.txt", false);
    97. BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
    98. for (Product product : products) {
    99. str = product.getId() + " " + product.getName() + " " + product.getCurrentQuantity() + " " + product.getTotalAmount();
    100. bufferedWriter.write(str, 0, str.length());
    101. bufferedWriter.newLine();
    102. }
    103. bufferedWriter.close();
    104. fileWriter.close();
    105. }
    106. }
    107. /**
    108. * 产品类
    109. */
    110. class Product {
    111. private String id;
    112. private String name;
    113. private int currentQuantity;
    114. private int totalAmount;
    115. public Product(String id, String name, int currentQuantity, int totalAmount) {
    116. this.id = id;
    117. this.name = name;
    118. this.currentQuantity = currentQuantity;
    119. this.totalAmount = totalAmount;
    120. }
    121. /**
    122. * 更新商品当前数量
    123. */
    124. public void setCurrentQuantity(int currentQuantity) {
    125. this.currentQuantity = currentQuantity;
    126. }
    127. /**
    128. * 更新商品总数
    129. */
    130. public void setTotalAmount(int totalAmount) {
    131. this.totalAmount = totalAmount;
    132. }
    133. public String getId() {
    134. return id;
    135. }
    136. /**
    137. * 获取商品名称
    138. */
    139. public String getName() {
    140. return name;
    141. }
    142. /**
    143. * 获取商品当前数量
    144. */
    145. public int getCurrentQuantity() {
    146. return currentQuantity;
    147. }
    148. /**
    149. * 获取商品总数
    150. */
    151. public int getTotalAmount() {
    152. return totalAmount;
    153. }
    154. }
    155. /**
    156. * 产品管理类
    157. */
    158. class ProductManagement {
    159. ArrayList<Product> products;
    160. Product p = null;
    161. public ProductManagement(ArrayList<Product> products) {
    162. this.products = products;
    163. }
    164. /**
    165. * 添加商品
    166. */
    167. public boolean add(Product product) {
    168. int i;
    169. boolean flag = false;
    170. for (i = 0; i < products.size(); i++) {
    171. if (products.get(i).getName().equals(product.getName())) {
    172. products.get(i).setTotalAmount(products.get(i).getTotalAmount() + product.getTotalAmount());
    173. products.get(i).setCurrentQuantity(products.get(i).getCurrentQuantity() + product.getCurrentQuantity());
    174. flag = true;
    175. break;
    176. }
    177. }
    178. if (i >= products.size()) {
    179. flag = true;
    180. products.add(product);
    181. }
    182. return flag;
    183. }
    184. /**
    185. * 借出商品
    186. */
    187. public boolean lend(String str, int num) {
    188. if ((p = queryProduct(str)) != null) {
    189. if ((p.getCurrentQuantity() - num) < 0) {
    190. return false;
    191. }
    192. p.setCurrentQuantity(p.getCurrentQuantity() - num);
    193. return true;
    194. } else {
    195. return false;
    196. }
    197. }
    198. /**
    199. * 模糊查询产品信息
    200. */
    201. public boolean queryProducts(String name) {
    202. boolean flag = false;
    203. for (Product product : products) {
    204. if (product.getName().contains(name)) {
    205. System.out.println("产品id:" + product.getId() + " 商品名称:" + product.getName() + " 商品当前数量:" + product.getCurrentQuantity() + " 商品总数量:" + product.getTotalAmount());
    206. flag = true;
    207. }
    208. }
    209. return flag;
    210. }
    211. /**
    212. * 输出全部商品的全部信息
    213. */
    214. public void queryProducts() {
    215. for (Product product : products) {
    216. System.out.println("产品id:" + product.getId() + " 商品名称:" + product.getName() + " 商品当前数量:" + product.getCurrentQuantity() + " 商品总数量:" + product.getTotalAmount());
    217. }
    218. System.out.println("\n");
    219. }
    220. /**
    221. * 商品名称查询商品
    222. */
    223. public Product queryProduct(String name) {
    224. Product product = null;
    225. for (Product value : products) {
    226. if (name.equals(value.getName())) {
    227. product = value;
    228. }
    229. }
    230. return product;
    231. }
    232. }

    文件chanpin.txt内容

    1 小可乐 20 20
    2 中可乐 1 20
    3 雪碧 19 20
    4 大可乐 0 20
    5 橙汁 18 20
    6 我很想喝可乐 1 20

    控制台输出:
    image.png
    image.png
    image.png
    image.png
    image.png
    image.png
    image.png
    image.png
    image.png