diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/BusComparatorByColor.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/BusComparatorByColor.java new file mode 100644 index 0000000..956b7cb --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/BusComparatorByColor.java @@ -0,0 +1,76 @@ +package com.example.doubledeckerbus; + +import java.util.Comparator; + +public class BusComparatorByColor implements Comparator { + @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); + } +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/BusComparatorByType.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/BusComparatorByType.java new file mode 100644 index 0000000..68e0161 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/BusComparatorByType.java @@ -0,0 +1,65 @@ +package com.example.doubledeckerbus; + +import java.util.Comparator; + +public class BusComparatorByType implements Comparator { + @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); + } +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java index 9025974..bfb9db8 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java @@ -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(); diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingObjectBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingObjectBus.java index c90b0fa..e7015ec 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingObjectBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingObjectBus.java @@ -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; + } } diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java index 92662ce..b8b5eb3 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java @@ -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 { +public class MapWithSetBusesGeneric implements Comparable{ private final int _pictureWidth; private final int _pictureHeight; private final int _placeSizeWidth = 210; @@ -170,5 +171,35 @@ public class MapWithSetBusesGeneric comparator) + { + _setBuses.Sort(comparator); + } } diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java index 9b87fca..519a0bb 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java @@ -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 { @@ -22,6 +19,10 @@ class SetBusesGeneric { } 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 { } 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 { public void Clear() { _places.clear(); } + + public void Sort(Comparator comparator) { + if (comparator == null) { + return; + } + _places.sort(comparator); + } } diff --git a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml index 1bf0fe7..ccbb9fa 100644 --- a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml +++ b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml @@ -14,7 +14,7 @@ - + @@ -52,6 +52,8 @@ + + @@ -62,9 +64,9 @@ -