From eb2ffae473e2165cdf62733e0f2d070e7ce64717 Mon Sep 17 00:00:00 2001 From: Oleksii Aleshchenko Date: Thu, 18 May 2023 16:03:31 +0300 Subject: [PATCH] update lab4 --- src/lab4/Furniture.java | 26 ++++++++++++++++++++++++-- src/lab4/Main.java | 33 ++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/lab4/Furniture.java b/src/lab4/Furniture.java index e13f68c..f7ecea1 100644 --- a/src/lab4/Furniture.java +++ b/src/lab4/Furniture.java @@ -3,7 +3,7 @@ package lab4; /** * My class Furniture. */ -public class Furniture { +public class Furniture { /** * */ @@ -13,6 +13,12 @@ public class Furniture { private int width; private int price; + private T additional; + + public void setAdditional(T additional) { + this.additional = additional; + } + /** * * @param material @@ -22,13 +28,18 @@ public class Furniture { * @param 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.height = height; this.width = width; this.price = price; } + public void setMaterial(String material) { + this.material = material; + } + public String getMaterial() { return material; } @@ -52,4 +63,15 @@ public class Furniture { /*public Furniture() { }*/ + + @Override + public String toString() { + return "Furniture{" + + "material='" + material + '\'' + + ", length=" + length + + ", height=" + height + + ", width=" + width + + ", price=" + price + + '}'; + } } diff --git a/src/lab4/Main.java b/src/lab4/Main.java index 0b8e164..67b3fb7 100644 --- a/src/lab4/Main.java +++ b/src/lab4/Main.java @@ -4,19 +4,42 @@ import java.util.Arrays; public class Main { public static void main(String[] args) { +// todo Comparable //todo equals+hashcode // todo JavaDoc + final Furniture furniture1 = new Furniture<>("A", 1, 1, 1, 1); + final Furniture furniture2 = new Furniture<>("D", 1, 1, 1, 4); Furniture[] furnitureArray = { - new Furniture("A", 1, 1, 1, 1), - new Furniture("D", 1, 1, 1, 4), - new Furniture("B", 1, 2, 1, 1), - new Furniture("C", 1, 1, 3, 1), + furniture1, + furniture2, + new Furniture("B", 1, 2, 1, 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())); -// todo print + for (Furniture furniture : furnitureArray) { + System.out.println(furniture); + } + + System.out.println("+++++++++++++"); // todo check Arrays.sort(furnitureArray, (o1, o2) -> Integer.compare(o2.getPrice(), o1.getPrice())); + for (Furniture furniture : furnitureArray) { + System.out.println(furniture); + } + + System.out.println("+++++++++++++"); } }