diff --git a/src/main/java/core/basesyntax/Car.java b/src/main/java/core/basesyntax/Car.java index a84872f46..fbac4be56 100644 --- a/src/main/java/core/basesyntax/Car.java +++ b/src/main/java/core/basesyntax/Car.java @@ -1,25 +1,107 @@ 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; + List newWheesls = new ArrayList<>(); + for (Wheel wheel : wheels) { + newWheesls.add(wheel.clone()); + } + this.wheels = newWheesls; + this.engine = engine == null ? null : engine.clone(); + } + + public String getColor() { + return color; + } + + public Engine getEngine() { + return engine == null ? null : engine.clone(); + } + + public List getWheels() { + List newWheesls = new ArrayList<>(); + for (Wheel wheel : wheels) { + newWheesls.add(wheel.clone()); + } + return newWheesls; + } + + public int getYear() { + return year; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null) { + return false; + } + if (obj.getClass().equals(Car.class)) { + Car current = (Car) obj; + return Objects.equals(this.color, current.color) + && Objects.equals(this.engine, current.engine) + && (this.year == current.year) + && Objects.equals(this.wheels, current.wheels); + } + return false; + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + year; + result = 31 * result + ((color == null) ? 0 : color.hashCode()); + result = 31 * result + ((wheels == null) ? 0 : wheels.hashCode()); + result = 31 * result + ((engine == null) ? 0 : engine.hashCode()); + return result; + } + @Override public String toString() { return "Car{" - + "year=" + year - + ", color='" + color + '\'' - + ", wheels=" + wheels - + ", engine=" + engine - + '}'; + + "year=" + year + + ", color='" + color + '\'' + + ", wheels=" + wheels + + ", engine=" + engine + + '}'; + } + + public Car addWheel(Wheel wheel) { + List newWheels = new ArrayList<>(); + newWheels.addAll(wheels); + if (wheel == null) { + throw new NullPointerException(); + } else { + newWheels.add(wheel); + } + return new Car(year, color, newWheels, engine); + } + + public Car changeColor(String blue) { + return new Car(year, blue, wheels, engine); + } + + public Car changeEngine(Engine otherMaker) { + if (otherMaker == null) { + throw new NullPointerException(); + } + return new Car(year, color, wheels, otherMaker); } } diff --git a/src/main/java/core/basesyntax/Engine.java b/src/main/java/core/basesyntax/Engine.java index a26a75c5f..404dcb003 100644 --- a/src/main/java/core/basesyntax/Engine.java +++ b/src/main/java/core/basesyntax/Engine.java @@ -1,16 +1,66 @@ 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 String getManufacturer() { + return manufacturer; + } + + public void setHorsePower(int horsePower) { + this.horsePower = horsePower; + } + + public void setManufacturer(String manufacturer) { + this.manufacturer = manufacturer; + } + @Override public String toString() { return "Engine{" - + "horsePower=" + horsePower - + ", manufacturer='" + manufacturer + '\'' - + '}'; + + "horsePower=" + horsePower + + ", manufacturer='" + manufacturer + '\'' + + '}'; + } + + @Override + public Engine clone() { + try { + Engine clonedEngine = (Engine) super.clone(); + return clonedEngine; + } catch (CloneNotSupportedException e) { + throw new RuntimeException("Can't create Engine clone"); + } + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Engine engine = (Engine) o; + return getHorsePower() == engine.getHorsePower() + && Objects.equals(getManufacturer(), engine.getManufacturer()); + } + + @Override + public int hashCode() { + return Objects.hash(getHorsePower(), getManufacturer()); } } diff --git a/src/main/java/core/basesyntax/Wheel.java b/src/main/java/core/basesyntax/Wheel.java index a09d6cd9e..8b09a0519 100644 --- a/src/main/java/core/basesyntax/Wheel.java +++ b/src/main/java/core/basesyntax/Wheel.java @@ -1,14 +1,64 @@ package core.basesyntax; -public class Wheel { - private int radius; +import java.util.ArrayList; +import java.util.List; +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 String toString() { return "Wheel{" - + "radius=" + radius - + '}'; + + "radius=" + radius + + '}'; + } + + @Override + public Wheel clone() { + try { + return (Wheel) super.clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeException("Can't clone wheel", e); + } + + } + + private static List getCopy(List wheels) { + List wheelCopy = new ArrayList<>(wheels.size()); + for (Wheel wheel : wheels) { + wheelCopy.add(wheel.clone()); + } + return wheelCopy; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Wheel wheel = (Wheel) o; + return getRadius() == wheel.getRadius(); + } + + @Override + public int hashCode() { + return Objects.hashCode(getRadius()); } }