Java recursive usage example

demand:

  • Direct investment ratio: Calculate the investment ratio of A unit to B, C...units (for example: A to B invests 80%)
  • Indirect capital contribution ratio: Calculate the indirect capital contribution ratio of Ba, Bb... of unit A's investment in unit B (for example, if B invests Ba 60%, then the calculation method of the investment ratio of A to Ba: 80% * 60%)
  • Indirect investment ratio: Calculate the indirect investment ratio of Ba1, Ba2... of the investment of A unit to Ba unit
  • ......

 Code demo:

 XML form (hierarchical relationship):

<Node unitTitle = "" unitCode ="" >
	<system_attrs>
            <attr code="" value=""/>
        </system_attrs>
	<VirtualNodeType/>
	<Node unitTitle = "" unitCode ="" >
		<system_attrs>
			<attr code="" value=""/>
		</system_attrs>
		<VirtualNodeType/>
		<Node>
		
		</Node>
		<Node>

		</Node>
	</Node>
	<Node>
		<Node>

		</Node>
		<Node>

		</Node>
	</Node>
</Node>
package com.xxx.xxx.xxx;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.xxx.runner.Runner;
import com.xxx.runner.RunnerParameter;
import com.xxx.core.Context;

/**
 * 出资比例数据提取计划任务
 * 
 */
public class BillStockRightScalePlanTaskRunner extends Runner {

	public static Map<String, BigDecimal> scaleMap = new HashMap<String, BigDecimal>();

	@Override
	public boolean excute(RunnerParameter param, Context context) {
		scaleMap.clear();
		getStockRightScale(context);
		return true;
	}

	/**
	 * 计算比例
	 * 
	 * @param context
	 */
	public static void getStockRightScale(Context context) {
		// 通过某方法调用得到对象 Tree tree
		String xml = tree.getXmlData();
		// 解析字符串
		Document document = null;
		try {
			document = DocumentHelper.parseText(xml);
			Element root = document.getRootElement();
			Map<Element,BigDecimal> sonMap = new HashMap<Element,BigDecimal>();
			Iterator<Element> nodeElement = root.elementIterator("Node");
			while (nodeElement.hasNext()) {
				Element second = (Element) nodeElement.next();
				String code = second.attributeValue("unitCode");
				Iterator attrs = second.elementIterator("system_attrs");
				while (attrs.hasNext()) {
					Element secondSystemAttrs = (Element) attrs.next();
					Iterator secondAttr = secondSystemAttrs.elementIterator("attr");
					while (secondAttr.hasNext()) {
						Element attrValue = (Element) secondAttr.next();
						BigDecimal scale = new BigDecimal(attrValue.attributeValue("value").substring(2));
						scaleMap.put(code, scale);
						sonMap.put(second,scale);
					}
				}
			}
			// 后续层级 递归
			countStockRightScale(sonMap);
//			Set<String> keySet = scaleMap.keySet();
//			for (String str : keySet) {
//				if(scaleMap.get(str).compareTo(new BigDecimal("100.00")) < 1){
//					//System.out.println(str+"**********"+scaleMap.get(str));
//				}
//			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}

/**
	 * 后续层级
	 * @param context
	 * @param sonMap
	 */
	private static void countStockRightScale(Map<Element, BigDecimal> sonMap) {
		Map<String, BigDecimal> tempScaleMap = new HashMap<String, BigDecimal>();
		Map<Element,BigDecimal> tempMap = new HashMap<Element,BigDecimal>();
		if(sonMap!= null && sonMap.size()>0){
			Set<Entry<Element,BigDecimal>> entrySet = sonMap.entrySet();
			for (Entry<Element, BigDecimal> entry : entrySet) {
				BigDecimal scale = entry.getValue();
				Element element = entry.getKey();
				Iterator<Element> nodeElement = element.elementIterator("Node");
				while (nodeElement.hasNext()) {
					Element tempElement = (Element) nodeElement.next();
					String code = tempElement.attributeValue("unitCode");
					Iterator attrs = tempElement.elementIterator("system_attrs");
					while (attrs.hasNext()) {
						Element secondSystemAttrs = (Element) attrs.next();
						Iterator secondAttr = secondSystemAttrs.elementIterator("attr");
						while (secondAttr.hasNext()) {
							Element attrValue = (Element) secondAttr.next();
							BigDecimal value = new BigDecimal(attrValue.attributeValue("value").substring(2));
							BigDecimal theScale = scale.divide(new BigDecimal("100")).multiply(value);
							if(scaleMap.get(code)!=null){
								tempScaleMap.put(code, scaleMap.get(code).add(theScale).setScale(2,BigDecimal.ROUND_HALF_UP));
							}else{
								tempScaleMap.put(code, theScale.setScale(2,BigDecimal.ROUND_HALF_UP));
							}
							tempMap.put(tempElement, theScale);
						}
					}
				}
			}
			scaleMap.putAll(tempScaleMap);
		}
		if(tempScaleMap.size()>0){
			countStockRightScale(tempMap);
		}
	}

}

 

Guess you like

Origin blog.csdn.net/xiangwang2016/article/details/103201476