diff --git a/src/main/java/core/basesyntax/Car.java b/src/main/java/core/basesyntax/Car.java index a84872f46..4e28c5a09 100644 --- a/src/main/java/core/basesyntax/Car.java +++ b/src/main/java/core/basesyntax/Car.java @@ -1,25 +1,88 @@ 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; + public Car(int year, String color, List wheels, Engine engine) { + this.year = year; + this.color = color; + this.wheels = new ArrayList<>(); + + for (Wheel wheel : wheels) { + this.wheels.add(new Wheel(wheel)); + } + this.engine = engine == null ? null : new Engine(engine); + } + + public int getYear() { + return year; + } + + public String getColor() { + return color; + } + + public List getWheels() { + List copy = new ArrayList<>(); + + for (Wheel wheel : wheels) { + copy.add(new Wheel(wheel)); + } + return copy; + } + + public Engine getEngine() { + return engine == null ? null : new Engine(engine); + } + + @Override + public boolean equals(Object object) { + if (this == object) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } + Car car = (Car) object; + 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 changeColor(String newColor) { + return new Car(year,newColor, getWheels(),engine); + } + + public Car changeEngine(Engine newEngine) { + return new Car(year, color, getWheels(), newEngine); + } + + public Car addWheel(Wheel wheel) { + List newWheels = new ArrayList<>(wheels); + newWheels.add(wheel); + return new Car(year, color, newWheels, engine); + } //implement this class @Override public String toString() { return "Car{" - + "year=" + year - + ", color='" + color + '\'' - + ", wheels=" + wheels - + ", engine=" + engine - + '}'; + + "year=" + year + + ", color='" + color + '\'' + + ", wheels=" + wheels + + ", engine=" + engine + + '}'; } } diff --git a/src/main/java/core/basesyntax/Engine.java b/src/main/java/core/basesyntax/Engine.java index a26a75c5f..877ad4f0f 100644 --- a/src/main/java/core/basesyntax/Engine.java +++ b/src/main/java/core/basesyntax/Engine.java @@ -1,16 +1,67 @@ package core.basesyntax; -public class Engine { +import java.util.Objects; + +public class Engine implements Cloneable { private int horsePower; private String manufacturer; + public Engine(int horsePower, String manufacturer) { + this.horsePower = horsePower; + this.manufacturer = manufacturer; + } + + public Engine(Engine engine) { + this.horsePower = engine.horsePower; + this.manufacturer = engine.manufacturer; + } + + @Override + public Engine clone() { + return new Engine(this); + } + + 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 object) { + if (this == object) { + return true; + } + + if (object == null || getClass() != object.getClass()) { + return false; + } + Engine engine = (Engine) object; + return horsePower == engine.horsePower && Objects.equals(manufacturer, engine.manufacturer); + } + + @Override + public int hashCode() { + return Objects.hash(horsePower, manufacturer); + } + //implement this class @Override public String toString() { return "Engine{" - + "horsePower=" + horsePower - + ", manufacturer='" + manufacturer + '\'' - + '}'; + + "horsePower=" + horsePower + + ", manufacturer='" + manufacturer + '\'' + + '}'; } } diff --git a/src/main/java/core/basesyntax/Wheel.java b/src/main/java/core/basesyntax/Wheel.java index a09d6cd9e..4d45300b8 100644 --- a/src/main/java/core/basesyntax/Wheel.java +++ b/src/main/java/core/basesyntax/Wheel.java @@ -1,14 +1,58 @@ package core.basesyntax; -public class Wheel { +import java.util.Objects; + +public class Wheel implements Cloneable { private int radius; + public Wheel(int radius) { + this.radius = radius; + } + + public Wheel(Wheel wheel) { + this.radius = wheel.radius; + } + + public int getRadius() { + return radius; + } + + public void setRadius(int radius) { + this.radius = radius; + } + + @Override + public boolean equals(Object object) { + if (this == object) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } + Wheel wheel = (Wheel) object; + return radius == wheel.radius; + } + + @Override + public int hashCode() { + return Objects.hashCode(radius); + } //implement this class @Override public String toString() { return "Wheel{" - + "radius=" + radius - + '}'; + + "radius=" + radius + + '}'; + } + + @Override + public Wheel clone() { + try { + Wheel clone = (Wheel) super.clone(); + return clone; + } catch (CloneNotSupportedException e) { + throw new AssertionError(); + } } }