1.1  重点

1.2  代码编程

 

 

1.1  重点

一种新的思维方式:可以看看 demo(一个方法 可以 根据两个参数 任一参数判断

 

 

1.2  代码编程

需求:

一个方法 可以 根据两个参数 任一参数判断,

 

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

 

商品服务类:(看filterSkuListByParamB方法)

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;
    }


    /**
     * versionTestC
     * 然并卵
     *
     * 一个方法 可以 根据两个参数 任一参数判断,
     *
     * 若flag为true ,根据枚举类判断,否则,根据总价判断
     *
     * @param skuList
     * @return
     */
    public static List<Sku> filterSkuListByParamB(List<Sku> skuList,SkuCategoryEnum skuCategoryEnum,Double dTotalPrice,boolean flag){
        List<Sku> lElectronicsList = new ArrayList<Sku>();
        for (int i=0;i<skuList.size();i++){
            if ((flag && skuList.get(i).getSkuCategory().equals(skuCategoryEnum))
                ||
                (!flag&&skuList.get(i).getTotalPrice()>dTotalPrice)){
                lElectronicsList.add(skuList.get(i));
            }
        }
        return lElectronicsList;
    }



    /**
     * versionTestD
     * 生产上不建议这种写法,会引起类爆炸。一切都是为了引入lambda表达式
     *
     * @param skuList
     * @return
     */
    public static List<Sku> filterSkuListByParamPrediCate(List<Sku> skuList,SkuPrediCate skuPrediCate){
        List<Sku> lElectronicsList = new ArrayList<Sku>();
        for (int i=0;i<skuList.size();i++){
            if (skuPrediCate.test(skuList.get(i))){
                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 VersionTestC {

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

        List<Sku> getParamSkuList = CartService.filterSkuListByParamB(skuList,null,200.00,false);
        System.out.println(JSON.toJSONString(getParamSkuList,true));

   }

}

 

 

打印日志:

[
    {
        "skuCategory":"ELECTRONICS",
        "skuId":2020001,
        "skuName":"无人机",
        "skuPrice":999.0,
        "totalNum":1,
        "totalPrice":999.0
    },
    {
        "skuCategory":"BOOKS",
        "skuId":2020005,
        "skuName":"剑指高效编程",
        "skuPrice":288.0,
        "totalNum":1,
        "totalPrice":288.0
    },
    {
        "skuCategory":"CLOTHING",
        "skuId":2020006,
        "skuName":"大头皮鞋",
        "skuPrice":300.0,
        "totalNum":1,
        "totalPrice":300.0
    },
    {
        "skuCategory":"SPROTS",
        "skuId":2020007,
        "skuName":"杠铃",
        "skuPrice":2000.0,
        "totalNum":1,
        "totalPrice":2000.0
    },
    {
        "skuCategory":"ELECTRONICS",
        "skuId":2020008,
        "skuName":"ThinkPad",
        "skuPrice":5000.0,
        "totalNum":1,
        "totalPrice":5000.0
    }
]

Process finished with exit code 0

 

posted on 2020-04-01 06:39  菜鸟乙  阅读(210)  评论(0编辑  收藏  举报