64 lines
2.0 KiB
Java
64 lines
2.0 KiB
Java
package CollectionGenericObjects;
|
|
|
|
import Drawnings.CountWheels;
|
|
import Drawnings.DrawningCleaningCar;
|
|
import Drawnings.DrawningTruck;
|
|
import Drawnings.IDrawningWheels;
|
|
import Entities.EntityCleaningCar;
|
|
import Entities.EntityTruck;
|
|
|
|
import java.lang.reflect.Array;
|
|
import java.util.Random;
|
|
|
|
public class AdditionalCollection <T extends EntityTruck, U extends IDrawningWheels>{
|
|
private T[] _collectionEntity;
|
|
private U[] _collectionWheels;
|
|
public AdditionalCollection(int value, Class<T> type1, Class<T> type2) {
|
|
_collectionEntity = (T[]) Array.newInstance(type1, value);
|
|
_collectionWheels = (U[]) Array.newInstance(type2, value);
|
|
}
|
|
public int GetCountEntities() {
|
|
return _collectionEntity.length;
|
|
}
|
|
public int GetCountWheels() {
|
|
return _collectionWheels.length;
|
|
}
|
|
|
|
public int Insert(T entity) {
|
|
for (int i = 0; i < GetCountEntities(); i++)
|
|
{
|
|
if (_collectionEntity[i] == null)
|
|
{
|
|
_collectionEntity[i] = entity;
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
public int Insert(U decks) {
|
|
for (int i = 0; i < GetCountWheels(); i++)
|
|
{
|
|
if (_collectionWheels[i] == null)
|
|
{
|
|
_collectionWheels[i] = decks;
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
public DrawningTruck CreateFromAdditionalCollection() {
|
|
Random random = new Random();
|
|
if (_collectionEntity == null || _collectionWheels == null) return null;
|
|
T entity = _collectionEntity[random.nextInt(GetCountEntities())];
|
|
U wheels = _collectionWheels[random.nextInt(GetCountWheels())];
|
|
DrawningTruck drawingTruck = null;
|
|
if (entity instanceof EntityCleaningCar) {
|
|
drawingTruck = new DrawningCleaningCar((EntityCleaningCar) entity, wheels);
|
|
}
|
|
else {
|
|
drawingTruck = new DrawningTruck(entity, wheels);
|
|
}
|
|
return drawingTruck;
|
|
}
|
|
}
|