/**
 * {@link <a href=
 * "https://[Log in to view URL]"
 * target= "_blank"></a>}
 * 
 * @author itammb ( Italia Massimiliano Buscati )
 * @version JDK 1.15
 *
 */

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

class Main {

	public static interface ComponentOrder {
		public Count execute();
	}

	public static class Count {
		public Count(double element, double weight) {
			this.element = element;
			this.weight = weight;
		}

		final private double element;
		final private double weight;

		public double getElement() {
			return element;
		}

		public double getWeight() {
			return weight;
		}
	}

	public static class LeafElement implements ComponentOrder {

		public LeafElement(Count count) {
			this.count = count;
		}

		private Count count;

		public Count execute() {
			return count;
		}
	}

	public static class CompositePackage implements ComponentOrder {

		public CompositePackage(List<ComponentOrder> children) {
			this.children = children;
		}

		private List<ComponentOrder> children;

		public boolean add(ComponentOrder componentOrder) {
			return children.add(componentOrder);
		}

		public boolean remove(ComponentOrder componentOrder) {
			return children.remove(componentOrder);
		}

		public ComponentOrder get(int index) {
			return children.get(index);
		}

		public Count execute() {
			double element = 0;
			double weight = 0;

			for (ComponentOrder order : children) {
				Count tmp = order.execute();
				element += tmp.element;
				weight += tmp.weight;
			}

			return new Count(element, weight);
		}
	}

	public static class Phone extends LeafElement {
		public Phone(Count count) {
			super(count);
		}
	}

	public static class HeadPhones extends LeafElement {
		public HeadPhones(Count count) {
			super(count);
		}
	}

	public static class Charger extends LeafElement {
		public Charger(Count count) {
			super(count);
		}
	}

	public static class ComplexOrder {
		public ComplexOrder(List<CompositePackage> allPackage) {
			this.allPackage = allPackage;
		}

		private List<CompositePackage> allPackage;

		public CompositePackage load() {
			CompositePackage newPackage = new CompositePackage(new ArrayList<ComponentOrder>());
			allPackage.add(newPackage);

			return newPackage;
		}

		public boolean fillPackage(CompositePackage box, ComponentOrder element) {
			return box.add(element);
		}

		public Count count() {
			double element = 0;
			double weight = 0;

			for (CompositePackage aPackage : allPackage) {
				Count tmp = aPackage.execute();
				element += tmp.element;
				weight += tmp.weight;
			}

			return new Count(element, weight);
		}
	}

	public static void main(String args[]) {
		// Unit test - uso del design pattern: composite

		ComplexOrder order = new ComplexOrder(new ArrayList<CompositePackage>());

		CompositePackage boxMobile = order.load();
		order.fillPackage(boxMobile, new Phone(new Count(1, 0.4)));
		order.fillPackage(boxMobile, new HeadPhones(new Count(1, 0.05)));
		CompositePackage boxCharger = order.load();
		order.fillPackage(boxCharger, new Charger(new Count(1, 0.2)));
		order.fillPackage(boxMobile, boxCharger);

		Count count = order.count();
		System.out.println("Tot. element: " + count.element + " -- Tot. wheight: " + count.weight);

	}
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: