Efficient farewell 996, opening the door 2-6 java programming efficient combat: determination logic parametric - Entity Class

1.1 Key

1.2 Programming Code

 

 

1.1 Key

A new way of thinking: You can take a look at demo (Predicate predicate interface does not recommend using this method in large quantities, easy class explosion )

 

 

1.2 Programming Code

demand:

Incoming any of the parameters, you can get the corresponding results (programming only write the kinds of goods and the total price, to add other words, a straightforward extension of the predicate class interface can)

 

Commodity same entity class :( 2-3)

:( same enumeration class 2-3)

 

Goods class :( see filterSkuListByParamPrediCate method)

Package Penalty for com.imooc.zhangxiaoxi.lambda.cart; Import java.util.ArrayList; Import java.util.List; public class CartService { / ** * anonymous inner classes, there is a memory leak risk, not production projects referenced, but to back up the lambda expression references * / Private static List <Sku> = skuList new new the ArrayList <Sku> () {{the Add ( new new Sku (2020001, "UAV", 999.00,1,999.00 , SkuCategoryEnum.ELECTRONICS)); the Add ( new new Sku (2,020,002, "T-shirt", 50.00,2,100.00 , SkuCategoryEnum.CLOTHING)); the Add ( new new Sku (2020003, "of Human Bondage", 30.00,1,30.00 , SkuCategoryEnum.BOOKS)); the Add ( new new Sku (2020004, "Old Man", 20.00,1,20.00 , SkuCategoryEnum.BOOKS)); the Add (

new Sku (2020005, "prove safety and efficient programming", 288.00,1,288.00 , SkuCategoryEnum.BOOKS)); the Add ( new new Sku (2,020,006, "bulk shoes", 300.00,1,300.00 , SkuCategoryEnum.CLOTHING)); the Add ( new new Sku (2020007 "barbell", 2000.00,1,2000.00 , SkuCategoryEnum.SPROTS)); the Add ( new new Sku (2020008, "the ThinkPad", 5000.00,1,5000.00 , SkuCategoryEnum.ELECTRONICS));}}; public static List <Sku> getSkuList () { return skuList;} / ** * * versionTestA find the shopping cart all electronic products * * Note for loop * for (Sku sku: skuList) using * @return * / public static List <Sku> getElectronicsSkuList (List <Sku> skuList) {List <Sku> = lElectronicsList new new the ArrayList <Sku>(); for(SKU Sku: skuList) { IF (. Sku.getSkuCategory () the equals (SkuCategoryEnum.ELECTRONICS)) {lElectronicsList.add (SKU);}} / * for (int I = 0; I <skuList.size (); I ++ ) {IF (skuList.get (I) .getSkuCategory () the equals (SkuCategoryEnum.ELECTRONICS)) {lElectronicsList.add (skuList.get (I));.}} * / return lElectronicsList;} / ** * * versionTestB single dimensions condition parameters of the incoming different enum, print out a list of different products * * Note that the second argument: how the transfer of enumeration class parameters * * * @param skuList * @return * / public static list <Sku> filterSkuListByParam (List <Sku> skuList, skuCategoryEnum skuCategoryEnum) {List <Sku> = lElectronicsList new new the ArrayList <Sku> (); for (0 = I int; I <skuList.size (); I ++ ) { IF (skuList.get (I) .getSkuCategory () the equals (skuCategoryEnum)) {lElectronicsList.add (skuList.get (I));.}} return lElectronicsList;} / ** * versionTestC * * * egg and then a method may be one of the parameters is determined according to two parameters, * * If flag is true, enumerated type is determined, otherwise according to the total price is determined * * @param skuList * @return * / public static List <Sku> filterSkuListByParamB (List <Sku> skuList, skuCategoryEnum skuCategoryEnum, dTotalPrice Double, Boolean In Flag) {List <Sku> = lElectronicsList new new the ArrayList <Sku> (); for ( int I = 0; I <skuList.size (); I ++ ) { IF ((&& In Flag. skuList.get (I) .getSkuCategory () the equals (skuCategoryEnum)) || (! && skuList.get In Flag (I) .getTotalPrice ()> dTotalPrice)) {lElectronicsList.add (skuList.get (I));}} return lElectronicsList;} / ** * * versionTestD such an approach is not recommended for production, the class will cause an explosion. All to the lambda expression is introduced * * @param skuList * @return * / public static List <Sku> filterSkuListByParamPrediCate (List <Sku> skuList, SkuPrediCate skuPrediCate) {List <Sku> = lElectronicsList new new the ArrayList <Sku> (); for ( int I = 0; I <skuList.size (); I ++ ) { IF (skuPrediCate.test (skuList.get (I))) {lElectronicsList.add (skuList.get (I));lElectronicsList; }}

 

Predicate Interface:

Package com.imooc.zhangxiaoxi.lambda.cart; 

/ ** 
 * verb interfaces 
 * / 
public  interface SkuPrediCate { 

    Boolean Test (Sku SKU); 

}

 

Predicate achieve Total categories:

Package com.imooc.zhangxiaoxi.lambda.cart; 

public  class TotalPricePrediCate implements SkuPrediCate { 
    @Override 
    public  boolean test (Sku sku) {
         return sku.getTotalPrice ()> 200.00? true : false ; 
    } 
}

 

Predicates to achieve the kind of merchandise categories:

package com.imooc.zhangxiaoxi.lambda.cart;

public class CategoryPrediCate implements SkuPrediCate {
    @Override
    public boolean test(Sku sku) {
        return SkuCategoryEnum.BOOKS.equals(sku.getSkuCategory())?true:false;
    }
}

 

Test categories:

package com.imooc.zhangxiaoxi.lambda.cart;

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

import java.util.List;

public class VersionTestD {

    @Test
   public void filterSkuListByParam(){
       List<Sku> skuList = CartService.getSkuList();

       /*List<Sku> getParamSkuList = CartService.filterSkuListByParamPrediCate(skuList,new TotalPricePrediCate());
        System.out.println(JSON.toJSONString(getParamSkuList,true));*/

        List<Sku> getParamSkuList = CartService.filterSkuListByParamPrediCate(skuList,new CategoryPrediCate());
        System.out.println(JSON.toJSONString(getParamSkuList,true));
    }

}

 

Print log:

[
    {
        "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 " ,
        " skuName ":" to prove safety and efficient programming ",
        " skuId ": 2020005,
        "skuPrice":288.0,
        "totalNum":1,
        "totalPrice":288.0
    }
]

Process finished with exit code 0

 

Guess you like

Origin www.cnblogs.com/1446358788-qq/p/12610132.html