package com.kduda.battleships.models.units;

import java.util.ArrayList;
import java.util.Iterator;

public enum UnitFactory {
    INSTANCE;
    ArrayList<Unit> units;
    Iterator<Unit> unitsIterator;

    UnitFactory() {
        initializeUnitsList();
    }

    void initializeUnitsList() {
        units = new ArrayList<>();
        units.add(createGroundLevelUnit(UnitType.Ship, 4));

        units.add(createGroundLevelUnit(UnitType.Ship, 3));
        units.add(createGroundLevelUnit(UnitType.Ship, 3));

        units.add(createGroundLevelUnit(UnitType.Ship, 2));
        units.add(createGroundLevelUnit(UnitType.Ship, 2));
        units.add(createGroundLevelUnit(UnitType.Ship, 2));

        units.add(createGroundLevelUnit(UnitType.Ship, 1));
        units.add(createGroundLevelUnit(UnitType.Ship, 1));
        units.add(createGroundLevelUnit(UnitType.Ship, 1));

        units.add(createGroundLevelUnit(UnitType.Tank, 4));

        units.add(createGroundLevelUnit(UnitType.Tank, 3));
        units.add(createGroundLevelUnit(UnitType.Tank, 3));

        units.add(createGroundLevelUnit(UnitType.Tank, 2));
        units.add(createGroundLevelUnit(UnitType.Tank, 2));
        units.add(createGroundLevelUnit(UnitType.Tank, 2));

        units.add(createPlane());
        units.add(createPlane());
        units.add(createPlane());

        unitsIterator = units.iterator();
    }

    Unit createGroundLevelUnit(UnitType type, int size) {
        switch (type) {
            case Ship:
                return new Ship(size);
            case Tank:
                return new Tank(size);
        }
        return null;
    }

    Unit createPlane() {
        return new Plane();
    }

    public Unit getNextUnit() {
        if (unitsIterator.hasNext())
            return unitsIterator.next();
        else
            return null;
    }
}

@@ -83,16 +83,20 @@
 va.util.
+List
 Iterator
@@ -169,16 +169,20 @@
 ts;%0A    
+List
 Iterator
@@ -1370,17 +1370,21 @@
 = units.
-i
+listI
 terator(
@@ -1850,11 +1850,178 @@
 l;%0A    %7D
+%0A%0A    public Unit getPreviousUnit() %7B%0A        if (unitsIterator.hasPrevious())%0A            return unitsIterator.previous();%0A        else%0A            return null;%0A    %7D
 %0A%7D%0A

Change iterator to list iterator