66 lines
2.0 KiB
Java
66 lines
2.0 KiB
Java
package CollectionGenericObjects;
|
|
|
|
import DifferenceOfWheels.IOrnaments;
|
|
import Drawnings.DrawningDumpTruck;
|
|
import Drawnings.DrawningTruck;
|
|
import Entities.EntityDumpTruck;
|
|
import Entities.EntityTruck;
|
|
|
|
import java.lang.reflect.Array;
|
|
import java.util.Random;
|
|
|
|
public class AdditionalCollection<T extends EntityTruck, U extends IOrnaments> {
|
|
public T[] _collectionEntityTruck;
|
|
public U[] _collectionIOrnaments;
|
|
public int Len_entity;
|
|
public int Len_ornament;
|
|
public AdditionalCollection(int len, Class<T> entity, Class<U> ornament){
|
|
_collectionEntityTruck = (T[]) Array.newInstance(entity, len);
|
|
_collectionIOrnaments = (U[]) Array.newInstance(ornament, len);
|
|
Len_entity = len;
|
|
Len_ornament = len;
|
|
}
|
|
|
|
public int Insert(T entity) {
|
|
int index = 0;
|
|
while (index < Len_entity) {
|
|
if (_collectionEntityTruck[index] == null)
|
|
{
|
|
_collectionEntityTruck[index] = entity;
|
|
return index;
|
|
}
|
|
++index;
|
|
}
|
|
return -1;
|
|
}
|
|
public int Insert(U ornament) {
|
|
int index = 0;
|
|
while (index < Len_ornament) {
|
|
if (_collectionIOrnaments[index] == null)
|
|
{
|
|
_collectionIOrnaments[index] = ornament;
|
|
return index;
|
|
}
|
|
++index;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public DrawningTruck CreateDrawningTruckClass(){
|
|
if (_collectionIOrnaments == null || _collectionEntityTruck == null){
|
|
return null;
|
|
}
|
|
DrawningTruck _drawningTruck;
|
|
Random random = new Random();
|
|
EntityTruck _truck = _collectionEntityTruck[random.nextInt(0, Len_entity)];
|
|
IOrnaments _ornament = _collectionIOrnaments[random.nextInt(0, Len_ornament)];
|
|
if (_truck instanceof EntityDumpTruck){
|
|
_drawningTruck = new DrawningDumpTruck((EntityDumpTruck) _truck, _ornament);
|
|
}
|
|
else{
|
|
_drawningTruck = new DrawningTruck(_truck, _ornament);
|
|
}
|
|
return _drawningTruck;
|
|
}
|
|
}
|