【JavaSE】商场商品管理系统 使用I/O流输出到外部文件 内涵list.stream.filter常用方式

代码

//写一个程序能够对商品进行增,删,改,查

import java.io.*;
import java.util.*;

public class MAIN {
    private static Scanner scanner = new Scanner(System.in);
    public static final String PATH = "D:\\JavaProjects\\b站草稿\\大作业商品管理器\\src\\Message.txt";

    public static void main(String[] args) {
        while (true){
            System.out.println("请选择功能: 1.插入商品 2.删除商品 3.修改商品 4.查看全部商品 5.查找一个商品 6.退出");
            int choose = scanner.nextInt();
            switch (choose){
                case 1:
                    insert();
                    break;
                case 2:
                    delete();
                    break;
                case 3:
                    update();
                    break;
                case 4:
                    check();
                    break;
                case 5:
                    findone();
                    break;
                case 6:
                    System.exit(-1);
                    break;
            }
        }
    }

    //增添商品
    private static void insert(){
        int id = Repeat();
        System.out.println("请输入商品名字");
        String name = scanner.next();

        System.out.println("请输入商品价格");
        int price = scanner.nextInt();

        OutputStream put = null;
        try {
            //true代表追加,不覆盖
             put = new FileOutputStream(PATH,true);
             String shop = id + " " + name+ " " + price + "\n";
             put.write(shop.getBytes());
             put.flush();
            System.out.println("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (put != null){
                try {
                    put.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //删除商品
    private static void delete(){
        System.out.println("请输入商品ID");
        int id = scanner.nextInt();
        List<Goods> allGoods = findAllGoods();

        Iterator<Goods> iterator = allGoods.iterator();
        while (iterator.hasNext()){
            Goods goods = iterator.next();
            if (goods.getId() == id){
                iterator.remove();
                System.out.println("删除成功");
            }
        }
        OutData(allGoods);
    }


    //修改商品
    private static void update(){
        System.out.println("请输入商品ID");
        int id = scanner.nextInt();
        System.out.println("请输入商品名字");
        String name = scanner.next();
        System.out.println("请输入商品价格");
        int price = scanner.nextInt();

        List<Goods> allGoods = findAllGoods();
        Iterator<Goods> iterator = allGoods.iterator();
        while (iterator.hasNext()){
            Goods goods = iterator.next();
            if (goods.getId() == id){
                goods.setId(id);
                goods.setName(name);
                goods.setPrice(price);
                System.out.println("修改成功");
            }
        }
        OutData(allGoods);
    }

    //查看全部商品
    private static void check(){
        //新建List<Goods>对象接收findallgoods集合
        List<Goods> findall = findAllGoods();

        //强遍历,接着输出
        for (Goods goods : findall) {
            System.out.println(goods.getId() + " " + goods.getName() + " "+ goods.getPrice() );
        }
    }

    //查找一个商品
    private static void findone(){
        System.out.println("请输入商品ID");
        int id = scanner.nextInt();
        List<Goods> one = findAllGoods();
        for (Goods goods : one) {
            if (goods.getId() == id){
                System.out.println(goods.getId() + " " + goods.getName() + " "+ goods.getPrice());
            }else {
                System.out.println("您查询的商品不存在");
            }
        }
    }



    
    //判断有无重复代码
    private static int Repeat(){
        //防止id重复
        Integer id = null;
        boolean flag = true;
        //while(true)的思想 用bool的参数传进while,在后续代码中按条件修改参数已达跳出循环的效果
        while (flag){
            System.out.println("请输入商品ID");
            id = scanner.nextInt();
            //因为filter方法里的参数需要是final所以要重新定义一个
            final Integer i = id;
            //函数式编程,查找集合中的第一个对象ObjectList.stream() .filter(a -> "xxx".equals(a.getName())) .findFirst();
            Optional<Goods> first = findAllGoods().stream().filter(p -> p.getId() == i).findFirst();
            //判断是否存在
            flag = first.isPresent();
            //first.isEmpty()判断是否为空
            if (flag){
                System.out.println("该商品已存在");
            }
        }
        return id;
    }

    //把文件写入磁盘方法
    private static void OutData(List<Goods> goods){
        Writer writer = null;
        try {
            writer = new FileWriter(PATH);
            BufferedWriter bufferedWriter = new BufferedWriter(writer);
            for (Goods good : goods) {
                bufferedWriter.write(good.getId() + " " + good.getName() + " "+ good.getPrice());
                bufferedWriter.newLine();
                bufferedWriter.flush();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //查找全部商品方法
    //新建一个返回值是List数组的泛型类指定为Goods类
    private static List<Goods> findAllGoods(){
        //新建一个goods的ArrayList以便后面接收数据
        List<Goods> goods = new ArrayList<>();
        //标准写法先定义一个FileReader对象为空
        FileReader fileReader = null;
        try {
            //这里传入地址
            fileReader = new FileReader(PATH);
            //放入缓冲流 为了使用readline使用BufferedReader
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            //新建接收读入数据的字符串
            String goodstr;
            //while循环,逐行读出数据至goodstr
            while ((goodstr = bufferedReader.readLine()) != null){
                //新建字符串数组,用空格作为分隔符,新方法split
                String[] goodsElem = goodstr.split(" ");
                //因为用空格做了分隔符所以传入数组的第一个元素为id,第二是name,第三是pirce
                //直接作为参数传进刚刚新建的Goods对象,因为第一第三需要的是int,这里需要强转为int,方法为Integer.parseInt()
                Goods goods1 = new Goods(Integer.parseInt(goodsElem[0]),
                        goodsElem[1],Integer.parseInt(goodsElem[2]));
                //将Goods一个个添加到ArrayList里
                goods.add(goods1);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回ArratList
        return goods;
    }

}

list.stream.filter常用方式

1.查找集合中的第一个对象

Optional first= ObjectList.stream() .filter(a -> “hanmeimei”.equals(a.getUserName())) .findFirst();

2.返回符合查询条件的集合

//所有名字叫hanmeimei的集合

List firstObject= ObjectList.stream() .filter(a -> “hanmeimei”.equals(a.getUserName())) .collect(Collectors.toList());

//所有名字为空的集合

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值