1.1  重点

1.2  代码编程

 

 

1.1  重点

枚举类的使用(SkuCategoryEnum);

JSONtoString方法的使用(测试类);

两种不同list的使用方式(商品服务类);

list的add方法

 

 

1.2  代码编程

需求:

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

 

商品实体类:

package com.imooc.zhangxiaoxi.lambda.cart;

/**
 * 商品类
 */
public class Sku {
    private Integer skuId;      //商品编号
    private String skuName;     //商品名称
    private Double skuPrice;    //商品单价
    private Integer totalNum;   //商品个数
    private Double totalPrice;  //商品总价
    private Enum skuCategory;   //枚举类型 商品类型

    public Sku(Integer skuId, String skuName, Double skuPrice, Integer totalNum, Double totalPrice, Enum skuCategory) {
        this.skuId = skuId;
        this.skuName = skuName;
        this.skuPrice = skuPrice;
        this.totalNum = totalNum;
        this.totalPrice = totalPrice;
        this.skuCategory = skuCategory;
    }

    public Integer getSkuId() {
        return skuId;
    }

    public String getSkuName() {
        return skuName;
    }

    public Double getSkuPrice() {
        return skuPrice;
    }

    public Integer getTotalNum() {
        return totalNum;
    }

    public Double getTotalPrice() {
        return totalPrice;
    }

    public Enum getSkuCategory() {
        return skuCategory;
    }
}

 

商品服务类:

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

    


}

 

枚举类:

package com.imooc.zhangxiaoxi.lambda.cart;

/**
 * 枚举类的使用
 */
public enum SkuCategoryEnum {

    CLOTHING(10,"服装类"),
    ELECTRONICS(20,"电子类"),
    SPROTS(30,"运动类"),
    BOOKS(40,"图书类");

    private Integer code;//码值
    private String name;//名称

    SkuCategoryEnum(Integer code, String name) {
        this.code = code;
        this.name = name;
    }
}

 

 

测试类:

package com.imooc.zhangxiaoxi.lambda.cart;

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

import java.util.List;

public class VersionTestA {

    @Test
    /**
     * 筛选出电子类型产品
     */
    public void getElectronicsSkuList(){
        List<Sku> skuList = CartService.getSkuList();
        List<Sku> electronicList = CartService.getElectronicsSkuList(skuList);
        System.out.println("electronicList=="+ JSON.toJSONString(electronicList,true));
    }
}

 

 

打印日志:

electronicList==[
    {
        "skuCategory":"ELECTRONICS",
        "skuId":2020001,
        "skuName":"无人机",
        "skuPrice":999.0,
        "totalNum":1,
        "totalPrice":999.0
    },
    {
        "skuCategory":"ELECTRONICS",
        "skuId":2020008,
        "skuName":"ThinkPad",
        "skuPrice":5000.0,
        "totalNum":1,
        "totalPrice":5000.0
    }
]

Process finished with exit code 0

 

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