diff --git a/src/main/java/core/basesyntax/Car.java b/src/main/java/core/basesyntax/Car.java index a84872f46..d6c3f6f01 100644 --- a/src/main/java/core/basesyntax/Car.java +++ b/src/main/java/core/basesyntax/Car.java @@ -1,25 +1,99 @@ 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("Wheels cannot be null"); + } + this.wheels = copyWheels(wheels); + this.engine = engine == null + ? null + : new Engine(engine.getHorsePower(), engine.getManufacturer()); + } + + public int getYear() { + return year; + } + + public String getColor() { + return color; + } + + @Override + public int hashCode() { + int result = 31 * this.year; + result = 31 * result + (this.color == null ? 0 : this.color.hashCode()); + result = 31 * result + (this.wheels == null ? 0 : this.wheels.hashCode()); + result = 31 * result + (this.engine == null ? 0 : this.engine.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } else if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + Car newCar = (Car) obj; + return (this.year == newCar.year) + && Objects.equals(this.color, newCar.color) + && this.wheels.equals(newCar.wheels) + && this.engine.equals(newCar.engine); + } + + public List getWheels() { + return new ArrayList<>(copyWheels(this.wheels)); + } + + public Engine getEngine() { + return this.engine == null + ? null + : new Engine(engine.getHorsePower(), engine.getManufacturer()); + } @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 newList = new ArrayList<>(this.wheels); + newList.add(wheel); + return new Car(this.year, this.color, newList, this.engine); + } + + public Car changeEngine(Engine otherMaker) { + Engine newEngine = new Engine(otherMaker.getHorsePower(), otherMaker.getManufacturer()); + return new Car(this.year, this.color, this.wheels, newEngine); + } + + public Car changeColor(String blue) { + return new Car(this.year, blue, this.wheels, this.engine); + } + + private List copyWheels(List wheels) { + List copy = new ArrayList<>(); + for (Wheel size: wheels) { + copy.add(new Wheel(size.getRadius())); + } + return copy; } } diff --git a/src/main/java/core/basesyntax/Engine.java b/src/main/java/core/basesyntax/Engine.java index a26a75c5f..16fa3e751 100644 --- a/src/main/java/core/basesyntax/Engine.java +++ b/src/main/java/core/basesyntax/Engine.java @@ -1,16 +1,65 @@ package core.basesyntax; -public class Engine { +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 i) { + this.horsePower = i; + } + + public String getManufacturer() { + return manufacturer; + } + + public void setManufacturer(String newMaker) { + this.manufacturer = newMaker; + } + @Override public String toString() { return "Engine{" - + "horsePower=" + horsePower - + ", manufacturer='" + manufacturer + '\'' - + '}'; + + "horsePower=" + horsePower + + ", manufacturer='" + manufacturer + '\'' + + '}'; + } + + @Override + protected Engine clone() { + try { + return (Engine)super.clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeException(e); + } + + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } else if (obj == null || this.getClass() != obj.getClass()) { + return false; + } + return this.horsePower == ((Engine) obj).horsePower + && this.manufacturer == ((Engine) obj).manufacturer; + } + + @Override + public int hashCode() { + int result = 31 * horsePower; + result = 31 * result + (manufacturer == null ? 0 : manufacturer.hashCode()); + return result; } } diff --git a/src/main/java/core/basesyntax/Wheel.java b/src/main/java/core/basesyntax/Wheel.java index a09d6cd9e..d7c1baa58 100644 --- a/src/main/java/core/basesyntax/Wheel.java +++ b/src/main/java/core/basesyntax/Wheel.java @@ -1,14 +1,48 @@ package core.basesyntax; -public class Wheel { +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 i) { + this.radius = i; + } @Override public String toString() { return "Wheel{" - + "radius=" + radius - + '}'; + + "radius=" + radius + + '}'; + } + + @Override + public int hashCode() { + return Integer.hashCode(this.radius); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } else if (obj == null || getClass() != obj.getClass()) { + return false; + } + return this.radius == ((Wheel) obj).radius; + } + + @Override + protected Wheel clone() { + try { + return (Wheel) super.clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeException(e); + } } } diff --git a/src/test/java/core/basesyntax/CarTest.java b/src/test/java/core/basesyntax/CarTest.java index 62a971ee5..c93d6274a 100644 --- a/src/test/java/core/basesyntax/CarTest.java +++ b/src/test/java/core/basesyntax/CarTest.java @@ -43,7 +43,7 @@ public void set_isEngineInCarChanged() { } @Test - public void carConstructor_checkWheelsAfterAddingToCar() { + public void carConstructor_AftecheckWheelsrAddingToCar() { List expected = List.of(new Wheel(20), new Wheel(13)); Car car = new Car(1999, null, expected, testEngine); List actual = car.getWheels();