update lab4
This commit is contained in:
parent
277f8f82f6
commit
1583e33ab3
|
@ -5,7 +5,7 @@ package lab4;
|
||||||
// * My class Furniture.
|
// * My class Furniture.
|
||||||
// */
|
// */
|
||||||
//todo uncomment /*<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 material
|
||||||
* @param length
|
* @param length
|
||||||
* @param height
|
* @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
|
@Override
|
||||||
public int compareTo(Object o) {
|
public int compareTo(Furniture o) {
|
||||||
return /*this.*/price - ((Furniture) o).price;
|
final int priceDifference = price - o.price;
|
||||||
// return /*this.*/material.compareTo(((Furniture) o).material);
|
return priceDifference != 0
|
||||||
|
? priceDifference
|
||||||
|
// : -material.compareTo(o.material);
|
||||||
|
: o.material.compareTo(material);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
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) {
|
||||||
System.out.println(Integer.MIN_VALUE - 1);
|
System.out.println(Integer.MIN_VALUE - 1);
|
||||||
// todo Comparable
|
|
||||||
//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("F", 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));
|
||||||
System.out.println(furniture1.compareTo("furniture2"));
|
// System.out.println(furniture1.compareTo("furniture2"));
|
||||||
|
|
||||||
Furniture[] furnitureArray = {
|
Furniture[] furnitureArray = {
|
||||||
furniture1,
|
furniture1,
|
||||||
|
@ -46,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