package jp.ne.naokiur.design.pattern.composite;

import java.util.ArrayList;
import java.util.List;

public abstract class Atom {
    private String elementSynbol;

    protected List<Atom> bonds;

    public Atom(String elementSynbol) {
        this.bonds = new ArrayList<Atom>();
        this.elementSynbol = elementSynbol;
    }

    public String getElementSynbol() {
        return elementSynbol;
    }

    public String createChemicalFormula() {
        String result = this.elementSynbol;
        for (Atom atom : bonds) {
            result = result + atom.createChemicalFormula();
        }


        return result;
    }
}

@@ -87,20 +87,68 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Apubli
@@ -490,24 +490,26 @@
 Formula() %7B%0A
+//
         Stri
@@ -552,104 +552,1372 @@
     
-for (Atom atom : bonds) %7B%0A            result = result + atom.createChemicalFormula();%0A        %7D%0A
+Map%3CString, Integer%3E elementCount = new HashMap%3C%3E();%0A        elementCount.put(this.elementSynbol, 1);%0A%0A        for (Atom atom : bonds) %7B%0A            Map%3CString, Integer%3E bondElementCount = atom.countElement();%0A%0A            for (Map.Entry%3CString, Integer%3E entry : bondElementCount.entrySet()) %7B%0A                if (elementCount.containsKey(entry.getKey())) %7B%0A                    Integer count = elementCount.get(entry.getKey()) + entry.getValue();%0A                    elementCount.put(entry.getKey(), count);%0A                %7D else %7B%0A                    elementCount.put(entry.getKey(), 1);%0A%0A                %7D%0A            %7D%0A        %7D%0A%0A        String result = %22%22;%0A        for (Map.Entry%3CString, Integer%3E element : elementCount.entrySet()) %7B%0A            result = result + element.getKey() + element.getValue();%0A        %7D%0A%0A        return result;%0A    %7D%0A%0A    private Map%3CString, Integer%3E countElement() %7B%0A        Map%3CString, Integer%3E elementCount = new HashMap%3C%3E();%0A        elementCount.put(this.elementSynbol, 1);%0A        for (Atom atom : bonds) %7B%0A            if (elementCount.containsKey(atom.getElementSynbol())) %7B%0A                Integer count = elementCount.get(atom.getElementSynbol()) + 1;%0A                elementCount.put(atom.getElementSynbol(), count);%0A            %7D else %7B%0A                elementCount.put(atom.getElementSynbol(), 1);%0A%0A            %7D%0A        %7D
 %0A%0A  
@@ -1921,32 +1921,38 @@
 %0A        return 
-resul
+elementCoun
 t;%0A    %7D%0A%7D%0A

Fix createChemicalFormula().