From 86e06c3a5a153e4951fc6bc83d7345c5eacffb56 Mon Sep 17 00:00:00 2001 From: BadOfficer Date: Fri, 17 Feb 2023 01:25:57 +0200 Subject: [PATCH] Added lab4 --- .../lab4/Boat.java | 43 +++++++++++++++++++ .../lab4/Lab4.java | 30 +++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/IO_24/_02_Бондаренко_Тарас_Андрійович/lab4/Boat.java create mode 100644 src/IO_24/_02_Бондаренко_Тарас_Андрійович/lab4/Lab4.java diff --git a/src/IO_24/_02_Бондаренко_Тарас_Андрійович/lab4/Boat.java b/src/IO_24/_02_Бондаренко_Тарас_Андрійович/lab4/Boat.java new file mode 100644 index 0000000..275183d --- /dev/null +++ b/src/IO_24/_02_Бондаренко_Тарас_Андрійович/lab4/Boat.java @@ -0,0 +1,43 @@ +package IO_24._02_Бондаренко_Тарас_Андрійович.lab4; + +public class Boat { + private String name; + private int price; + private int mass; + private int age; + private String type; + + public Boat(String name, int price, int mass, int age, String type) { + this.name = name; + this.price = price; + this.mass = mass; + this.age = age; + this.type = type; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getMass() { + return mass; + } + + public int getAge() { + return age; + } + + public String getType() { + return type; + } + + @Override + public String toString() { + return String.format("%s - %s, which was built %d years ago, with mass - %d kg, has price - %d$;", + type, name, age, mass, price); + } +} diff --git a/src/IO_24/_02_Бондаренко_Тарас_Андрійович/lab4/Lab4.java b/src/IO_24/_02_Бондаренко_Тарас_Андрійович/lab4/Lab4.java new file mode 100644 index 0000000..423c902 --- /dev/null +++ b/src/IO_24/_02_Бондаренко_Тарас_Андрійович/lab4/Lab4.java @@ -0,0 +1,30 @@ +package IO_24._02_Бондаренко_Тарас_Андрійович.lab4; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + +public class Lab4 { + public static void main(String[] args) { + List boatList = Arrays.asList( + new Boat("Row V. Wave", 150000, 50000, 25, "cutter"), + new Boat("Dirty Oar", 250000, 40000, 15, "cutter"), + new Boat("Tumeric", 450000, 100000, 45, "cutter"), + new Boat("Life is Good", 350000, 20000, 5, "cutter") + ); + + boatList.sort(Comparator.comparing(Boat::getName));// comparing - method of Comparator interface + System.out.println("Sorted by name:"); + printed(boatList); + + boatList.sort(Comparator.comparing(Boat::getPrice).reversed()); + System.out.println("\nSorted by price in reverse order:"); + printed(boatList); + } + + public static void printed(List boatList) { + for (Boat boat : boatList) { + System.out.println(boat); + } + } +}