Skip to content
Open

1 #1259

Show file tree
Hide file tree
Changes from 6 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
87 changes: 71 additions & 16 deletions src/main/java/core/basesyntax/Car.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,80 @@
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 = new ArrayList<>();
if (wheels != null) {
for (Wheel w : wheels) {
this.wheels.add(w.clone());
}
}

this.engine = engine == null ? null : engine.clone();
}

public int getYear() {
return year;
}

public String getColor() {
return color;
}

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

public List<Wheel> getWheels() {
List<Wheel> copy = new ArrayList<>();
for (Wheel w : wheels) {
copy.add(w.clone());
}
return copy;
}

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 = new ArrayList<>(wheels);
newWheels.add(newWheel.clone());
return new Car(year, color, newWheels, engine);
}

@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 String toString() {
return "Car{"
+ "year=" + year
+ ", color='" + color + '\''
+ ", wheels=" + wheels
+ ", engine=" + engine
+ '}';
public int hashCode() {
return Objects.hash(year, color, wheels, engine);
}
}
48 changes: 41 additions & 7 deletions src/main/java/core/basesyntax/Engine.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,50 @@
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 String getManufacturer() {
return manufacturer;
}

public void setHorsePower(int horsePower) {
this.horsePower = horsePower;
}

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

@Override
public Engine clone() {
return new Engine(horsePower, manufacturer);
}

@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
&& java.util.Objects.equals(manufacturer, engine.manufacturer);
}

@Override
public String toString() {
return "Engine{"
+ "horsePower=" + horsePower
+ ", manufacturer='" + manufacturer + '\''
+ '}';
public int hashCode() {
return java.util.Objects.hash(horsePower, manufacturer);
}
}
37 changes: 31 additions & 6 deletions src/main/java/core/basesyntax/Wheel.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
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 radius) {
this.radius = radius;
}

@Override
public Wheel clone() {
return new Wheel(radius);
}

@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 String toString() {
return "Wheel{"
+ "radius=" + radius
+ '}';
public int hashCode() {
return java.util.Objects.hash(radius);
}
}
Loading