Skip to content
Open
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
93 changes: 79 additions & 14 deletions src/main/java/core/basesyntax/Car.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,90 @@
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;
this.wheels = copyWheels(wheels);
this.engine = (engine == null) ? null : engine.clone();
}

public int getYear() {
return year;
}

public String getColor() {
return color;
}

public List<Wheel> getWheels() {
return copyWheels(wheels);
}

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

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

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

public Car addWheel(Wheel newWheel) {
List<Wheel> newWheels = copyWheels(wheels);
newWheels.add(newWheel.clone());
return new Car(year, color, newWheels, engine);
}

private List<Wheel> copyWheels(List<Wheel> wheels) {
Objects.requireNonNull(wheels, "Wheels list cannot be null");
List<Wheel> copy = new ArrayList<>();
for (Wheel w : wheels) {
if (w != null) {
copy.add(w.clone());
}
}
return copy;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Car)) {
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() {
return "Car{"
+ "year=" + year
+ ", color='" + color + '\''
+ ", wheels=" + wheels
+ ", engine=" + engine
+ '}';
+ "year=" + year
+ ", color='" + color + '\''
+ ", wheels=" + wheels
+ ", engine=" + engine
+ '}';
}
}
58 changes: 53 additions & 5 deletions src/main/java/core/basesyntax/Engine.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,64 @@
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;
}

@Override
public Engine clone() {
try {
return (Engine) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException("Clone not supported", e);
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Engine)) {
return false;
}
Engine engine = (Engine) o;
return horsePower == engine.horsePower
&& Objects.equals(manufacturer, engine.manufacturer);
}

@Override
public int hashCode() {
return Objects.hash(horsePower, 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 + '\''
+ '}';
}
}
46 changes: 42 additions & 4 deletions src/main/java/core/basesyntax/Wheel.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,52 @@
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;
}

@Override
public Wheel clone() {
try {
return (Wheel) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException("Clone not supported", e);
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Wheel)) {
return false;
}
Wheel wheel = (Wheel) o;
return radius == wheel.radius;
}

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

public void setRadius(int radius) {
this.radius = radius;
}

@Override
public String toString() {
return "Wheel{"
+ "radius=" + radius
+ '}';
+ "radius=" + radius
+ '}';
}
}
Loading