From 177e1684984d3671075fc9997ea908e5bada4d58 Mon Sep 17 00:00:00 2001 From: Mark Zaharchenko Date: Sun, 16 Oct 2022 22:40:07 +0400 Subject: [PATCH] Generic Classes --- .../MapWithSetBusesGeneric.java | 112 ++++++++++++++++++ .../doubledeckerbus/SetBusesGeneric.java | 49 ++++++++ 2 files changed, 161 insertions(+) create mode 100644 DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java create mode 100644 DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java new file mode 100644 index 0000000..9948cc4 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java @@ -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 { + private final int _pictureWidth; + private final int _pictureHeight; + private final int _placeSizeWidth = 210; + private final int _placeSizeHeight = 90; + private final SetBusesGeneric _setBuses; + private final U _map; + + public MapWithSetBusesGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setBuses = new SetBusesGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public boolean add(MapWithSetBusesGeneric map, T bus) + { + return map._setBuses.Insert(bus); + } + + public boolean remove(MapWithSetBusesGeneric 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); + } + } +} + diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java new file mode 100644 index 0000000..21a2f15 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java @@ -0,0 +1,49 @@ +package com.example.doubledeckerbus; + +import java.util.Collection; +import java.util.Objects; + + +class SetBusesGeneric { + 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; + } +} + +