Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class Race{}
- class MordorUnit extends Race{}
- class MiddleEarthUnit extends Race{}
- interface Unit<T> {}
- interface Infantry<T> extends Unit{}
- interface Cavalry<T> extends Unit{}
- public class Army<T extends Race> {
- private ArrayList<Cavalry<T>> cavalry = new ArrayList<>();
- private ArrayList<Infantry<T>> infantry = new ArrayList<>();
- private ArrayList<Unit> army = new ArrayList<>();
- public ArrayList<Cavalry<T>> getCavalry() {
- return cavalry;
- }
- public ArrayList<Infantry<T>> getInfantry() {
- return infantry;
- }
- public ArrayList<Unit> getArmy() {
- return army;
- }
- public boolean recruit(Unit<T> unit) {
- if (unit instanceof Infantry){
- army.add(unit);
- return infantry.add((Infantry) unit);
- }
- if (unit instanceof Cavalry){
- army.add(unit);
- return cavalry.add((Cavalry) unit);
- }
- return false;
- }
- public boolean release(Unit unit) {
- if (cavalry.contains(unit)){
- army.remove(unit);
- return cavalry.remove(unit);
- }
- if (infantry.contains(unit)){
- army.remove(unit);
- return infantry.remove(unit);
- }
- return false;
- }
- public void print() {
- army.stream().filter(x -> x instanceof Cavalry).forEach(x -> System.out.print(x.toString() + "\n"));
- army.stream().filter(x -> x instanceof Infantry).forEach(x -> System.out.print(x.toString() + "\n"));
- }
- public Unit getRandomUnit() {
- return army.get((int) (Math.random() * army.size()));
- }
- public Unit getRandomUnit(Unit unit) {
- if (unit instanceof Cavalry) cavalry.get((int) (Math.random() * cavalry.size()));
- if (unit instanceof Infantry) infantry.get((int) (Math.random() * infantry.size()));
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment