鱼C论坛

 找回密码
 立即注册
查看: 1718|回复: 1

用流解决商品进货问题(菜菜,大佬帮帮)

[复制链接]
发表于 2022-4-22 20:52:44 | 显示全部楼层 |阅读模式
5鱼币
每个商城都需要进货,而这些进货记录整理起来很不方便,本案例要求编写一个记录商城进货交易的程序,使用字节流将商场的进货信息记录在本地的csv文件中。程序具体要求如下:
当用户输入商品编号时,后台会根据商品编号查询到相应商品信息,并打印商品信息。接着让用户输入需要进货的商品数量,程序将原有的库存数量与输入的数量相加,将相加后的结果信息保存至本地的csv文件中。

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-5-5 12:37:16 | 显示全部楼层

用着笨方法试着写了下,这里 csv 文件结构是这样的:
  1. Id,Goods,Inventory
  2. 1,Banada,10
  3. 2,Apple,10
  4. 3,Durain,15
  5. 4,Orange,10
  6. 5,Strawberry,10
复制代码


GoodsInventory.java 文件:
  1. import java.io.*;
  2. import java.nio.charset.StandardCharsets;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5. import java.util.TreeMap;

  6. public class GoodsInventory {
  7.     private Map<Integer, Object[]> map = new TreeMap<>();
  8.     private Object[] head;

  9.     public boolean writeFile(String filePath, Map<Integer, Object[]> map) {
  10.         FileOutputStream fos = null;
  11.         try {
  12.             fos = new FileOutputStream(filePath);
  13.             fos.write(String.format("%s,%s,%s\n", head).getBytes(StandardCharsets.UTF_8));
  14.             for (Integer id : map.keySet()){
  15.                 fos.write(String.format("%s,%s,%s\n", map.get(id)).getBytes(StandardCharsets.UTF_8));
  16.             }
  17.             System.out.println("库存文件更新成功!");
  18.         } catch (IOException e) {
  19.             throw new RuntimeException(e);
  20.         } finally {
  21.             if (fos != null){
  22.                 try {
  23.                     fos.close();
  24.                 } catch (IOException e) {
  25.                     throw new RuntimeException(e);
  26.                 }
  27.             }
  28.         }
  29.         return false;
  30.     }


  31.     public boolean updateInventoryById(Integer id, Integer count){
  32.         Object[] obj = map.get(id);
  33.         if (obj == null){
  34.             return false;
  35.         }
  36.         Integer temp = (Integer) obj[2] + count;
  37.         if (temp < 0){
  38.             System.out.println("库存不足!");
  39.             return false;
  40.         }
  41.         obj[2] = (Integer) obj[2] + count;
  42.         return true;
  43.     }


  44.     public Map<Integer, Object[]> readFile(String fileName){
  45.         FileInputStream fis = null;
  46.         BufferedReader br = null;
  47.         try {
  48.             fis = new FileInputStream(fileName);
  49.             br = new BufferedReader(new InputStreamReader(fis));

  50.             String data;
  51.             String[] str;
  52.             if ((data = br.readLine()) != null){
  53.                 str = data.split(",");
  54.                 head = str;
  55.             }
  56.             while ((data = br.readLine()) != null) {
  57.                 str = data.split(",");
  58.                 Object[] temp = {str[0], str[1], Integer.parseInt(str[2])};
  59.                 map.put(Integer.parseInt(str[0]), temp);
  60.             }
  61.         } catch (Exception e) {
  62.             throw new RuntimeException("文件未找到,请检查路径或文件名是否正确!");
  63.         } finally {
  64.             try {
  65.                 if (br != null){
  66.                     br.close();
  67.                 } else if (fis != null)
  68.                     fis.close();
  69.             } catch (Exception e) {
  70.                 throw new RuntimeException(e);
  71.             }
  72.         }
  73.         return map;
  74.     }

  75.     public boolean printGoodsByIdData(Integer id){
  76.         Object[] obj = map.get(id);
  77.         if (obj == null){
  78.             System.out.println("错误:没有找到此ID商品项!");
  79.             System.out.print("请重新输入商品ID: ");
  80.             return false;
  81.         }
  82.         System.out.printf("%s\t %s\t %s\n", head);
  83.         System.out.printf("%s\t %s\t %s\n", map.get(id));
  84.         return true;
  85.     }
  86. }
复制代码


GoodsInventoryTest.java 测试程序:
  1. import java.util.Map;
  2. import java.util.Scanner;

  3. public class GoodsInventoryTest {

  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);

  6.         String filePath = "./src/pers/uang/demo05/GoodsInventory.csv";
  7.         GoodsInventory inventory = new GoodsInventory();
  8.         Map<Integer , Object[]> map = inventory.readFile(filePath);
  9.         int id = -1;
  10.         boolean flag = false;
  11.         boolean isNew = false;
  12.         while (!flag){
  13.             System.out.print("请输入商品ID: ");
  14.             while (!flag) {
  15.                 id = scan.nextInt();
  16.                 scan.nextLine();
  17.                 flag = inventory.printGoodsByIdData(id);
  18.             }
  19.             System.out.print("\n请输入需要更新的库存数量(负号表示库存减少):");
  20.             int count = scan.nextInt();
  21.             scan.nextLine();
  22.             if (inventory.updateInventoryById(id, count)) {
  23.                 isNew = true;
  24.                 System.out.println("\n更新成功!");
  25.             } else {
  26.                 System.out.println("\n更新失败!");
  27.             }
  28.             System.out.print("还需要更新其它商品的库存记录吗(回车则继续, 任意输入退出系统): ");
  29.             String isExit = scan.nextLine();
  30.             if ("".equals(isExit)){
  31.                 flag = false;
  32.             }
  33.         }

  34.         // 写入文件中去
  35.         if (isNew){
  36.             inventory.writeFile(filePath, map);
  37.         } else {
  38.             System.out.println("库存暂未更新, 无需写入!");
  39.         }
  40.     }
  41. }
复制代码




想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 08:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表