案例7-2商品进货交易记录

package qust1.com;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class CollectionTest {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            Collection<Goods> goodsList = new ArrayList<>();
            boolean flag = true;
            while (flag) {
                SystemMenu();
                System.out.println("输入你的选项");
                int input = getValidIntInput(sc);
                switch (input) {
                    case 1:
                        addStorage(sc, goodsList);
                        break;
                    case 2:
                        showGoods(goodsList);
                        break;
                    case 3:
                        deleteGoods(goodsList, sc);
                        break;
                    case 4:
                        handleStockEntry(sc, goodsList);
                        break;
                    default:
                        flag = false;
                        break;
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static void SystemMenu() {
        System.out.println("-------库存管理系统------");
        System.out.println("1. 商品入库功能");
        System.out.println("2. 商品显示功能");
        System.out.println("3. 商品删除功能");
        System.out.println("4. 进货记录功能");
    }

    private static void addStorage(Scanner sc, Collection<Goods> goodsList) {
        Goods goods = new Goods();
        System.out.println("输入id");

        int id = getValidIntInput(sc);
        if (!goodsList.isEmpty()) {
            for (Goods value : goodsList) {
                if (value.getId() == id) {
                    System.out.println("该ID已存在");
                    return;
                }
            }
        }
        System.out.println("输入名字");
        String name = sc.next();

        System.out.println("输入颜色");
        String color = sc.next();
        System.out.println("输入价格");
        double price = getValidDoubleInput(sc);
        System.out.println("输入数量");
        int number = getValidIntInput(sc);
        goods.setId(id);
        goods.setName(name);
        goods.setColor(color);
        goods.setPrice(price);
        goods.setNum(number);
        goodsList.add(goods);
    }

    private static void showGoods(Collection<Goods> goodsList) {
        System.out.println("显示");
        System.out.println("编号" + "\t" + "名字\t" + "颜色\t" + "价格\t" + "数量\t");
        if (!goodsList.isEmpty()) {
            for (Goods goods : goodsList) {
                System.out.println(goods);
            }
        } else {
            System.out.println("没有商品可以显示");
        }
    }

    private static void deleteGoods(Collection<Goods> goodsList, Scanner sc) {
        System.out.println("输入id");
        int id = sc.nextInt();
        if (!goodsList.isEmpty()) {
            Iterator<Goods> iterator = goodsList.iterator();
            while (iterator.hasNext()) {
                if (iterator.next().getId() == id) {
                    iterator.remove();
                    System.out.println("删除成功");
                    return;
                }
            }
            System.out.println("没有这个ID");
        } else {
            System.out.println("库存为空");
        }
    }

    private static int getValidIntInput(Scanner sc) {
        while (true) {
            try {
                return sc.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("输入一个有效的整数");
                sc.next(); // 清除无效输入
            }
        }
    }

    private static double getValidDoubleInput(Scanner sc) {
        while (true) {
            try {
                return sc.nextDouble();
            } catch (InputMismatchException e) {
                System.out.println("输入一个有效的double值");
                sc.next(); // 清除无效输入
            }
        }
    }

    private static void handleStockEntry(Scanner sc, Collection<Goods> goodsList) throws IOException {
        System.out.println("输入商品编号");
        int id = getValidIntInput(sc);
        Goods selectedGoods = null;

        // 查找商品
        for (Goods goods : goodsList) {
            if (goods.getId() == id) {
                selectedGoods = goods;
                break;
            }
        }

        if (selectedGoods == null) {
            System.out.println("未找到该商品");
            return;
        }

        // 打印商品信息
        System.out.println("商品信息:");
        System.out.println(selectedGoods);

        // 输入进货数量
        System.out.println("输入进货数量");
        int quantityToAdd = getValidIntInput(sc);

        // 更新库存
        int newQuantity = selectedGoods.getNum() + quantityToAdd;
        selectedGoods.setNum(newQuantity);

        // 保存进货记录到CSV文件
        savePurchaseRecord(selectedGoods, quantityToAdd);
    }

    private static void savePurchaseRecord(Goods goods, int quantityToAdd) throws IOException {
        double totalPrice = goods.getPrice() * quantityToAdd;
        String contact = "联系人"; // 可替换为实际联系人

        // 获取当前日期
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String dateString = today.format(formatter);

        // 文件路径
        String fileName = "进货记录" + dateString + ".csv";
        String filePath = "D:\\IO\\" + fileName;

        // 记录行格式
        String record = goods.getId() + ","
                + goods.getName() + ","
                + quantityToAdd + ","
                + goods.getPrice() + ","
                + totalPrice + ","
                + contact + "\n";

        // 写入记录到CSV文件,若文件存在则追加
        try (BufferedOutputStream bos = new BufferedOutputStream(
                Files.newOutputStream(Paths.get(filePath), StandardOpenOption.CREATE, StandardOpenOption.APPEND))) {
            bos.write(record.getBytes());
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值