From 1583e33ab3fec7b3b25b9f17efa3858df0823bdd Mon Sep 17 00:00:00 2001 From: Oleksii Aleshchenko Date: Thu, 25 May 2023 16:04:54 +0300 Subject: [PATCH] update lab4 --- src/lab4/Furniture.java | 18 ++++++++--- src/lab4/Main.java | 38 +++++++++++++++++++++-- src/lab4/MaterialFurnitureComparator.java | 11 +++++++ src/lab4/PriceFurnitureComparator.java | 11 +++++++ 4 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 src/lab4/MaterialFurnitureComparator.java create mode 100644 src/lab4/PriceFurnitureComparator.java diff --git a/src/lab4/Furniture.java b/src/lab4/Furniture.java index d8dce60..dcabc47 100644 --- a/src/lab4/Furniture.java +++ b/src/lab4/Furniture.java @@ -5,7 +5,7 @@ package lab4; // * My class Furniture. // */ //todo uncomment /**/ -public /*abstract*/ class Furniture/**/ implements Comparable/**/{ +public /*abstract*/ class Furniture/**/ implements Comparable { /** * */ @@ -22,7 +22,6 @@ public /*abstract*/ class Furniture/**/ implements Comparable/**/{ // } /** - * * @param material * @param length * @param height @@ -77,9 +76,18 @@ public /*abstract*/ class Furniture/**/ implements Comparable/**/{ '}'; } +// @Override +// public int compareTo(Object o) { +// return /*this.*/price - ((Furniture) o).price; +//// return /*this.*/material.compareTo(((Furniture) o).material); +// } + @Override - public int compareTo(Object o) { - return /*this.*/price - ((Furniture) o).price; -// return /*this.*/material.compareTo(((Furniture) o).material); + public int compareTo(Furniture o) { + final int priceDifference = price - o.price; + return priceDifference != 0 + ? priceDifference +// : -material.compareTo(o.material); + : o.material.compareTo(material); } } diff --git a/src/lab4/Main.java b/src/lab4/Main.java index 76c1ce9..a4dc7fc 100644 --- a/src/lab4/Main.java +++ b/src/lab4/Main.java @@ -1,18 +1,18 @@ package lab4; import java.util.Arrays; +import java.util.Comparator; public class Main { public static void main(String[] args) { System.out.println(Integer.MIN_VALUE - 1); -// todo Comparable //todo equals+hashcode // todo JavaDoc final Furniture/**/ furniture1 = new Furniture("A", 1, 1, 1, 1); final Furniture/**/ furniture2 = new Furniture("F", 1, 1, 1, 4); System.out.println(furniture1.compareTo(furniture2)); - System.out.println(furniture1.compareTo("furniture2")); +// System.out.println(furniture1.compareTo("furniture2")); Furniture[] furnitureArray = { furniture1, @@ -46,5 +46,39 @@ public class Main { } System.out.println("+++++++++++++"); + + /*Arrays.sort(furnitureArray); + for (Furniture furniture : furnitureArray) { + System.out.println(furniture); + } + + System.out.println("+++++++++++++");*/ + + /*Arrays.sort(furnitureArray, new PriceFurnitureComparator()); + for (Furniture furniture : furnitureArray) { + System.out.println(furniture); + } + + System.out.println("+++++++++++++");*/ + + /*Arrays.sort(furnitureArray, new Comparator() { + @Override + public int compare(Furniture o1, Furniture o2) { + return o1.getMaterial().compareTo(o2.getMaterial()); + } + });*/ +// Arrays.sort(furnitureArray, (o1, o2) -> o1.getMaterial().compareTo(o2.getMaterial())); +// Arrays.sort(furnitureArray, Comparator.comparing(Furniture::getMaterial)); +// Arrays.sort(furnitureArray, Comparator.comparing(Furniture::getMaterial).reversed()); +// Arrays.sort(furnitureArray, Comparator.comparing(Furniture::getPrice) +// .thenComparing(Furniture::getMaterial).reversed()); + Arrays.sort(furnitureArray, Comparator.comparing(Furniture::getPrice) + .thenComparing(Comparator.comparing(Furniture::getMaterial).reversed())); + + for (Furniture furniture : furnitureArray) { + System.out.println(furniture); + } + + System.out.println("+++++++++++++"); } } diff --git a/src/lab4/MaterialFurnitureComparator.java b/src/lab4/MaterialFurnitureComparator.java new file mode 100644 index 0000000..3152d50 --- /dev/null +++ b/src/lab4/MaterialFurnitureComparator.java @@ -0,0 +1,11 @@ +package lab4; + +import java.util.Comparator; + +public class MaterialFurnitureComparator implements Comparator { + + @Override + public int compare(Furniture o1, Furniture o2) { + return o1.getMaterial().compareTo(o2.getMaterial()); + } +} diff --git a/src/lab4/PriceFurnitureComparator.java b/src/lab4/PriceFurnitureComparator.java new file mode 100644 index 0000000..62e0e37 --- /dev/null +++ b/src/lab4/PriceFurnitureComparator.java @@ -0,0 +1,11 @@ +package lab4; + +import java.util.Comparator; + +public class PriceFurnitureComparator implements Comparator { + + @Override + public int compare(Furniture o1, Furniture o2) { + return o1.getPrice() - o2.getPrice(); + } +}