import java.util.Random; import java.util.ArrayList; public class ShipGenericDop { private ArrayList Ships; private ArrayList Decks; private int countShips; private int max_countShips; private int countDecks; private int max_countDecks; private Random rand; private int _pictureWidth; private int _pictureHeight; public ShipGenericDop(int _max_countShips, int _max_countDecks, int pictureWidth, int pictureHeight){ max_countShips = _max_countShips; max_countDecks = _max_countDecks; Ships = new ArrayList(max_countShips); Decks = new ArrayList(max_countDecks); countShips = 0; countDecks = 0; _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; rand = new Random(); } public boolean AddShip(T ship){ if(ship == null){ return false; } if (countShips > max_countShips){ return false; } Ships.add(countShips++, ship); return true; } public boolean AddDeck(U deck){ if(deck == null){ return false; } if (countDecks > max_countDecks){ return false; } Decks.add(countDecks++, deck); return true; } public DrawingShip getObjectRandomShip(){ if(countShips == 0 || countDecks == 0){ return null; } int i = rand.nextInt(countShips); int j = rand.nextInt(countDecks); if(Ships.get(i) instanceof EntityContainerShip){ return new DrawingContainerShip((EntityContainerShip)Ships.get(i), (IDecksDrawing)Decks.get(j), _pictureWidth, _pictureHeight); } else{ return new DrawingShip((EntityShip)Ships.get(i),_pictureWidth, _pictureHeight); } } }