package me.ronggenliu.dp.construction.composite;

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

public class Leader implements Worker {

	private List<Worker> workers = new ArrayList<Worker>();
	private String name;
	
	public Leader(String name) {
		super();
		this.name = name;
	}
	
	public void add(Worker worker) {
		this.workers.add(worker);
	}
	
	public void remove(Worker worker) {
		this.workers.remove(worker);
	}
	
	public Worker get(int i) {
		return this.workers.get(i);
	}

	@Override
	public void doAction() {
		System.out.println(toString());
	}

	@Override
	public String toString() {
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("My name is " + this.name + ", I'm a leader, I have " + this.workers.size() + " subordinates. they are:\n");
		sBuilder.append("-------------\n");
		for(Worker worker: this.workers) {
			sBuilder.append(worker.toString() + "\n");
		}
		sBuilder.append("-------------\n");
		return sBuilder.toString();
	}
	
	
	
}

@@ -849,16 +849,18 @@
 -%5Cn%22);%0D%0A
+//
 %09%09for(Wo
@@ -889,16 +889,18 @@
 ers) %7B%0D%0A
+//
 %09%09%09sBuil
@@ -942,13 +942,93 @@
 );%0D%0A
+//
 %09%09%7D%0D%0A
+%09%09this.workers.forEach(worker -%3E sBuilder.append(worker.toString() + %22%5Cn%22));%0D%0A
 %09%09sB

use forEach in Java 8.