diff --git a/lab7/lab7.iml b/lab7/lab7.iml new file mode 100644 index 0000000..9465dd8 --- /dev/null +++ b/lab7/lab7.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/lab7/out/production/lab7/lab7/Car.class b/lab7/out/production/lab7/lab7/Car.class new file mode 100644 index 0000000..c2aec9b Binary files /dev/null and b/lab7/out/production/lab7/lab7/Car.class differ diff --git a/lab7/out/production/lab7/lab7/CarCollection.class b/lab7/out/production/lab7/lab7/CarCollection.class new file mode 100644 index 0000000..8174d55 Binary files /dev/null and b/lab7/out/production/lab7/lab7/CarCollection.class differ diff --git a/lab7/out/production/lab7/lab7/CombatsCar.class b/lab7/out/production/lab7/lab7/CombatsCar.class new file mode 100644 index 0000000..bfc2887 Binary files /dev/null and b/lab7/out/production/lab7/lab7/CombatsCar.class differ diff --git a/lab7/out/production/lab7/lab7/ElectricCar.class b/lab7/out/production/lab7/lab7/ElectricCar.class new file mode 100644 index 0000000..11ab620 Binary files /dev/null and b/lab7/out/production/lab7/lab7/ElectricCar.class differ diff --git a/lab7/out/production/lab7/lab7/Main.class b/lab7/out/production/lab7/lab7/Main.class new file mode 100644 index 0000000..8523948 Binary files /dev/null and b/lab7/out/production/lab7/lab7/Main.class differ diff --git a/lab7/out/production/lab7/lab7/SportsCar.class b/lab7/out/production/lab7/lab7/SportsCar.class new file mode 100644 index 0000000..454f537 Binary files /dev/null and b/lab7/out/production/lab7/lab7/SportsCar.class differ diff --git a/lab7/src/lab7/Car.java b/lab7/src/lab7/Car.java new file mode 100644 index 0000000..fb9af96 --- /dev/null +++ b/lab7/src/lab7/Car.java @@ -0,0 +1,42 @@ +package lab7; + +public class Car { + + protected String mark; + protected String model; + protected int price; + protected int year; + protected float fuel; + + public Car(String mark, String model, int price, int year, float fuel) { + setValues(mark, model, price, year, fuel); + } + +// Цей конструктор був створений окремо для класу ElectricCar + public Car(String mark, String model, int price, int year) { + setValues(mark, model, price, year); + } + + public void setValues(String mark, String model, int price, int year, float fuel) { + this.mark = mark; + this.model = model; + this.price = price; + this.year = year; + this.fuel = fuel; + } + + public void setValues(String mark, String model, int price, int year) { + this.mark = mark; + this.model = model; + this.price = price; + this.year = year; + } + + public void printValues() { + System.out.println("Марка: " + mark); + System.out.println("Модель: " + model + " " + year); + System.out.println("Ціна: " + price + "$"); + System.out.println("Витрати палива: " + fuel + "л/100км"); + } + +} diff --git a/lab7/src/lab7/CarCollection.java b/lab7/src/lab7/CarCollection.java new file mode 100644 index 0000000..5f8effb --- /dev/null +++ b/lab7/src/lab7/CarCollection.java @@ -0,0 +1,137 @@ +package lab7; + +import java.util.*; + +public class CarCollection implements List { + private final LinkedList carList; + + public CarCollection() { + carList = new LinkedList<>(); + } + + public CarCollection(T car) { + carList = new LinkedList<>(); + carList.add(car); + } + + public CarCollection(Collection c) { + carList = new LinkedList<>(c); + } + + // Реалізація методів List + @Override + public int size() { + return carList.size(); + } + + @Override + public boolean isEmpty() { + return carList.isEmpty(); + } + + @Override + public boolean contains(Object o) { + return carList.contains(o); + } + + @Override + public Iterator iterator() { + return carList.iterator(); + } + + @Override + public Object[] toArray() { + return carList.toArray(); + } + + @Override + public T1[] toArray(T1[] a) { + return carList.toArray(a); + } + + @Override + public boolean add(T t) { + return carList.add(t); + } + + @Override + public boolean remove(Object o) { + return carList.remove(o); + } + + @Override + public boolean containsAll(Collection c) { + return carList.containsAll(c); + } + + @Override + public boolean addAll(Collection c) { + return carList.addAll(c); + } + + @Override + public boolean addAll(int index, Collection c) { + return carList.addAll(index, c); + } + + @Override + public boolean removeAll(Collection c) { + return carList.removeAll(c); + } + + @Override + public boolean retainAll(Collection c) { + return carList.retainAll(c); + } + + @Override + public void clear() { + carList.clear(); + } + + @Override + public T get(int index) { + return carList.get(index); + } + + @Override + public T set(int index, T element) { + return carList.set(index, element); + } + + @Override + public void add(int index, T element) { + carList.add(index, element); + } + + @Override + public T remove(int index) { + return carList.remove(index); + } + + @Override + public int indexOf(Object o) { + return carList.indexOf(o); + } + + @Override + public int lastIndexOf(Object o) { + return carList.lastIndexOf(o); + } + + @Override + public ListIterator listIterator() { + return carList.listIterator(); + } + + @Override + public ListIterator listIterator(int index) { + return carList.listIterator(index); + } + + @Override + public List subList(int fromIndex, int toIndex) { + return carList.subList(fromIndex, toIndex); + } + +} \ No newline at end of file diff --git a/lab7/src/lab7/CombatsCar.java b/lab7/src/lab7/CombatsCar.java new file mode 100644 index 0000000..8a7eeae --- /dev/null +++ b/lab7/src/lab7/CombatsCar.java @@ -0,0 +1,29 @@ +package lab7; + +public class CombatsCar extends Car { + + private final int crew; + private final float weight; + private final String country; + + public CombatsCar(String mark, String model, int price, int year, float fuel, int crew, float weight, String country) { + super(mark, model, price, year, fuel); + this.crew = crew; + this.weight = weight; + this.country = country; + } + + public String getCountry() { + return country; + } + + public void printValues() { + super.printValues(); + System.out.println("--- Особливості бойової машини ---"); + System.out.println("Екіпаж: " + crew + " людей"); + System.out.println("Бойова маса: " + weight + "т"); + System.out.println("Країна-виробник: " + country); + + } + +} diff --git a/lab7/src/lab7/ElectricCar.java b/lab7/src/lab7/ElectricCar.java new file mode 100644 index 0000000..04012d9 --- /dev/null +++ b/lab7/src/lab7/ElectricCar.java @@ -0,0 +1,25 @@ +package lab7; + +public class ElectricCar extends Car { + + private final float electricity; + private final float chargingSpeed; + + public ElectricCar(String mark, String model, int price, int year, float electricity, int chargingSpeed) { + super(mark, model, price, year); + this.electricity = electricity; + this.chargingSpeed = chargingSpeed; + } + + public float getElectricity() { + return electricity; + } + + public void printValues() { + super.printValues(); + System.out.println("-Особливості електрокару-"); + System.out.println("Споживання електроенергії: " + electricity + " кВт·год/100км"); + System.out.println("Швидкість зарядки (з 0% до 100%): " + chargingSpeed + " хв"); + } + +} diff --git a/lab7/src/lab7/Main.java b/lab7/src/lab7/Main.java new file mode 100644 index 0000000..0681976 --- /dev/null +++ b/lab7/src/lab7/Main.java @@ -0,0 +1,73 @@ +package lab7; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.Scanner; + +public class Main { + + public static void main(String[] args) { + +// Використання пустого конструктора CarCollection + CarCollection sportsCars = new CarCollection<>(); + sportsCars.add(new SportsCar("Ferrari", "458 Italia", 230000, 2015, 9.8f, 562, 202, 4.5f)); + sportsCars.add(new SportsCar("Lamborghini", "Huracan", 260000, 2020, 9.5f, 640, 211, 5.2f)); + sportsCars.add(new SportsCar("Porsche", "911", 150000, 2018, 14.3f, 450, 191, 3.0f)); + sportsCars.add(new SportsCar("McLaren", "570S", 200000, 2017, 11.2f, 562, 204, 3.8f)); + sportsCars.add(new SportsCar("Aston Martin", "Vantage", 150000, 2021, 12.3f, 503, 195, 4.0f)); + +// Сортування спорткарів за ціною + System.out.println("~~~~~ Відсортовані спорткари за ціною ~~~~~"); + sportsCars.sort(Comparator.comparingInt(SportsCar::getPrice)); + + for (int i = sportsCars.size() - 1; i >= 0; i--) { + sportsCars.get(i).printValues(); + System.out.println(); + } + +// Використання конструктора CarCollection з передаванням в його лише один об'єкт + CarCollection combatsCars = new CarCollection<>(new CombatsCar("Humvee", "M1114", 220000, 1995, 6.5f, 4, 5.5f, "США")); + combatsCars.add(new CombatsCar("Puma", "IFV", 3500000, 2010, 10.7f, 6, 31f, "Німеччина")); + combatsCars.add(new CombatsCar("Warrior", "IFV", 4500000, 1988, 12.4f, 3, 29f, "Великобританія")); + combatsCars.add(new CombatsCar("ZBD", "05", 1200000, 2011, 9.4f, 4, 25f, "Китай")); + combatsCars.add(new CombatsCar("VBCI", "IFV", 5000000, 2008, 10.7f, 8, 32f, "Франція")); + +// Пошук бойової машини за країною-виробником + Scanner scanner = new Scanner(System.in); + System.out.print("Введіть країну-виробницю бойової машини: "); + String userCountry = scanner.nextLine(); + + System.out.println("Результати пошуку:\n"); + for (CombatsCar car : combatsCars) { + if (car.getCountry().equals(userCountry)) { + car.printValues(); + } + } + + ArrayList electricCarsList = new ArrayList<>(); + electricCarsList.add(new ElectricCar("Tesla", "Model S", 80000, 2022, 21.5f, 45)); + electricCarsList.add(new ElectricCar("Chevrolet", "Bolt", 35000, 2021, 28.0f, 60)); + electricCarsList.add(new ElectricCar("Nissan", "Leaf", 32000, 2021, 24.0f, 40)); + electricCarsList.add(new ElectricCar("BMW", "i3", 45000, 2021, 19.5f, 30)); + electricCarsList.add(new ElectricCar("Hyundai", "Kona", 42000, 2022, 22.0f, 50)); + +// Використання конструктора CarCollection з передаванням в його колекцію об'єктів + CarCollection electricCars = new CarCollection<>(electricCarsList); + +// Пошук автомобіля по діапазону споживання електроенергії + System.out.println("\nВведіть діапазон споживання електроенергії (кВт·год/100км):"); + System.out.print("від "); + float electricityLowerLimit = scanner.nextFloat(); + System.out.print("до "); + float electricityUpperLimit = scanner.nextFloat(); + + System.out.println("Результати пошуку:\n"); + for (ElectricCar car : electricCars) { + if (car.getElectricity() < electricityUpperLimit & car.getElectricity() > electricityLowerLimit) { + car.printValues(); + } + } + + } + +} diff --git a/lab7/src/lab7/SportsCar.java b/lab7/src/lab7/SportsCar.java new file mode 100644 index 0000000..6b77e1e --- /dev/null +++ b/lab7/src/lab7/SportsCar.java @@ -0,0 +1,28 @@ +package lab7; + +public class SportsCar extends Car { + + private final int power; + private final float speed; + private final float engineCapacity; + + public SportsCar(String mark, String model, int price, int year, float fuel, int power, float speed, float engineCapacity) { + super(mark, model, price, year, fuel); + this.power = power; + this.speed = speed; + this.engineCapacity = engineCapacity; + } + + public int getPrice() { + return price; + } + + public void printValues() { + super.printValues(); + System.out.println("-Особливості спорткару-"); + System.out.println("Потужість: " + power + " кінських сил"); + System.out.println("Максимальна швидкість: " + speed + " км/год"); + System.out.println("Об'єм двигуна: " + engineCapacity + "л"); + } + +}