sku算法

sku商品算法

package com.util.common.shop.sku;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.alibaba.fastjson.JSONObject;


public class Sku {
    
    private List<String> getColorList() {
        List<String> colorList = new ArrayList<String>();
        colorList.add("blue");
        colorList.add("yellow");
        colorList.add("red");
        colorList.add("green");
        colorList.add("black");
        return colorList;
    }
    
    private List<String> getCapacityList() {
        List<String> capacityList = new ArrayList<String>();
        capacityList.add("1L");
        capacityList.add("2L");
        capacityList.add("3L");
        capacityList.add("5L");
        capacityList.add("10L");
        return capacityList;
    }
    
    private List<String> getQualityList() {
        List<String> qualityList = new ArrayList<String>();
        qualityList.add("特等");
        qualityList.add("一等");
        qualityList.add("二等");
        return qualityList;
    }
    
    @SuppressWarnings("unchecked")
    public JSONObject getAttributeAndValue(int array[], Map<String, Object> skuMap) {
        JSONObject jsonObject = new JSONObject(new LinkedHashMap<>());
        int index = 0;
        for (Map.Entry<String, Object> entry : skuMap.entrySet()) {
            List<String> valueList = (List<String>) entry.getValue();
            jsonObject.put(entry.getKey() + "", valueList.get(array[index]));
            index++;
        }
        return jsonObject;
    }
    
	public static void main(String[] args) {
		Sku sku = new Sku();
		Map<String, Object> skuMap = new TreeMap<String, Object>();
		List<String> colorList = sku.getColorList();
		skuMap.put("color", colorList);
		
		List<String> capacityList = sku.getCapacityList();
		skuMap.put("capacity", capacityList);
		
		List<String> qualityList = sku.getQualityList();
		skuMap.put("quality", qualityList);
			
		int array[] = new int[]{colorList.size(), capacityList.size(), qualityList.size()};
		String[][] dealNumArray = sku.dealNumArray(array);
		String[] result = sku.doExchange(dealNumArray);
		
		for (String object : result) {
			String[] split = object.split("-");
			int[] arrayIndex = new int[split.length];
			for (int index = 0; index < split.length; index++) {
				arrayIndex[index] = Integer.valueOf(split[index]);
			}
			JSONObject attributeAndValue = sku.getAttributeAndValue(arrayIndex, skuMap);

			System.out.println(attributeAndValue.toJSONString());
		}
		
	}
	
	public String[][] dealNumArray(int valueLengthArray[]) {
		String[][] dealNumArray = new String[valueLengthArray.length][];
		for (int index = 0; index < valueLengthArray.length; index++) {
			String[] array = new String[valueLengthArray[index]];
			for (int choose = 0; choose < valueLengthArray[index]; choose++) {
				array[choose] = String.valueOf(choose);
			}
			dealNumArray[index] = array;
		}
		
		return dealNumArray;
	}
	
	/**
	 * [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2]]
	 * @param arr
	 * @return
	 */
	public String[] doExchange(String[][] arr) {
		int len = arr.length;
		// 当数组大于等于2个的时候
		if (len >= 2) {
			int len1 = arr[0].length;
			int len2 = arr[1].length;
			int lenBoth = len1 * len2;
			String[] items = new String[lenBoth];
			int index = 0;
			for (int i = 0; i < len1; i++) {
				for (int j = 0; j < len2; j++) {
					items[index] = arr[0][i] + "-" + arr[1][j];
					index++;
				}
			}
			// 将新组合的数组并到原数组中
			String newArr[][] = new String[len - 1][];
			for (int i = 2; i < arr.length; i++) {
				newArr[i - 1] = arr[i];
			}
			newArr[0] = items;
			// 执行回调
			return doExchange(newArr);
		} else {
			return arr[0];
		}
	}
}

说明:

第一步
[5, 5, 3]

第二步
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2]]

第三步
[0-0, 0-1, 0-2, 0-3, 0-4, 1-0, 1-1, 1-2, 1-3, 1-4, 2-0, 2-1, 2-2, 2-3, 2-4, 3-0, 3-1, 3-2, 3-3, 3-4, 4-0, 4-1, 4-2, 4-3, 4-4]

第四步
[[0-0, 0-1, 0-2, 0-3, 0-4, 1-0, 1-1, 1-2, 1-3, 1-4, 2-0, 2-1, 2-2, 2-3, 2-4, 3-0, 3-1, 3-2, 3-3, 3-4, 4-0, 4-1, 4-2, 4-3, 4-4], [0, 1, 2]]

第五步
[0-0-0, 0-0-1, 0-0-2, 0-1-0, 0-1-1, 0-1-2, 0-2-0, 0-2-1, 0-2-2, 0-3-0, 0-3-1, 0-3-2, 0-4-0, 0-4-1, 0-4-2,
1-0-0, 1-0-1, 1-0-2, 1-1-0, 1-1-1, 1-1-2, 1-2-0, 1-2-1, 1-2-2, 1-3-0, 1-3-1, 1-3-2, 1-4-0, 1-4-1, 1-4-2,
2-0-0, 2-0-1, 2-0-2, 2-1-0, 2-1-1, 2-1-2, 2-2-0, 2-2-1, 2-2-2, 2-3-0, 2-3-1, 2-3-2, 2-4-0, 2-4-1, 2-4-2,
3-0-0, 3-0-1, 3-0-2, 3-1-0, 3-1-1, 3-1-2, 3-2-0, 3-2-1, 3-2-2, 3-3-0, 3-3-1, 3-3-2, 3-4-0, 3-4-1, 3-4-2,
4-0-0, 4-0-1, 4-0-2, 4-1-0, 4-1-1, 4-1-2, 4-2-0, 4-2-1, 4-2-2, 4-3-0, 4-3-1, 4-3-2, 4-4-0, 4-4-1, 4-4-2]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值