}
package punic;
public class Archer implements Warrior {
@Override
public int returnStrength() {
return 2;
}
}
package punic;
public class Horseman implements Warrior {
@Override
public int returnStrength() {
return 3;
}
}
package punic;
public class Infantryman implements Warrior {
@Override
public int returnStrength() {
return 1;
}
}
package punic;
public interface Warrior {
int returnStrength();
}
package punic;
import java.util.ArrayList;
import java.util.Iterator;
public class Proxy implements Warrior {
ArrayList<Warrior> Army=new ArrayList<Warrior>();
void addUnit(Warrior p){
Army.add(p);
}
public int returnStrength(){
int total = 0;
for (Iterator<Warrior> i = Army.iterator(); i.hasNext();) {
total+=i.next().returnStrength();
}
return total;
}
static public Proxy CreateLegion(){
Proxy legion = new Proxy();
for (int i=0; i<3000; ++i) legion.addUnit(new Infantryman());
for (int i=0; i<1200; ++i) legion.addUnit(new Archer());
for (int i=0; i<300; ++i) legion.addUnit(new Horseman());
return legion;
}
}
package punic;
import static punic.Proxy.CreateLegion;
public class Game {
public static void main(String[] args){
Proxy nArmy = new Proxy();
for (int i=0; i<3; ++i) nArmy.addUnit(CreateLegion());
System.out.println("Roman army has strength " + nArmy.returnStrength());
}
}