From 07e589e4efa9909bda68057e1eb2f7cdf8b94b4b Mon Sep 17 00:00:00 2001 From: darina_melnyk Date: Thu, 16 Apr 2026 20:44:25 +0300 Subject: [PATCH] Finish README task --- .vscode/settings.json | 3 + src/main/java/core/basesyntax/Car.java | 89 +++++++++++++++++++---- src/main/java/core/basesyntax/Engine.java | 58 +++++++++++++-- src/main/java/core/basesyntax/Wheel.java | 46 +++++++++++- 4 files changed, 173 insertions(+), 23 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..001c4361e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.checkstyle.configuration": "/sun_checks.xml" +} \ No newline at end of file diff --git a/src/main/java/core/basesyntax/Car.java b/src/main/java/core/basesyntax/Car.java index a84872f46..0cd66ab53 100644 --- a/src/main/java/core/basesyntax/Car.java +++ b/src/main/java/core/basesyntax/Car.java @@ -1,25 +1,86 @@ package core.basesyntax; +import java.util.ArrayList; import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; -/** - * 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 = wheels.stream() + .map(Wheel::clone) + .collect(Collectors.toList()); + this.engine = engine == null ? null : engine.clone(); + } @Override public String toString() { return "Car{" - + "year=" + year - + ", color='" + color + '\'' - + ", wheels=" + wheels - + ", engine=" + engine - + '}'; + + "year=" + year + + ", color='" + color + '\'' + + ", wheels=" + wheels + + ", engine=" + engine + + '}'; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof Car)) { + return false; + } + Car car = (Car) obj; + 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); + } + + public Car changeEngine(Engine engine) { + return new Car(year, color, getWheels(), engine == null ? null : engine.clone()); + } + + public Car changeColor(String newColor) { + return new Car(year, newColor, getWheels(), engine == null ? null : engine.clone()); + } + + public Car addWheel(Wheel newWheel) { + List newWheels = wheels.stream() + .map(Wheel::clone) + .collect(Collectors.toList()); + newWheels.add(newWheel.clone()); + return new Car(year, color, newWheels, engine == null ? null : engine.clone()); + } + + public int getYear() { + return year; + } + + public String getColor() { + return color; + } + + public List getWheels() { + List copy = new ArrayList<>(); + for (Wheel wheel : wheels) { + copy.add(wheel.clone()); + } + return copy; + } + + public Engine getEngine() { + return engine == null ? null : engine.clone(); } } diff --git a/src/main/java/core/basesyntax/Engine.java b/src/main/java/core/basesyntax/Engine.java index a26a75c5f..bda9e9485 100644 --- a/src/main/java/core/basesyntax/Engine.java +++ b/src/main/java/core/basesyntax/Engine.java @@ -1,16 +1,64 @@ package core.basesyntax; -public class Engine { +import java.util.Objects; + +public final 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; + } @Override public String toString() { return "Engine{" - + "horsePower=" + horsePower - + ", manufacturer='" + manufacturer + '\'' - + '}'; + + "horsePower=" + horsePower + + ", manufacturer='" + manufacturer + '\'' + + '}'; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof Engine)) { + return false; + } + Engine engine = (Engine) obj; + return horsePower == engine.horsePower && Objects.equals(manufacturer, + engine.manufacturer); + } + + @Override + public int hashCode() { + return Objects.hash(horsePower, manufacturer); + } + + @Override + public Engine clone() { + try { + return (Engine) super.clone(); + } catch (CloneNotSupportedException e) { + throw new AssertionError("Clone not supported", e); + } + } + + public void setHorsePower(int horsePower) { + this.horsePower = horsePower; + } + + public void setManufacturer(String manufacturer) { + this.manufacturer = manufacturer; + } + + public int getHorsePower() { + return horsePower; + } + + public String getManufacturer() { + return manufacturer; } } diff --git a/src/main/java/core/basesyntax/Wheel.java b/src/main/java/core/basesyntax/Wheel.java index a09d6cd9e..b128b077c 100644 --- a/src/main/java/core/basesyntax/Wheel.java +++ b/src/main/java/core/basesyntax/Wheel.java @@ -1,14 +1,52 @@ package core.basesyntax; -public class Wheel { +import java.util.Objects; + +public final class Wheel implements Cloneable { private int radius; - //implement this class + public Wheel(int radius) { + this.radius = radius; + } @Override public String toString() { return "Wheel{" - + "radius=" + radius - + '}'; + + "radius=" + radius + + '}'; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof Wheel)) { + return false; + } + Wheel wheel = (Wheel) obj; + return radius == wheel.radius; + } + + @Override + public int hashCode() { + return Objects.hash(radius); + } + + @Override + public Wheel clone() { + try { + return (Wheel) super.clone(); + } catch (CloneNotSupportedException e) { + throw new AssertionError("Clone not supported", e); + } + } + + public void setRadius(int radius) { + this.radius = radius; + } + + public int getRadius() { + return radius; } }