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]