Compare commits
2 Commits
eb2ffae473
...
1583e33ab3
Author | SHA1 | Date |
---|---|---|
|
1583e33ab3 | |
|
277f8f82f6 |
|
@ -1,9 +1,11 @@
|
||||||
package lab4;
|
package lab4;
|
||||||
|
|
||||||
/**
|
|
||||||
* My class Furniture.
|
///**
|
||||||
*/
|
// * My class Furniture.
|
||||||
public class Furniture<T> {
|
// */
|
||||||
|
//todo uncomment /*<Furniture>*/
|
||||||
|
public /*abstract*/ class Furniture/*<T>*/ implements Comparable<Furniture> {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@ -13,14 +15,13 @@ public class Furniture<T> {
|
||||||
private int width;
|
private int width;
|
||||||
private int price;
|
private int price;
|
||||||
|
|
||||||
private T additional;
|
// private T additional;
|
||||||
|
|
||||||
public void setAdditional(T additional) {
|
// public void setAdditional(T additional) {
|
||||||
this.additional = additional;
|
// this.additional = additional;
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param material
|
* @param material
|
||||||
* @param length
|
* @param length
|
||||||
* @param height
|
* @param height
|
||||||
|
@ -74,4 +75,19 @@ public class Furniture<T> {
|
||||||
", price=" + price +
|
", 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,19 @@
|
||||||
package lab4;
|
package lab4;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// todo Comparable
|
System.out.println(Integer.MIN_VALUE - 1);
|
||||||
//todo equals+hashcode
|
//todo equals+hashcode
|
||||||
// todo JavaDoc
|
// todo JavaDoc
|
||||||
final Furniture<Furniture> furniture1 = new Furniture<>("A", 1, 1, 1, 1);
|
final Furniture/*<Furniture>*/ furniture1 = new Furniture("A", 1, 1, 1, 1);
|
||||||
final Furniture<String> furniture2 = new Furniture<>("D", 1, 1, 1, 4);
|
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 = {
|
Furniture[] furnitureArray = {
|
||||||
furniture1,
|
furniture1,
|
||||||
furniture2,
|
furniture2,
|
||||||
|
@ -22,8 +27,8 @@ public class Main {
|
||||||
}
|
}
|
||||||
System.out.println("+++++++++++++");
|
System.out.println("+++++++++++++");
|
||||||
|
|
||||||
furniture1.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)");
|
// furniture2.setAdditional("new Furniture(\"Y\", 1, 1, 1, 0)");
|
||||||
|
|
||||||
// furnitureArray[0].setMaterial("E");
|
// furnitureArray[0].setMaterial("E");
|
||||||
|
|
||||||
|
@ -41,5 +46,39 @@ public class Main {
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("+++++++++++++");
|
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("+++++++++++++");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue