From 177e1684984d3671075fc9997ea908e5bada4d58 Mon Sep 17 00:00:00 2001 From: Mark Zaharchenko Date: Sun, 16 Oct 2022 22:40:07 +0400 Subject: [PATCH 1/3] 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; + } +} + + -- 2.25.1 From 9ba224ac0dc18ba7aeddfa44ef7a3f9e5228091a Mon Sep 17 00:00:00 2001 From: Mark Zaharchenko Date: Mon, 17 Oct 2022 17:23:34 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=BE=D1=81?= =?UTF-8?q?=20=D0=B8=D0=B7=203=20=D0=B1=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../doubledeckerbus/ControllerBus.java | 54 +++++- .../doubledeckerbus/ControllerMap.java | 124 ------------ .../ControllerMapWithSetBus.java | 179 ++++++++++++++++++ .../example/doubledeckerbus/DrawingBus.java | 1 + .../com/example/doubledeckerbus/Form.java | 11 +- .../MapWithSetBusesGeneric.java | 44 +++-- .../doubledeckerbus/SetBusesGeneric.java | 39 ++-- .../example/doubledeckerbus/SimpleMap.java | 2 +- .../com/example/doubledeckerbus/FormBus.fxml | 5 +- .../com/example/doubledeckerbus/FormMap.fxml | 78 -------- .../doubledeckerbus/FormMapWithSetBus.fxml | 95 ++++++++++ 11 files changed, 392 insertions(+), 240 deletions(-) delete mode 100644 DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMap.java create mode 100644 DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java delete mode 100644 DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMap.fxml create mode 100644 DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java index 47f98da..07b4d56 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java @@ -5,23 +5,31 @@ import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; -import javafx.scene.control.Button; -import javafx.scene.control.ChoiceBox; -import javafx.scene.control.Label; +import javafx.scene.control.*; import javafx.scene.layout.AnchorPane; import javafx.scene.paint.Color; +import java.io.IOException; +import java.util.Optional; import java.util.Random; +import static com.example.doubledeckerbus.ControllerMapWithSetBus._mapBusesCollectionGeneric; + public class ControllerBus { protected DrawingBus _bus; + public DrawingBus SelectedBus; protected ObservableList countOfDoors = FXCollections.observableArrayList(3, 4, 5); @FXML protected Button buttonCreate; + @FXML + protected Button buttonSelectBus; + @FXML protected Button buttonDown; @@ -89,13 +97,15 @@ public class ControllerBus { public void initialize() { choiceDoors.setItems(countOfDoors); choiceDoors.setValue(3); - } + } InvalidationListener listener = o -> BorderChanged(); private void Draw() { GraphicsContext gc = canvasBus.getGraphicsContext2D(); + gc.setFill(Color.WHITE); + gc.fillRect(0, 0, pictureBoxBus.getWidth(), pictureBoxBus.getHeight()); _bus.DrawTransport(gc); } @@ -111,6 +121,8 @@ public class ControllerBus { pictureBoxBus.widthProperty().addListener(listener); pictureBoxBus.heightProperty().addListener(listener); Random rnd = new Random(); + ColorPicker colorPicker = new ColorPicker(); + ObservableList a = colorPicker.getCustomColors(); _bus = new DrawingDDB(rnd.nextInt(300), rnd.nextInt(2000), Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), choiceDoors.getValue(), @@ -119,4 +131,38 @@ public class ControllerBus { SetData(); BorderChanged(); } + @FXML + private void ButtonSelectBus_Click(ActionEvent event) throws IOException { + SelectedBus = _bus; + + if (SelectedBus == null) { + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle("SelectBus"); + alert.setContentText("Вы не создали объект"); + Optional option = alert.showAndWait(); + } + else { + + DrawingObjectBus bus = new DrawingObjectBus(SelectedBus); + + if (_mapBusesCollectionGeneric.add(bus) != -1) { + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle("SelectBus"); + alert.setContentText("Вы создали объект"); + Optional option = alert.showAndWait(); + } else { + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle("SelectBus"); + alert.setContentText("Не удалось добавить объект"); + Optional option = alert.showAndWait(); + } + } + + FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormMapWithSetBus.fxml")); + Scene scene = new Scene(fxmlLoader.load()); + Form.myStage.setTitle("DoubleDeckerBus"); + Form.myStage.setScene(scene); + Form.myStage.show(); + + } } diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMap.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMap.java deleted file mode 100644 index 41ff74a..0000000 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMap.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.example.doubledeckerbus; - -import javafx.collections.FXCollections; -import javafx.collections.ObservableList; -import javafx.event.ActionEvent; -import javafx.fxml.FXML; -import javafx.scene.canvas.Canvas; -import javafx.scene.canvas.GraphicsContext; -import javafx.scene.control.Button; -import javafx.scene.control.ChoiceBox; -import javafx.scene.control.Label; -import javafx.scene.layout.AnchorPane; -import javafx.scene.paint.Color; - -import java.util.Random; - -public class ControllerMap{ - private AbstractMap _abstractMap; - protected ObservableList countOfDoors = FXCollections.observableArrayList(3, 4, 5); - protected ObservableList countOfMap = FXCollections.observableArrayList("Простая карта", "Водная карта"); - - @FXML - private ChoiceBox choiceMap; - - @FXML - protected Button buttonCreate; - - @FXML - protected Button buttonDown; - - @FXML - protected Button buttonLeft; - - @FXML - protected Button buttonRight; - - @FXML - protected Button buttonUp; - - @FXML - protected Canvas canvasBus; - - @FXML - protected AnchorPane pictureBoxBus; - - @FXML - protected Label statusColor; - - @FXML - protected Label statusSpeed; - - @FXML - protected Label statusWeight; - - @FXML - protected ChoiceBox choiceDoors; - - @FXML - void ButtonCreate_Click(ActionEvent event) { - BorderChanged(); - Random rnd = new Random(); - var bus = new DrawingBus(rnd.nextInt(100, 300), rnd.nextFloat(1000, 2000), - Color.rgb(rnd.nextInt(256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), - choiceDoors.getValue()); - SetData(bus); - } - - @FXML - public void initialize() { - choiceDoors.setItems(countOfDoors); - choiceDoors.setValue(countOfDoors.get(0)); - choiceMap.setItems(countOfMap); - choiceMap.setValue(countOfMap.get(0)); - - _abstractMap = new SimpleMap(); - } - - @FXML - void ButtonCreateExtra_Click() { - canvasBus.setWidth(pictureBoxBus.getWidth()); - canvasBus.setHeight(pictureBoxBus.getHeight()); - Random rnd = new Random(); - var bus = new DrawingDDB(rnd.nextInt(300), rnd.nextInt(2000), - Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), - choiceDoors.getValue(), - Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), - rnd.nextBoolean(), rnd.nextBoolean()); - SetData(bus); - } - - @FXML - void ButtonMove_Click(ActionEvent event) { - String name = ((Button) event.getSource()).getId(); - Direction dir = Direction.None; - switch (name) { - case "buttonUp" -> dir = Direction.Up; - case "buttonDown" -> dir = Direction.Down; - case "buttonLeft" -> dir = Direction.Left; - case "buttonRight" -> dir = Direction.Right; - } - _abstractMap.MoveObject(dir); - } - - private void BorderChanged() { - canvasBus.setWidth(pictureBoxBus.getWidth()); - canvasBus.setHeight(pictureBoxBus.getHeight()); - } - - private void SetData(DrawingBus bus) - { - statusSpeed.setText("Скорость: %s".formatted(bus.Bus.Speed)); - statusWeight.setText("Вес: %s".formatted(bus.Bus.Weight)); - statusColor.setText("Цвет: %s".formatted(bus.Bus.BodyColor)); - GraphicsContext gc = canvasBus.getGraphicsContext2D(); - - switch (choiceMap.getValue()) { - case "Простая карта" -> _abstractMap = new SimpleMap(); - case "Водная карта" -> _abstractMap = new WaterMap(); - } - - _abstractMap.CreateMap((int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight(), - new DrawingObjectBus(bus), gc); - } -} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java new file mode 100644 index 0000000..9562343 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java @@ -0,0 +1,179 @@ +package com.example.doubledeckerbus; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.canvas.Canvas; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.control.*; +import javafx.scene.layout.AnchorPane; +import javafx.scene.paint.Color; + +import java.io.IOException; +import java.util.Objects; +import java.util.Optional; + + +public class ControllerMapWithSetBus { + static MapWithSetBusesGeneric _mapBusesCollectionGeneric; + AbstractMap map = new SimpleMap(); + static String map_name = "Простая карта"; + protected ObservableList countOfMap = FXCollections.observableArrayList("Простая карта", "Водная карта"); + + @FXML + private Button buttonAddBus; + + @FXML + private Button buttonDown; + + @FXML + private Button buttonLeft; + + @FXML + private Button buttonRemoveCar; + + @FXML + private Button buttonRight; + + @FXML + private Button buttonShowMap; + + @FXML + private Button buttonShowStorage; + + @FXML + private Button buttonUp; + + @FXML + private Canvas canvasBus; + + @FXML + private ChoiceBox comboBoxSelectorMap; + + @FXML + private AnchorPane pictureBoxBus; + + @FXML + private TextField textBoxPosition; + + @FXML + private void initialize(){ + comboBoxSelectorMap.setItems(countOfMap); + comboBoxSelectorMap.setValue(map_name); + } + + GraphicsContext gc; + private void FirstIncome() { + if (_mapBusesCollectionGeneric == null) { + _mapBusesCollectionGeneric = new MapWithSetBusesGeneric( + (int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight(), map); + } + gc = canvasBus.getGraphicsContext2D(); + if (comboBoxSelectorMap.getValue() != map_name) { + map_name = comboBoxSelectorMap.getValue(); + switch (map_name) { + case "Простая карта" -> map = new SimpleMap(); + case "Водная карта" -> map = new WaterMap(); + } + _mapBusesCollectionGeneric.changeMap(map); + } + } + @FXML + private void ButtonAddBus_Click(ActionEvent event) throws IOException { + FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormBus.fxml")); + Scene scene = new Scene(fxmlLoader.load()); + Form.myStage.setTitle("DoubleDeckerBus"); + Form.myStage.setScene(scene); + Form.myStage.show(); + + FirstIncome(); + } + + @FXML + private void ButtonRemoveBus_Click(ActionEvent event) + { + FirstIncome(); + if (Objects.equals(textBoxPosition.getText(), "")) + { + return; + } + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.setTitle("RemoveBus"); + alert.setHeaderText("Вы действительно хотите удалить объект?"); + + Optional option = alert.showAndWait(); + if (option.get() == ButtonType.CANCEL) { + return; + } + + int pos; + try { + pos = Integer.parseInt(textBoxPosition.getText()); + } + catch (Exception e) { + return; + } + + if (_mapBusesCollectionGeneric.remove(pos) != null) + { + alert = new Alert(Alert.AlertType.WARNING); + alert.setTitle("RemoveBus"); + alert.setContentText("Вы удалили объект"); + option = alert.showAndWait(); + } + else + { + alert = new Alert(Alert.AlertType.WARNING); + alert.setTitle("RemoveBus"); + alert.setContentText("Не удалось удалить объект"); + option = alert.showAndWait(); + } + } + + @FXML + private void ButtonShowStorage_Click(ActionEvent event) + { + FirstIncome(); + if (_mapBusesCollectionGeneric == null) + { + return; + } + gc.setFill(Color.WHITE); + gc.fillRect(0, 0, pictureBoxBus.getWidth(), pictureBoxBus.getHeight()); + _mapBusesCollectionGeneric.ShowSet(gc); + } + + @FXML + private void ButtonShowOnMap_Click(ActionEvent event) { + FirstIncome(); + if (_mapBusesCollectionGeneric == null) { + return; + } + gc.setFill(Color.WHITE); + gc.fillRect(0, 0, pictureBoxBus.getWidth(), pictureBoxBus.getHeight()); + _mapBusesCollectionGeneric.ShowOnMap(gc); + } + + @FXML + private void ButtonMove_Click(ActionEvent event) + { + if (_mapBusesCollectionGeneric == null) + { + return; + } + + String name = ((Button) event.getSource()).getId(); + Direction dir = Direction.None; + switch (name) { + case "buttonUp" -> dir = Direction.Up; + case "buttonDown" -> dir = Direction.Down; + case "buttonLeft" -> dir = Direction.Left; + case "buttonRight" -> dir = Direction.Right; + } + _mapBusesCollectionGeneric.MoveObject(dir); + } +} + + diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java index d64807d..cb372b3 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java @@ -88,6 +88,7 @@ public class DrawingBus { return; } + gc.setFill(Bus.BodyColor); gc.fillRect(_startPosX, _startPosY + 10, 100, 30); diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java index 83c9192..eb0b4d1 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java @@ -8,13 +8,16 @@ import javafx.stage.Stage; import java.io.IOException; public class Form extends Application { + static Stage myStage; + @Override public void start(Stage stage) throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormMap.fxml")); + FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormMapWithSetBus.fxml")); Scene scene = new Scene(fxmlLoader.load()); - stage.setTitle("DoubleDeckerBus"); - stage.setScene(scene); - stage.show(); + myStage = stage; + myStage.setTitle("DoubleDeckerBus"); + myStage.setScene(scene); + myStage.show(); } public static void main(String[] args) { diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java index 9948cc4..a94d632 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java @@ -11,7 +11,7 @@ public class MapWithSetBusesGeneric _setBuses; - private final U _map; + private U _map; public MapWithSetBusesGeneric(int picWidth, int picHeight, U map) { @@ -23,14 +23,18 @@ public class MapWithSetBusesGeneric map, T bus) - { - return map._setBuses.Insert(bus); + public void changeMap(U map) { + _map = map; } - public boolean remove(MapWithSetBusesGeneric map, int position) + public int add(T bus) { - return map._setBuses.Remove(position); + return _setBuses.Insert(bus); + } + + public T remove(int position) + { + return _setBuses.Remove(position); } public void ShowSet(GraphicsContext gc) @@ -100,12 +104,30 @@ public class MapWithSetBusesGeneric 0) + currentWidth--; + else { + currentWidth = width - 1; + currentHeight++; + } + if (currentHeight > height) return; } } } diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java index 21a2f15..c6e3c6c 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java @@ -6,39 +6,46 @@ import java.util.Objects; class SetBusesGeneric { private Object[] _places; + private int BusyPlaces = 0; public SetBusesGeneric(int count) { _places = new Object[count]; } - public boolean Insert(T car) + public int Insert(T bus) { - // TODO вставка в начало набора - return true; + return Insert(bus, 0); } - public boolean Insert(T bus, int position) + public int Insert(T bus, int position) { - // TODO проверка позиции - // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - // проверка, что после вставляемого элемента в массиве есть пустой элемент - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - // TODO вставка по позиции + if (position < 0 || position >= _places.length|| BusyPlaces == _places.length) return -1; + + BusyPlaces++; + while (_places[position] != null) { + for (int i = _places.length - 1; i > 0; --i) { + if (_places[i] == null && _places[i - 1] != null) { + _places[i] = _places[i - 1]; + _places[i - 1] = null; + } + } + } _places[position] = bus; - return true; + return position; } - public boolean Remove(int position) + public T Remove(int position) { - // TODO проверка позиции - // TODO удаление объекта из массива, присовив элементу массива значение null - return true; + if (position < 0 || position >= _places.length) return null; + T savedBus = (T)_places[position]; + _places[position] = null; + return savedBus; } public T Get(int position) { - // TODO проверка позиции - return (T) _places[position]; + if (position < 0 || position >= _places.length) return null; + return (T)_places[position]; } public int Count() { diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SimpleMap.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SimpleMap.java index 4d3305c..3c78f37 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SimpleMap.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SimpleMap.java @@ -18,7 +18,7 @@ public class SimpleMap extends AbstractMap{ } int counter = 0; - while (counter < 50) + while (counter < 20) { int x = _random.nextInt(0, 100); int y = _random.nextInt(0, 100); diff --git a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormBus.fxml b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormBus.fxml index 5bbdc23..49bbe9c 100644 --- a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormBus.fxml +++ b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormBus.fxml @@ -66,8 +66,9 @@ - - - - - + + + + + +