揽一池星河 发表于 2022-4-22 20:52:44

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

每个商城都需要进货,而这些进货记录整理起来很不方便,本案例要求编写一个记录商城进货交易的程序,使用字节流将商场的进货信息记录在本地的csv文件中。程序具体要求如下:
当用户输入商品编号时,后台会根据商品编号查询到相应商品信息,并打印商品信息。接着让用户输入需要进货的商品数量,程序将原有的库存数量与输入的数量相加,将相加后的结果信息保存至本地的csv文件中。

Twilight6 发表于 2022-5-5 12:37:16


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

GoodsInventory.java 文件:
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

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

    public boolean writeFile(String filePath, Map<Integer, Object[]> map) {
      FileOutputStream fos = null;
      try {
            fos = new FileOutputStream(filePath);
            fos.write(String.format("%s,%s,%s\n", head).getBytes(StandardCharsets.UTF_8));
            for (Integer id : map.keySet()){
                fos.write(String.format("%s,%s,%s\n", map.get(id)).getBytes(StandardCharsets.UTF_8));
            }
            System.out.println("库存文件更新成功!");
      } catch (IOException e) {
            throw new RuntimeException(e);
      } finally {
            if (fos != null){
                try {
                  fos.close();
                } catch (IOException e) {
                  throw new RuntimeException(e);
                }
            }
      }
      return false;
    }


    public boolean updateInventoryById(Integer id, Integer count){
      Object[] obj = map.get(id);
      if (obj == null){
            return false;
      }
      Integer temp = (Integer) obj + count;
      if (temp < 0){
            System.out.println("库存不足!");
            return false;
      }
      obj = (Integer) obj + count;
      return true;
    }


    public Map<Integer, Object[]> readFile(String fileName){
      FileInputStream fis = null;
      BufferedReader br = null;
      try {
            fis = new FileInputStream(fileName);
            br = new BufferedReader(new InputStreamReader(fis));

            String data;
            String[] str;
            if ((data = br.readLine()) != null){
                str = data.split(",");
                head = str;
            }
            while ((data = br.readLine()) != null) {
                str = data.split(",");
                Object[] temp = {str, str, Integer.parseInt(str)};
                map.put(Integer.parseInt(str), temp);
            }
      } catch (Exception e) {
            throw new RuntimeException("文件未找到,请检查路径或文件名是否正确!");
      } finally {
            try {
                if (br != null){
                  br.close();
                } else if (fis != null)
                  fis.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
      }
      return map;
    }

    public boolean printGoodsByIdData(Integer id){
      Object[] obj = map.get(id);
      if (obj == null){
            System.out.println("错误:没有找到此ID商品项!");
            System.out.print("请重新输入商品ID: ");
            return false;
      }
      System.out.printf("%s\t %s\t %s\n", head);
      System.out.printf("%s\t %s\t %s\n", map.get(id));
      return true;
    }
}

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

public class GoodsInventoryTest {

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

      String filePath = "./src/pers/uang/demo05/GoodsInventory.csv";
      GoodsInventory inventory = new GoodsInventory();
      Map<Integer , Object[]> map = inventory.readFile(filePath);
      int id = -1;
      boolean flag = false;
      boolean isNew = false;
      while (!flag){
            System.out.print("请输入商品ID: ");
            while (!flag) {
                id = scan.nextInt();
                scan.nextLine();
                flag = inventory.printGoodsByIdData(id);
            }
            System.out.print("\n请输入需要更新的库存数量(负号表示库存减少):");
            int count = scan.nextInt();
            scan.nextLine();
            if (inventory.updateInventoryById(id, count)) {
                isNew = true;
                System.out.println("\n更新成功!");
            } else {
                System.out.println("\n更新失败!");
            }
            System.out.print("还需要更新其它商品的库存记录吗(回车则继续, 任意输入退出系统): ");
            String isExit = scan.nextLine();
            if ("".equals(isExit)){
                flag = false;
            }
      }

      // 写入文件中去
      if (isNew){
            inventory.writeFile(filePath, map);
      } else {
            System.out.println("库存暂未更新, 无需写入!");
      }
    }
}



页: [1]
查看完整版本: 用流解决商品进货问题(菜菜,大佬帮帮)