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
74 changes: 68 additions & 6 deletions src/main/java/core/basesyntax/Car.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,62 @@
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 = wheelsCopy(wheels);
this.engine = (engine == null) ? null : engine.clone();
}

public List<Wheel> wheelsCopy(List<Wheel> wheels) {
List<Wheel> wheelsCopy = new ArrayList<>(wheels.size());
for (Wheel wheel : wheels) {
wheelsCopy.add(wheel.clone());
}
return wheelsCopy;
}

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

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

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

public int getYear() {
return year;
}

public String getColor() {
return color;
}

public List<Wheel> getWheels() {
return wheelsCopy(this.wheels);
}

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

@Override
public String toString() {
Expand All @@ -22,4 +67,21 @@ public String toString() {
+ ", engine=" + engine
+ '}';
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
Car car = (Car) o;
return getYear() == car.getYear()
&& Objects.equals(getColor(), car.getColor())
&& Objects.equals(getWheels(), car.getWheels())
&& Objects.equals(getEngine(), car.getEngine());
}

@Override
public int hashCode() {
return Objects.hash(getYear(), getColor(), getWheels(), getEngine());
}
}
55 changes: 50 additions & 5 deletions src/main/java/core/basesyntax/Engine.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,61 @@
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 void setHorsePower(int horsePower) {
this.horsePower = horsePower;
}

public String getManufacturer() {
return manufacturer;
}

public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}

@Override
public String toString() {
return "Engine{"
+ "horsePower=" + horsePower
+ ", manufacturer='" + manufacturer + '\''
+ '}';
+ "horsePower=" + horsePower
+ ", 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 Engine clone() {
try {
return (Engine) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException("Can't create clone for Engine object", e);
}
}
}
39 changes: 37 additions & 2 deletions src/main/java/core/basesyntax/Wheel.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,49 @@
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 String toString() {
return "Wheel{"
+ "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() {
try {
return (Wheel) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException("Can't create clone for Wheel object", e);
}
}
}
Loading