|
发表于 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[2] + count;
- if (temp < 0){
- System.out.println("库存不足!");
- return false;
- }
- obj[2] = (Integer) obj[2] + 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[0], str[1], Integer.parseInt(str[2])};
- map.put(Integer.parseInt(str[0]), 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("库存暂未更新, 无需写入!");
- }
- }
- }
复制代码
|
|