From ea1d506182ec811b1be2a92e64e60f2d1fb4ea97 Mon Sep 17 00:00:00 2001 From: Lolita Date: Tue, 28 Apr 2026 15:47:05 +0300 Subject: [PATCH] Update Car, Engine and Wheel classes --- src/main/java/core/basesyntax/Car.java | 104 ++++++++++++++++++++-- src/main/java/core/basesyntax/Engine.java | 51 ++++++++++- src/main/java/core/basesyntax/Wheel.java | 38 +++++++- 3 files changed, 183 insertions(+), 10 deletions(-) diff --git a/src/main/java/core/basesyntax/Car.java b/src/main/java/core/basesyntax/Car.java index a84872f46..fc6a7582f 100644 --- a/src/main/java/core/basesyntax/Car.java +++ b/src/main/java/core/basesyntax/Car.java @@ -1,17 +1,109 @@ 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; + if (wheels == null) { + throw new NullPointerException(); + } else { + ArrayList newWheels = new ArrayList<>(); + for (Wheel wheel : wheels) { + newWheels.add(new Wheel(wheel.getRadius())); + } + this.wheels = newWheels; + } + if (engine == null) { + this.engine = null; + } else { + this.engine = new Engine(engine.getHorsePower(), engine.getManufacturer()); + } + } + + public int getYear() { + return year; + } + + public String getColor() { + return color; + } + + public Engine getEngine() { + return copyEngine(); + } + + public List getWheels() { + return copyWheels(); + } + + public Car changeColor(String newColor) { + return new Car(year, newColor, copyWheels(), copyEngine()); + } + + public Car changeEngine(Engine engine) { + return new Car(year, color, copyWheels(), + new Engine(engine.getHorsePower(), engine.getManufacturer())); + } + + public Car addWheel(Wheel newWheel) { + if (newWheel == null) { + return null; + } + List copiedWheels = copyWheels(); + copiedWheels.add(new Wheel(newWheel.getRadius())); + return new Car(year, color, copiedWheels, copyEngine()); + } + + private Engine copyEngine() { + if (engine == null) { + return null; + } + return new Engine(engine.getHorsePower(), engine.getManufacturer()); + } + + private List copyWheels() { + if (wheels == null) { + return null; + } + ArrayList newWheels = new ArrayList<>(); + for (Wheel wheel : wheels) { + newWheels.add(new Wheel(wheel.getRadius())); + } + return newWheels; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + Car car = (Car) obj; + return this.year == car.year + && Objects.equals(this.color, car.color) + && this.wheels.equals(car.wheels) + && this.engine.equals(car.engine); + + } + + @Override + public int hashCode() { + return Objects.hash(this.year, this.color, this.wheels, this.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..1e3065774 100644 --- a/src/main/java/core/basesyntax/Engine.java +++ b/src/main/java/core/basesyntax/Engine.java @@ -1,10 +1,57 @@ 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) { + if (manufacturer == null) { + throw new IllegalArgumentException("Manufacturer cannot be null"); + } + this.horsePower = horsePower; + this.manufacturer = manufacturer; + } + + public int getHorsePower() { + return horsePower; + } + + public String getManufacturer() { + return manufacturer; + } + + public void setHorsePower(int horsePower) { + this.horsePower = horsePower; + } + + 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 this.horsePower == engine.horsePower + && Objects.equals(this.manufacturer, engine.manufacturer); + } + + @Override + public int hashCode() { + return Objects.hash(this.horsePower, this.manufacturer); + } + + @Override + public Engine clone() { + return new Engine(this.horsePower, this.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..7ea9509c0 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 final 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 boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + Wheel wheel = (Wheel) obj; + return this.radius == wheel.radius; + } + + @Override + public int hashCode() { + return Objects.hash(this.radius); + } + + @Override + public Wheel clone() { + return new Wheel(this.radius); + } @Override public String toString() {