update lab4

This commit is contained in:
Oleksii Aleshchenko 2023-05-25 16:04:54 +03:00
parent 277f8f82f6
commit 1583e33ab3
4 changed files with 71 additions and 7 deletions

View File

@ -5,7 +5,7 @@ package lab4;
// * My class Furniture.
// */
//todo uncomment /*<Furniture>*/
public /*abstract*/ class Furniture/*<T>*/ implements Comparable/*<Furniture>*/{
public /*abstract*/ class Furniture/*<T>*/ implements Comparable<Furniture> {
/**
*
*/
@ -22,7 +22,6 @@ public /*abstract*/ class Furniture/*<T>*/ implements Comparable/*<Furniture>*/{
// }
/**
*
* @param material
* @param length
* @param height
@ -77,9 +76,18 @@ public /*abstract*/ class Furniture/*<T>*/ implements Comparable/*<Furniture>*/{
'}';
}
// @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);
}
}

View File

@ -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/*<Furniture>*/ furniture1 = new Furniture("A", 1, 1, 1, 1);
final Furniture/*<String>*/ 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<Furniture>() {
@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("+++++++++++++");
}
}

View File

@ -0,0 +1,11 @@
package lab4;
import java.util.Comparator;
public class MaterialFurnitureComparator implements Comparator<Furniture> {
@Override
public int compare(Furniture o1, Furniture o2) {
return o1.getMaterial().compareTo(o2.getMaterial());
}
}

View File

@ -0,0 +1,11 @@
package lab4;
import java.util.Comparator;
public class PriceFurnitureComparator implements Comparator<Furniture> {
@Override
public int compare(Furniture o1, Furniture o2) {
return o1.getPrice() - o2.getPrice();
}
}