Generic Classes

This commit is contained in:
Mark Zaharchenko 2022-10-16 22:40:07 +04:00
parent 1c11f7062c
commit 177e168498
2 changed files with 161 additions and 0 deletions

View File

@ -0,0 +1,112 @@
package com.example.doubledeckerbus;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import java.util.Collection;
public class MapWithSetBusesGeneric<T extends IDrawingObject, U extends AbstractMap> {
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 210;
private final int _placeSizeHeight = 90;
private final SetBusesGeneric<T> _setBuses;
private final U _map;
public MapWithSetBusesGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setBuses = new SetBusesGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public boolean add(MapWithSetBusesGeneric<T, U> map, T bus)
{
return map._setBuses.Insert(bus);
}
public boolean remove(MapWithSetBusesGeneric<T, U> map, int position)
{
return map._setBuses.Remove(position);
}
public void ShowSet(GraphicsContext gc)
{
DrawBackground(gc);
DrawBuses(gc);
}
public void ShowOnMap(GraphicsContext gc)
{
Shaking();
for (int i = 0; i < _setBuses.Count(); i++)
{
var bus = _setBuses.Get(i);
if (bus != null)
{
_map.CreateMap(_pictureWidth, _pictureHeight, bus, gc);
}
}
}
public void MoveObject(Direction direction)
{
if (_map != null)
{
_map.MoveObject(direction);
}
}
private void Shaking()
{
int j = _setBuses.Count() - 1;
for (int i = 0; i < _setBuses.Count(); i++)
{
if (_setBuses.Get(i) == null)
{
for (; j > i; j--)
{
var bus = _setBuses.Get(j);
if (bus != null)
{
_setBuses.Insert(bus, i);
_setBuses.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(GraphicsContext gc)
{
gc.setFill(Color.BLACK);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{//линия рамзетки места
gc.strokeLine(i * _placeSizeWidth, j * _placeSizeHeight,
i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
}
gc.strokeLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
private void DrawBuses(GraphicsContext gc)
{
for (int i = 0; i < _setBuses.Count(); i++)
{
// TODO установка позиции
_setBuses.Get(i).DrawingObject(gc);
}
}
}

View File

@ -0,0 +1,49 @@
package com.example.doubledeckerbus;
import java.util.Collection;
import java.util.Objects;
class SetBusesGeneric<T> {
private Object[] _places;
public SetBusesGeneric(int count) {
_places = new Object[count];
}
public boolean Insert(T car)
{
// TODO вставка в начало набора
return true;
}
public boolean Insert(T bus, int position)
{
// TODO проверка позиции
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
// проверка, что после вставляемого элемента в массиве есть пустой элемент
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
// TODO вставка по позиции
_places[position] = bus;
return true;
}
public boolean Remove(int position)
{
// TODO проверка позиции
// TODO удаление объекта из массива, присовив элементу массива значение null
return true;
}
public T Get(int position)
{
// TODO проверка позиции
return (T) _places[position];
}
public int Count() {
return _places.length;
}
}