map父子维护的ebom父子部件关系计算组套数量
使用ebom时候,把部件父子关系和安装数量维护在了map上面,要计算组套数量,需要递归从当前往上取出父级安装数量相乘,一直取到最顶层。
G的组套数量:GDB=322=12
E的组套数量:ECB=232=12
C的组套数量:CB=32=6
B的组套数量:B=2
public class CalUtil {
/**
* 计算组套数量
* @param map key是部件, value是数组,第一个值时部件,第二个值是key的部件安装数量
* @param key
* @return
*/
public static int getAccUom(Map<String,String[]> map, String key){
int num = 1;
if(map.get(key)!=null){
num = Integer.valueOf(map.get(key)[1])*getAccUom(map,map.get(key)[0]);
}
return num;
}
public static void main(String[] args) {
Map<String, String[]> map = new HashMap<>();
map.put("B",new String[]{"A","2"});
map.put("C",new String[]{"B","3"});
map.put("D",new String[]{"B","2"});
map.put("E",new String[]{"C","2"});
map.put("F",new String[]{"C","3"});
map.put("G",new String[]{"D","3"});
System.out.println("G安装数量:"+ CalUtil.getAccUom(map,"G"));
System.out.println("E安装数量:"+ CalUtil.getAccUom(map,"E"));
System.out.println("F安装数量:"+ CalUtil.getAccUom(map,"F"));
System.out.println("D安装数量:"+ CalUtil.getAccUom(map,"D"));
System.out.println("C安装数量:"+ CalUtil.getAccUom(map,"C"));
System.out.println("B安装数量:"+ CalUtil.getAccUom(map,"B"));
}
}
执行结果:
G安装数量:12
E安装数量:12
F安装数量:18
D安装数量:4
C安装数量:6
B安装数量:2