Skip to content
Open

done #1263

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 98 additions & 6 deletions src/main/java/core/basesyntax/Car.java
Original file line number Diff line number Diff line change
@@ -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<Wheel> wheels;
private Engine engine;
public final class Car {
private final int year;
private final String color;
private final List<Wheel> wheels;
private final Engine engine;

//implement this class
public Car(int year, String color, List<Wheel> wheels, Engine engine) {
this.year = year;
this.color = color;
if (wheels == null) {
throw new NullPointerException("");
}
List<Wheel> clonedWheels = new ArrayList<>();
for (Wheel wheel : wheels) {
try {
clonedWheels.add((Wheel) wheel.clone());
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
this.wheels = clonedWheels;
if (engine == null) {
this.engine = null;
} else {
this.engine = engine.clone();
}
}

public int getYear() {
return year;
}

public String getColor() {
return color;
}

public List<Wheel> getWheels() {
List<Wheel> clonedWheels = new ArrayList<>();

for (Wheel wheel : this.wheels) {
try {
clonedWheels.add((Wheel) wheel.clone());
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
return clonedWheels;
}

public Engine getEngine() {
if (engine == null) {
return null;
}
return engine.clone();
}

public Car addWheel(Wheel newWheel) {
if (newWheel == null) {
return this;
}

List<Wheel> clonedWheels = new ArrayList<>();
for (Wheel wheel : this.wheels) {
try {
clonedWheels.add((Wheel) wheel.clone());
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
clonedWheels.add(newWheel);
return new Car(year, color, clonedWheels, engine);
}

public Car changeEngine(Engine newEngine) {
return new Car(year, color, wheels, newEngine);
}

public Car changeColor(String color) {
return new Car(year, color, wheels, engine);
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
Car car = (Car) o;
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);
}

@Override
public String toString() {
Expand Down
49 changes: 47 additions & 2 deletions src/main/java/core/basesyntax/Engine.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
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 boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
Engine engine = (Engine) o;
return horsePower == engine.horsePower && Objects.equals(manufacturer, engine.manufacturer);
}

@Override
public int hashCode() {
return Objects.hash(horsePower, manufacturer);
}

@Override
public String toString() {
Expand All @@ -13,4 +48,14 @@ public String toString() {
+ ", manufacturer='" + manufacturer + '\''
+ '}';
}

@Override
public Engine clone() {
try {
Engine clone = (Engine) super.clone();
return clone;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
}
37 changes: 35 additions & 2 deletions src/main/java/core/basesyntax/Wheel.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
package core.basesyntax;

public class Wheel {
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 boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
Wheel wheel = (Wheel) o;
return radius == wheel.radius;
}

@Override
public int hashCode() {
return Objects.hashCode(radius);
}

@Override
public Wheel clone() throws CloneNotSupportedException {
Wheel clonedWheel = (Wheel) super.clone();
clonedWheel.radius = radius;
return clonedWheel;
}

@Override
public String toString() {
Expand Down
Loading