update lab4

This commit is contained in:
Oleksii Aleshchenko 2023-05-18 16:03:31 +03:00
parent f05125e298
commit eb2ffae473
2 changed files with 52 additions and 7 deletions

View File

@ -3,7 +3,7 @@ package lab4;
/** /**
* My class Furniture. * My class Furniture.
*/ */
public class Furniture { public class Furniture<T> {
/** /**
* *
*/ */
@ -13,6 +13,12 @@ public class Furniture {
private int width; private int width;
private int price; private int price;
private T additional;
public void setAdditional(T additional) {
this.additional = additional;
}
/** /**
* *
* @param material * @param material
@ -22,13 +28,18 @@ public class Furniture {
* @param price * @param price
*/ */
public Furniture(String material, int length, int height, int width, int price) { public Furniture(String material, int length, int height, int width, int price) {
this.material = material; // this.material = material;
setMaterial(material);
this.length = length; this.length = length;
this.height = height; this.height = height;
this.width = width; this.width = width;
this.price = price; this.price = price;
} }
public void setMaterial(String material) {
this.material = material;
}
public String getMaterial() { public String getMaterial() {
return material; return material;
} }
@ -52,4 +63,15 @@ public class Furniture {
/*public Furniture() { /*public Furniture() {
}*/ }*/
@Override
public String toString() {
return "Furniture{" +
"material='" + material + '\'' +
", length=" + length +
", height=" + height +
", width=" + width +
", price=" + price +
'}';
}
} }

View File

@ -4,19 +4,42 @@ import java.util.Arrays;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// 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<String> furniture2 = new Furniture<>("D", 1, 1, 1, 4);
Furniture[] furnitureArray = { Furniture[] furnitureArray = {
new Furniture("A", 1, 1, 1, 1), furniture1,
new Furniture("D", 1, 1, 1, 4), furniture2,
new Furniture("B", 1, 2, 1, 1), new Furniture("B", 1, 2, 1, 1),
new Furniture("C", 1, 1, 3, 1), new Furniture("C", 1, 1, 3, 1),
}; };
for (Furniture furniture : furnitureArray) {
// System.out.println(furniture.getMaterial() + " " + furniture.getPrice());
System.out.println(furniture);
}
System.out.println("+++++++++++++");
furniture1.setAdditional(new Furniture("Y", 1, 1, 1, 0));
furniture2.setAdditional("new Furniture(\"Y\", 1, 1, 1, 0)");
// furnitureArray[0].setMaterial("E");
Arrays.sort(furnitureArray, (o1, o2) -> o1.getMaterial().compareTo(o2.getMaterial())); Arrays.sort(furnitureArray, (o1, o2) -> o1.getMaterial().compareTo(o2.getMaterial()));
// todo print for (Furniture furniture : furnitureArray) {
System.out.println(furniture);
}
System.out.println("+++++++++++++");
// todo check // todo check
Arrays.sort(furnitureArray, (o1, o2) -> Integer.compare(o2.getPrice(), o1.getPrice())); Arrays.sort(furnitureArray, (o1, o2) -> Integer.compare(o2.getPrice(), o1.getPrice()));
for (Furniture furniture : furnitureArray) {
System.out.println(furniture);
}
System.out.println("+++++++++++++");
} }
} }