From 5b665e2312a781ad91eb09681f3c39e34bac8751 Mon Sep 17 00:00:00 2001 From: Den <99994444@ukr.net> Date: Tue, 2 Jun 2026 18:23:53 +0300 Subject: [PATCH] passed checklist and test --- src/main/java/core/basesyntax/Car.java | 84 ++++++++++++++++++++--- src/main/java/core/basesyntax/Engine.java | 47 ++++++++++++- src/main/java/core/basesyntax/Wheel.java | 38 +++++++++- 3 files changed, 156 insertions(+), 13 deletions(-) diff --git a/src/main/java/core/basesyntax/Car.java b/src/main/java/core/basesyntax/Car.java index a84872f46..a4fb7dacc 100644 --- a/src/main/java/core/basesyntax/Car.java +++ b/src/main/java/core/basesyntax/Car.java @@ -1,17 +1,83 @@ package core.basesyntax; +import java.util.ArrayList; import java.util.List; +import java.util.Objects; -/** - * Make this class immutable. See requirements in task description. - */ -public class Car { - private int year; - private String color; - private List wheels; - private Engine engine; +public final class Car { + private final int year; + private final String color; + private final List wheels; + private final Engine engine; - //implement this class + public Car(int year, String color, List wheels, Engine engine) { + this.year = year; + this.color = color; + this.wheels = getCopyWheels(wheels); + this.engine = engine == null ? null : engine.clone(); + } + + public int getYear() { + return year; + } + + public String getColor() { + return color; + } + + public List getWheels() { + List copyWheels = new ArrayList<>(); + for (Wheel wheel : this.wheels) { + copyWheels.add(wheel != null ? wheel.clone() : null); + } + return copyWheels; + } + + public Engine getEngine() { + return engine == null ? null : engine.clone(); + } + + public Car changeEngine(Engine newEngine) { + return new Car(this.year, this.color, this.wheels, newEngine); + } + + public Car changeColor(String newColor) { + return new Car(this.year, newColor, this.wheels, this.engine); + } + + public Car addWheel(Wheel newWheel) { + List copyWheels = getCopyWheels(this.wheels); + copyWheels.add(newWheel.clone()); + return new Car(this.year, this.color, copyWheels, this.engine); + } + + private List getCopyWheels(List wheelsList) { + List copyWheels = new ArrayList<>(); + for (Wheel wheel : wheelsList) { + copyWheels.add(wheel != null ? wheel.clone() : null); + } + return copyWheels; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Car car = (Car) o; + return year == car.year + && Objects.equals(color, car.color) + && Objects.equals(wheels, car.wheels) + && Objects.equals(engine, car.engine); + } + + @Override + public int hashCode() { + return Objects.hash(year, color, wheels, engine); + } @Override public String toString() { diff --git a/src/main/java/core/basesyntax/Engine.java b/src/main/java/core/basesyntax/Engine.java index a26a75c5f..557e2b86c 100644 --- a/src/main/java/core/basesyntax/Engine.java +++ b/src/main/java/core/basesyntax/Engine.java @@ -1,10 +1,53 @@ package core.basesyntax; -public class Engine { +import java.util.Objects; + +public class Engine implements Cloneable { private int horsePower; private String manufacturer; - //implement this class + public Engine(int horsePower, String manufacturer) { + this.horsePower = horsePower; + this.manufacturer = manufacturer; + } + + public int getHorsePower() { + return horsePower; + } + + public void setHorsePower(int horsePower) { + this.horsePower = horsePower; + } + + public String getManufacturer() { + return manufacturer; + } + + public void setManufacturer(String manufacturer) { + this.manufacturer = manufacturer; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + Engine engine = (Engine) obj; + return horsePower == engine.horsePower && Objects.equals(manufacturer, engine.manufacturer); + } + + @Override + public Engine clone() { + return new Engine(this.horsePower, this.manufacturer); + } + + @Override + public int hashCode() { + return Objects.hash(horsePower, manufacturer); + } @Override public String toString() { diff --git a/src/main/java/core/basesyntax/Wheel.java b/src/main/java/core/basesyntax/Wheel.java index a09d6cd9e..80e98e3d0 100644 --- a/src/main/java/core/basesyntax/Wheel.java +++ b/src/main/java/core/basesyntax/Wheel.java @@ -1,9 +1,43 @@ package core.basesyntax; -public class Wheel { +import java.util.Objects; + +public class Wheel implements Cloneable { private int radius; - //implement this class + public Wheel(int radius) { + this.radius = radius; + } + + public int getRadius() { + return radius; + } + + public void setRadius(int radius) { + this.radius = radius; + } + + @Override + public int hashCode() { + return Objects.hash(radius); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + Wheel wheel = (Wheel) obj; + return radius == wheel.radius; + } + + @Override + public Wheel clone() { + return new Wheel(this.radius); + } @Override public String toString() {