Efficient farewell 996, opening the door 2-3 java programming efficient combat: hard coded business logic

1.1 Key

1.2 Programming Code

 

 

1.1 Key

Use enumeration class (SkuCategoryEnum);

Using (test class) JSONtoString method;

Two different list of use (class of goods and services);

list of add method

 

 

1.2 Programming Code

demand:

11 double programmers add a lot to the baby cart, programmers female friends basin began to filter out all of the electronic type products

 

Product entity classes:

Package Penalty for com.imooc.zhangxiaoxi.lambda.cart; 

/ ** 
 * commodity 
 * / 
public  class Sku {
     Private Integer skuId;       // Product Number 
    Private String skuName;      // Product Name 
    Private Double skuPrice;     // commodity price 
    Private Integer totalnum ;    // number of the item 
    Private Double the totalPrice;   // total 
    Private the enum skuCategory;    // enumerated type product type 

    public Sku (skuId Integer, String skuName, skuPrice Double, Integer totalnum, Double the totalPrice, the 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;
    }

    publicInteger getTotalNum () {
         return totalNum; 
    } 

    Public Double given numerous price () {
         return total price; 
    } 

    Public Enum getSkuCategory () {
         return skuCategory; 
    } 
}

 

Goods class:

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 (2,020,004, "Old Man", 20.00,1,20.00 , SkuCategoryEnum.BOOKS)); 
            the Add ( new new Sku (2020005, "prove safety and efficient programming" 288.00,1,288.00 , SkuCategoryEnum.BOOKS)); 
            the Add ( new new Sku (2020006, "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 () .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;
    }

    


}

 

Enumeration class:

Package com.imooc.zhangxiaoxi.lambda.cart; 

/ ** 
 * using enumeration class 
 * / 
public  enum SkuCategoryEnum { 

    CLOTHING ( 10, "clothing" ), 
    ELECTRONICS ( 20 is, "electronics" ), 
    Sprots ( 30, "sports" ), 
    the BOOKS ( 40, "book type" ); 

    Private Integer code; // code value 
    Private String name; // name 

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

 

 

Test categories:

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

 

 

Print log:

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

 

Guess you like

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