diff --git a/src/main/java/ArmoredCarCompareByType.java b/src/main/java/ArmoredCarCompareByType.java index 0386396..a996342 100644 --- a/src/main/java/ArmoredCarCompareByType.java +++ b/src/main/java/ArmoredCarCompareByType.java @@ -37,6 +37,8 @@ public class ArmoredCarCompareByType implements Comparator { } return 1; } + if (xCar.getArmoredCar().drawingCaterpillar.compareTo(yCar.getArmoredCar().drawingCaterpillar) != 0) + return xCar.getArmoredCar().drawingCaterpillar.compareTo(yCar.getArmoredCar().drawingCaterpillar); var speedCompare = Integer.compare(xCar.getArmoredCar().getArmoredCar().getSpeed(), yCar.getArmoredCar().getArmoredCar().getSpeed()); if (speedCompare != 0) { diff --git a/src/main/java/DrawingCaterpillar.java b/src/main/java/DrawingCaterpillar.java index 2785b4e..fb3484b 100644 --- a/src/main/java/DrawingCaterpillar.java +++ b/src/main/java/DrawingCaterpillar.java @@ -1,6 +1,7 @@ import java.awt.*; +import java.util.HashMap; -public class DrawingCaterpillar implements IDrawingCaterpillar { +public class DrawingCaterpillar implements IDrawingCaterpillar, Comparable { private NumRinks numRinks = NumRinks.Four; private Color color; @@ -48,4 +49,15 @@ public class DrawingCaterpillar implements IDrawingCaterpillar { public NumRinks getNumRinks() { return numRinks; } + + @Override + public int compareTo(Object o) { + if (!(o instanceof IDrawingCaterpillar)) + return -1; + HashMap dict = new HashMap<>(); + dict.put(DrawingCaterpillar.class.getName(), 0); + dict.put(DrawingDoubleCaterpillar.class.getName(), 1); + dict.put(DrawingCrossCaterpillar.class.getName(), 2); + return Integer.compare(dict.get(this.getClass().getName()), dict.get(o.getClass().getName())); + } } diff --git a/src/main/java/DrawingCrossCaterpillar.java b/src/main/java/DrawingCrossCaterpillar.java index 3796747..7ee2ed0 100644 --- a/src/main/java/DrawingCrossCaterpillar.java +++ b/src/main/java/DrawingCrossCaterpillar.java @@ -1,4 +1,5 @@ import java.awt.*; +import java.util.HashMap; public class DrawingCrossCaterpillar implements IDrawingCaterpillar{ private NumRinks numRinks = NumRinks.Four; @@ -54,4 +55,15 @@ public class DrawingCrossCaterpillar implements IDrawingCaterpillar{ public NumRinks getNumRinks() { return numRinks; } + + @Override + public int compareTo(Object o) { + if (!(o instanceof IDrawingCaterpillar)) + return -1; + HashMap dict = new HashMap<>(); + dict.put(DrawingCaterpillar.class.getName(), 0); + dict.put(DrawingDoubleCaterpillar.class.getName(), 1); + dict.put(DrawingCrossCaterpillar.class.getName(), 2); + return Integer.compare(dict.get(this.getClass().getName()), dict.get(o.getClass().getName())); + } } diff --git a/src/main/java/DrawingDoubleCaterpillar.java b/src/main/java/DrawingDoubleCaterpillar.java index e6c8a78..1a7bc52 100644 --- a/src/main/java/DrawingDoubleCaterpillar.java +++ b/src/main/java/DrawingDoubleCaterpillar.java @@ -1,4 +1,5 @@ import java.awt.*; +import java.util.HashMap; public class DrawingDoubleCaterpillar implements IDrawingCaterpillar{ private NumRinks numRinks = NumRinks.Four; @@ -51,4 +52,15 @@ public class DrawingDoubleCaterpillar implements IDrawingCaterpillar{ public NumRinks getNumRinks() { return numRinks; } + + @Override + public int compareTo(Object o) { + if (!(o instanceof IDrawingCaterpillar)) + return -1; + HashMap dict = new HashMap<>(); + dict.put(DrawingCaterpillar.class.getName(), 0); + dict.put(DrawingDoubleCaterpillar.class.getName(), 1); + dict.put(DrawingCrossCaterpillar.class.getName(), 2); + return Integer.compare(dict.get(this.getClass().getName()), dict.get(o.getClass().getName())); + } } diff --git a/src/main/java/IDrawingCaterpillar.java b/src/main/java/IDrawingCaterpillar.java index 6a35aec..0d35365 100644 --- a/src/main/java/IDrawingCaterpillar.java +++ b/src/main/java/IDrawingCaterpillar.java @@ -1,6 +1,6 @@ import java.awt.*; -public interface IDrawingCaterpillar { +public interface IDrawingCaterpillar extends Comparable{ void setNumRinks(int n); void DrawCaterpillar(Graphics2D g2d, int startPosX, int startPosY);