Compare commits

...

2 Commits

Author SHA1 Message Date
Oleksii Aleshchenko 1583e33ab3 update lab4 2023-05-25 16:04:54 +03:00
Oleksii Aleshchenko 277f8f82f6 update lab4 2023-05-25 15:06:01 +03:00
4 changed files with 91 additions and 14 deletions

View File

@ -1,9 +1,11 @@
package lab4;
/**
* My class Furniture.
*/
public class Furniture<T> {
///**
// * My class Furniture.
// */
//todo uncomment /*<Furniture>*/
public /*abstract*/ class Furniture/*<T>*/ implements Comparable<Furniture> {
/**
*
*/
@ -13,14 +15,13 @@ public class Furniture<T> {
private int width;
private int price;
private T additional;
// private T additional;
public void setAdditional(T additional) {
this.additional = additional;
}
// public void setAdditional(T additional) {
// this.additional = additional;
// }
/**
*
* @param material
* @param length
* @param height
@ -74,4 +75,19 @@ public class Furniture<T> {
", price=" + price +
'}';
}
// @Override
// public int compareTo(Object o) {
// return /*this.*/price - ((Furniture) o).price;
//// return /*this.*/material.compareTo(((Furniture) o).material);
// }
@Override
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,14 +1,19 @@
package lab4;
import java.util.Arrays;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
// todo Comparable
System.out.println(Integer.MIN_VALUE - 1);
//todo equals+hashcode
// todo JavaDoc
final Furniture<Furniture> furniture1 = new Furniture<>("A", 1, 1, 1, 1);
final Furniture<String> furniture2 = new Furniture<>("D", 1, 1, 1, 4);
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"));
Furniture[] furnitureArray = {
furniture1,
furniture2,
@ -22,8 +27,8 @@ public class Main {
}
System.out.println("+++++++++++++");
furniture1.setAdditional(new Furniture("Y", 1, 1, 1, 0));
furniture2.setAdditional("new Furniture(\"Y\", 1, 1, 1, 0)");
// furniture1.setAdditional(new Furniture("Y", 1, 1, 1, 0));
// furniture2.setAdditional("new Furniture(\"Y\", 1, 1, 1, 0)");
// furnitureArray[0].setMaterial("E");
@ -41,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();
}
}