1.1  重点

1.2  代码编程

 

 

1.1  重点

 无

 

 

1.2  代码编程

需求:

程序员双11添加了很多宝贝到购物车,程序员女盆友开始筛选出所有的 任意类型 的产品

 

商品实体类:(同2-3)

 

商品服务类:

package com.imooc.zhangxiaoxi.lambda.cart;

import java.util.ArrayList;
import java.util.List;

public class CartService {

    /**
     * 匿名内部类,有内存泄漏的风险,不在生产项目中引用,只是为了后边引用出来lambda表达式
     */
    private static List<Sku> skuList = new ArrayList<Sku>(){
        {
            add(new Sku(2020001,"无人机",999.00,1,999.00,SkuCategoryEnum.ELECTRONICS));
            add(new Sku(2020002,"T-shirt",50.00,2,100.00,SkuCategoryEnum.CLOTHING));
            add(new Sku(2020003,"人生的枷锁",30.00,1,30.00,SkuCategoryEnum.BOOKS));
            add(new Sku(2020004,"老人与海",20.00,1,20.00,SkuCategoryEnum.BOOKS));
            add(new Sku(2020005,"剑指高效编程",288.00,1,288.00,SkuCategoryEnum.BOOKS));
            add(new Sku(2020006,"大头皮鞋",300.00,1,300.00,SkuCategoryEnum.CLOTHING));
            add(new Sku(2020007,"杠铃",2000.00,1,2000.00,SkuCategoryEnum.SPROTS));
            add(new Sku(2020008,"ThinkPad",5000.00,1,5000.00,SkuCategoryEnum.ELECTRONICS));
        }

    };

    public static List<Sku> getSkuList(){
        return skuList;
    }

    /**
     * versionTestA
     * 找出购物车中所有电子产品
     *
     * 注意for 循环
     *  for (Sku sku: skuList) 的使用
     * @return
     */
    public static List<Sku> getElectronicsSkuList(List<Sku> skuList){
        List<Sku> lElectronicsList = new ArrayList<Sku>();
        for (Sku sku: skuList){
           if (sku.getSkuCategory().equals(SkuCategoryEnum.ELECTRONICS)){
                lElectronicsList.add(sku);
            }
        }
/*
        for (int i=0;i<skuList.size();i++){
            if (skuList.get(i).getSkuCategory().equals(SkuCategoryEnum.ELECTRONICS)){
                lElectronicsList.add(skuList.get(i));
            }
        }
*/
        return lElectronicsList;
    }

    /**
     * versionTestB
     * 单一维度条件参数化,传入不同的enum,打印出不同的产品列表
     *
     * 注意第二个参数:如何调取枚举类参数
     *
     *
     * @param skuList
     * @return
     */
    public static List<Sku> filterSkuListByParam(List<Sku> skuList,SkuCategoryEnum skuCategoryEnum){
        List<Sku> lElectronicsList = new ArrayList<Sku>();
        for (int i=0;i<skuList.size();i++){
            if (skuList.get(i).getSkuCategory().equals(skuCategoryEnum)){
                lElectronicsList.add(skuList.get(i));
            }
        }
        return lElectronicsList;
    }


}

 

枚举类:(同2-3)

 

测试类:

package com.imooc.zhangxiaoxi.lambda.cart;

import com.alibaba.fastjson.JSON;
import org.junit.Test;

import java.util.List;

public class VersionTestB {

    @Test
   public void filterSkuListByParam(){
       List<Sku> skuList = CartService.getSkuList();
       List<Sku> getParamSkuList = CartService.filterSkuListByParam(skuList,SkuCategoryEnum.BOOKS);
       System.out.println(JSON.toJSONString(getParamSkuList,true));
   }

}

 

 

打印日志:

[
    {
        "skuCategory":"BOOKS",
        "skuId":2020003,
        "skuName":"人生的枷锁",
        "skuPrice":30.0,
        "totalNum":1,
        "totalPrice":30.0
    },
    {
        "skuCategory":"BOOKS",
        "skuId":2020004,
        "skuName":"老人与海",
        "skuPrice":20.0,
        "totalNum":1,
        "totalPrice":20.0
    },
    {
        "skuCategory":"BOOKS",
        "skuId":2020005,
        "skuName":"剑指高效编程",
        "skuPrice":288.0,
        "totalNum":1,
        "totalPrice":288.0
    }
]

Process finished with exit code 0

 

posted on 2020-03-31 05:55  菜鸟乙  阅读(200)  评论(0编辑  收藏  举报