done дон done

This commit is contained in:
ValAnn 2023-12-09 14:20:11 +04:00
parent 4d5540d8e4
commit a7b27fc5d0
3 changed files with 29 additions and 33 deletions

View File

@ -60,7 +60,7 @@ public class CarCollectionFrame extends JFrame {
CarsGenericCollection<DrawingCar, DrawingObjectCar> _cars = _storage.Get(listBoxStoragesJList.getSelectedValue()); CarsGenericCollection<DrawingCar, DrawingObjectCar> _cars = _storage.Get(listBoxStoragesJList.getSelectedValue());
GameFrame form = new GameFrame(); GameFrame form = new GameFrame();
if (form.getSelectedCar() != null) { if (form.getSelectedCar() != null) {
if (_cars.plus(_cars, form.SelectedCar) != -1) { if (_cars.plus(_cars, form.SelectedCar)) {
JOptionPane.showMessageDialog(null, "Объект добавлен"); JOptionPane.showMessageDialog(null, "Объект добавлен");
} else { } else {

View File

@ -23,11 +23,11 @@ public class CarsGenericCollection<T extends DrawingCar, U extends IMoveableObje
_pictureHeight = picHeight; _pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height); _collection = new SetGeneric<T>(width * height);
} }
public int plus(CarsGenericCollection<T, U> collect, T obj) public boolean plus(CarsGenericCollection<T, U> collect, T obj)
{ {
if (obj == null) if (obj == null)
{ {
return -1; return false;
} }
return collect._collection.Insert(obj); return collect._collection.Insert(obj);
} }

View File

@ -1,56 +1,52 @@
package Generics; package Generics;
import java.util.ArrayList;
import java.util.List;
public class SetGeneric<T extends Object> { public class SetGeneric<T extends Object> {
private Object[] _places; private final List<T> _places;
private final int _maxCount;
public int Count;
public int Count() { public int Count() {
return _places.length; return Count;
} }
public SetGeneric(int count) public SetGeneric(int count)
{ {
_places = new Object[count]; _places = new ArrayList<>();
_maxCount = count;
} }
public int Insert(T car) public boolean Insert(T car){
{ if(_places.size() == _maxCount)
return Insert(car,0); return false;
Insert(car, 0);
return true;
} }
public int Insert(T car, int position) public boolean Insert(T monorail, int position){
{ if (!(position >= 0 && position <= _places.size() && _places.size() < _maxCount))
if (!(position >= 0 && position < Count())) return false;
return -1; _places.add(position, monorail);
if (_places[position] != null) Count++;
{ return true;
int indexEnd = position + 1;
while (_places[indexEnd] != null)
{
indexEnd++;
}
for (int i = indexEnd + 1; i > position; i--)
{
_places[i] = _places[i - 1];
}
}
_places[position] = car;
return position;
} }
public boolean Remove(int position) public boolean Remove(int position)
{ {
if (position < Count() && position >= 0) if(!(position >= 0 && position < _places.size()))
{ {
_places[position] = null; return false;
return true;
} }
return false; _places.remove(position);
Count--;
return true;
} }
public T Get(int position) public T Get(int position)
{ {
if (position < Count() && position >= 0) { return (T)_places[position]; } if (position < Count() && position >= 0) { return (T)_places.get(position); }
return null; return null;
} }
} }