LabWork8 Zacharchenko PIbd-21 #12

Closed
shadowik wants to merge 1 commits from LabWork8 into LabWork7
7 changed files with 283 additions and 11 deletions

View File

@ -0,0 +1,76 @@
package com.example.doubledeckerbus;
import java.util.Comparator;
public class BusComparatorByColor implements Comparator<IDrawingObject> {
@Override
public int compare(IDrawingObject o1, IDrawingObject o2) {
if (o1 == null && o2 == null)
{
return 0;
}
if (o1 == null)
{
return 1;
}
if (o2 == null)
{
return -1;
}
if (!(o1 instanceof DrawingObjectBus bus1) || !(o2 instanceof DrawingObjectBus bus2))
{
if (!(o1 instanceof DrawingObjectBus) && !(o2 instanceof DrawingObjectBus))
{
return 0;
}
else if (!(o1 instanceof DrawingObjectBus))
{
return 1;
}
else
{
return -1;
}
}
String col1 = bus1.getBus().Bus.getBodyColor().toString();
String col2 = bus2.getBus().Bus.getBodyColor().toString();
var col = col1.compareTo(col2);
if (col != 0)
{
return col;
}
if (bus1.getBus().Bus instanceof EntityDDB DDB1 && bus2.getBus().Bus instanceof EntityDDB DDB2)
{
String extra1 = DDB1.ExtraColor.toString();
String extra2 = DDB2.ExtraColor.toString();
var extra = extra1.compareTo(extra2);
if (extra != 0)
{
return extra;
}
}
String doors1 = bus1.getBus().Doors.getClass().getSimpleName();
String doors2 = bus2.getBus().Doors.getClass().getSimpleName();
var res = doors1.compareTo(doors2);
if (res != 0)
{
return res;
}
if (bus1.getBus()._countOfDoors - bus2.getBus()._countOfDoors != 0)
{
return bus1.getBus()._countOfDoors - bus2.getBus()._countOfDoors;
}
if (bus1.getBus()._speed - bus2.getBus()._speed != 0)
{
return bus1.getBus()._speed - bus2.getBus()._speed;
}
return (int) (bus1.getBus()._weight - bus2.getBus()._weight);
}
}

View File

@ -0,0 +1,65 @@
package com.example.doubledeckerbus;
import java.util.Comparator;
public class BusComparatorByType implements Comparator<IDrawingObject> {
@Override
public int compare(IDrawingObject o1, IDrawingObject o2) {
if (o1 == null && o2 == null)
{
return 0;
}
if (o1 == null)
{
return 1;
}
if (o2 == null)
{
return -1;
}
if (!(o1 instanceof DrawingObjectBus bus1) || !(o2 instanceof DrawingObjectBus bus2))
{
if (!(o1 instanceof DrawingObjectBus) && !(o2 instanceof DrawingObjectBus))
{
return 0;
}
else if (!(o1 instanceof DrawingObjectBus))
{
return 1;
}
else
{
return -1;
}
}
if (!bus1.getBus().getClass().getSimpleName().equals(bus2.getBus().getClass().getSimpleName()))
{
if (bus1.getBus().getClass().getSimpleName().equals("DrawingBus"))
{
return -1;
}
return 1;
}
String doors1 = bus1.getBus().Doors.getClass().getSimpleName();
String doors2 = bus2.getBus().Doors.getClass().getSimpleName();
var res = doors1.compareTo(doors2);
if (res != 0)
{
return res;
}
if (bus1.getBus()._countOfDoors - bus2.getBus()._countOfDoors != 0)
{
return bus1.getBus()._countOfDoors - bus2.getBus()._countOfDoors;
}
if (bus1.getBus()._speed - bus2.getBus()._speed != 0)
{
return bus1.getBus()._speed - bus2.getBus()._speed;
}
return (int) (bus1.getBus()._weight - bus2.getBus()._weight);
}
}

View File

@ -161,6 +161,10 @@ public class ControllerMapWithSetBus {
_logger.warn("StorageOverFlow");
alert = new Alert(Alert.AlertType.ERROR, "Хранилище переполнено");
}
catch (IllegalArgumentException e) {
_logger.warn("The same bus");
alert = new Alert(Alert.AlertType.ERROR, "Такой автобус был");
}
showStorage();
alert.showAndWait();
}
@ -285,6 +289,38 @@ public class ControllerMapWithSetBus {
_logger.info("show storage");
}
@FXML
private void ButtonSortByType_Click(ActionEvent event)
{
if (listViewMaps.getSelectionModel().getSelectedIndex() == -1)
{
return;
}
String selectedMapName = listViewMaps.getSelectionModel().getSelectedItem();
if (selectedMapName != null)
{
_mapsCollection.get(selectedMapName).Sort(new BusComparatorByType());
_mapsCollection.get(selectedMapName).ShowSet(gc);
showStorage();
}
}
@FXML
private void ButtonSortByColor_Click(ActionEvent event)
{
if (listViewMaps.getSelectionModel().getSelectedIndex() == -1)
{
return;
}
String selectedMapName = listViewMaps.getSelectionModel().getSelectedItem();
if (selectedMapName != null)
{
_mapsCollection.get(selectedMapName).Sort(new BusComparatorByColor());
_mapsCollection.get(selectedMapName).ShowSet(gc);
showStorage();
}
}
@FXML
private void ButtonShowOnMap_Click(ActionEvent event) throws StorageOverflowException, BusNotFoundException {
FirstIncome();

View File

@ -2,6 +2,8 @@ package com.example.doubledeckerbus;
import javafx.scene.canvas.GraphicsContext;
import java.util.Objects;
public class DrawingObjectBus implements IDrawingObject {
private DrawingBus _bus = null;
@ -52,4 +54,53 @@ public class DrawingObjectBus implements IDrawingObject {
public static IDrawingObject Create(String data) {
return new DrawingObjectBus(ExtensionBus.CreateDrawingBus(data));
}
@Override
public boolean equals(Object obj) {
if (obj == null)
{
return false;
}
var otherBus = (DrawingObjectBus) obj;
if (otherBus == null)
{
return false;
}
var bus = _bus.Bus;
var otherBusBus = otherBus._bus.Bus;
if (bus.Speed != otherBusBus.Speed)
{
return false;
}
if (bus.Weight != otherBusBus.Weight)
{
return false;
}
if (!Objects.equals(bus.getBodyColor().toString(), otherBusBus.getBodyColor().toString()))
{
return false;
}
if (!_bus.Doors.getClass().toString().equals(otherBus._bus.Doors.getClass().toString())) {
return false;
}
if (_bus._countOfDoors != otherBus._bus._countOfDoors) {
return false;
}
if (bus instanceof EntityDDB DDB && otherBusBus instanceof EntityDDB otherDDB)
{
if (DDB.Ladder != otherDDB.Ladder) {
return false;
}
if (DDB.SecondStage != otherDDB.SecondStage) {
return false;
}
return Objects.equals(DDB.ExtraColor.toString(), otherDDB.ExtraColor.toString());
}
return true;
}
}

View File

@ -3,9 +3,10 @@ package com.example.doubledeckerbus;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import java.util.Comparator;
import java.util.Stack;
public class MapWithSetBusesGeneric<T extends IDrawingObject, U extends AbstractMap> {
public class MapWithSetBusesGeneric<T extends IDrawingObject , U extends AbstractMap> implements Comparable<MapWithSetBusesGeneric>{
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 210;
@ -170,5 +171,35 @@ public class MapWithSetBusesGeneric<T extends IDrawingObject, U extends Abstract
public void Clear() {
_setBuses.Clear();
}
@Override
public int compareTo(MapWithSetBusesGeneric o) {
if (o == null)
{
return 1;
}
if (this == o)
{
return 0;
}
var pictureWidthCompare = Integer.compare(_pictureWidth, o._pictureWidth);
if (pictureWidthCompare != 0)
{
return pictureWidthCompare;
}
var pictureHeightCompare = Integer.compare(_pictureHeight, o._pictureHeight);
if (pictureHeightCompare != 0)
{
return pictureHeightCompare;
}
Integer setCount = _setBuses.Count();
Integer otherSetCount= o._setBuses.Count();
return setCount.compareTo(otherSetCount);
}
public void Sort(Comparator<T> comparator)
{
_setBuses.Sort(comparator);
}
}

View File

@ -1,9 +1,6 @@
package com.example.doubledeckerbus;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.*;
class SetBusesGeneric<T> {
@ -22,6 +19,10 @@ class SetBusesGeneric<T> {
}
public int Insert(T bus, int position) throws StorageOverflowException {
if (_places.contains(bus))
{
throw new IllegalArgumentException("The same bus exist");
}
if (_places.size() == _maxCount)
{
throw new StorageOverflowException(_places.size());
@ -35,6 +36,7 @@ class SetBusesGeneric<T> {
}
public T Remove(int position) throws BusNotFoundException {
if (position < 0 || position >= _maxCount)
throw new BusNotFoundException();
T savedBus = _places.get(position - 1);
@ -69,6 +71,13 @@ class SetBusesGeneric<T> {
public void Clear() {
_places.clear();
}
public void Sort(Comparator<T> comparator) {
if (comparator == null) {
return;
}
_places.sort(comparator);
}
}

View File

@ -14,7 +14,7 @@
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="745.0" prefWidth="819.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.doubledeckerbus.ControllerMapWithSetBus">
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="848.0" prefWidth="930.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.doubledeckerbus.ControllerMapWithSetBus">
<columnConstraints>
<ColumnConstraints hgrow="ALWAYS" maxWidth="463.0" minWidth="10.0" percentWidth="70.0" prefWidth="423.0" />
<ColumnConstraints hgrow="NEVER" maxWidth="294.0" minWidth="10.0" percentWidth="30.0" prefWidth="177.0" />
@ -52,6 +52,8 @@
<RowConstraints maxHeight="200.0" minHeight="8.0" prefHeight="27.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="269.0" minHeight="10.0" prefHeight="65.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="250.0" minHeight="0.0" prefHeight="44.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="250.0" minHeight="10.0" prefHeight="44.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="250.0" minHeight="10.0" prefHeight="44.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="272.0" minHeight="10.0" prefHeight="34.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="272.0" minHeight="10.0" prefHeight="34.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="272.0" minHeight="0.0" prefHeight="46.0" vgrow="SOMETIMES" />
@ -62,9 +64,9 @@
<ChoiceBox fx:id="comboBoxSelectorMap" prefHeight="50.0" prefWidth="9.9999999E7" GridPane.rowIndex="1" />
<Button fx:id="buttonAddBus" mnemonicParsing="false" onAction="#ButtonAddBus_Click" prefHeight="50.0" prefWidth="9.999999999E9" text="Добавить автобус" GridPane.rowIndex="5" />
<TextField fx:id="textBoxPosition" GridPane.rowIndex="6" />
<Button fx:id="buttonRemoveCar" mnemonicParsing="false" onAction="#ButtonRemoveBus_Click" prefHeight="63.0" prefWidth="9.999999999E9" text="Удалить автобус" GridPane.rowIndex="7" />
<Button fx:id="buttonShowMap" mnemonicParsing="false" onAction="#ButtonShowOnMap_Click" prefHeight="63.0" prefWidth="9.99999999999E11" text="Посмотреть карту" GridPane.rowIndex="10" />
<AnchorPane prefHeight="157.0" prefWidth="332.0" GridPane.rowIndex="11">
<Button fx:id="buttonRemoveCar" mnemonicParsing="false" onAction="#ButtonRemoveBus_Click" prefHeight="63.0" prefWidth="9.999999999E9" text="Удалить автобус" GridPane.rowIndex="9" />
<Button fx:id="buttonShowMap" mnemonicParsing="false" onAction="#ButtonShowOnMap_Click" prefHeight="63.0" prefWidth="9.99999999999E11" text="Посмотреть карту" GridPane.rowIndex="12" />
<AnchorPane prefHeight="230.0" prefWidth="377.0" GridPane.rowIndex="13">
<children>
<Button fx:id="buttonLeft" layoutX="38.0" layoutY="52.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#ButtonMove_Click" prefHeight="30.0" prefWidth="30.0" AnchorPane.bottomAnchor="52.0" AnchorPane.rightAnchor="111.0">
<graphic>
@ -104,12 +106,14 @@
</Button>
</children>
</AnchorPane>
<Button fx:id="buttonShowStorage" mnemonicParsing="false" onAction="#ButtonShowStorage_Click" prefHeight="50.0" prefWidth="1.0E16" text="Посмотреть хранилище" GridPane.rowIndex="9" />
<Button fx:id="buttonShowStorage" mnemonicParsing="false" onAction="#ButtonShowStorage_Click" prefHeight="50.0" prefWidth="1.0E16" text="Посмотреть хранилище" GridPane.rowIndex="11" />
<TextField fx:id="TextFieldMap" />
<Button fx:id="Map" mnemonicParsing="false" onAction="#ButtonAddMap_Click" prefHeight="50.0" prefWidth="9.999999999E9" text="Добавить карту" GridPane.rowIndex="2" />
<ListView fx:id="listViewMaps" prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="3" />
<Button fx:id="buttonDeleteMap" mnemonicParsing="false" onAction="#ButtonDeleteMap_Click" prefHeight="50.0" prefWidth="9.999999999E9" text="Удалить карту" GridPane.rowIndex="4" />
<Button fx:id="ButtonDeleteEditBus" mnemonicParsing="false" onAction="#ButtonDeleteEditBus_Click" prefHeight="63.0" prefWidth="9.999999999E9" text="Вернуть автобус" GridPane.rowIndex="8" />
<Button fx:id="ButtonDeleteEditBus" mnemonicParsing="false" onAction="#ButtonDeleteEditBus_Click" prefHeight="63.0" prefWidth="9.999999999E9" text="Вернуть автобус" GridPane.rowIndex="10" />
<Button fx:id="ButtonSortByColor" mnemonicParsing="false" onAction="#ButtonSortByColor_Click" prefHeight="63.0" prefWidth="9.999999999E9" text="Сортировка по цвету" GridPane.rowIndex="7" />
<Button fx:id="buttonSortByType" mnemonicParsing="false" onAction="#ButtonSortByType_Click" prefHeight="63.0" prefWidth="9.999999999E9" text="Сортировка по типу" GridPane.rowIndex="8" />
</children>
</GridPane>
</children>