Skip to content
Open

1 #1259

Show file tree
Hide file tree
Changes from 5 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
90 changes: 74 additions & 16 deletions src/main/java/core/basesyntax/Car.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,83 @@
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(new Wheel(w));
}
}

this.engine = engine == null ? null : new Engine(engine);
}

public int getYear() {
return year;
}

public String getColor() {
return color;
}

// ❗ MUST return COPY
public Engine getEngine() {
return engine == null ? null : new Engine(engine);
}

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

// ❗ NO mutation → return new Car
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(new Wheel(newWheel));
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);
}
}
46 changes: 37 additions & 9 deletions src/main/java/core/basesyntax/Engine.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
package core.basesyntax;

public class Engine {
private int horsePower;
private String manufacturer;
import java.util.Objects;

//implement this class
public final class Engine {
private final int horsePower;
private final String manufacturer;

public Engine(int horsePower, String manufacturer) {
this.horsePower = horsePower;
this.manufacturer = manufacturer;
}

public Engine(Engine other) {
this.horsePower = other.horsePower;
this.manufacturer = other.manufacturer;
}

public int getHorsePower() {
return horsePower;
}

public String getManufacturer() {
return manufacturer;
}

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

@Override
public String toString() {
return "Engine{"
+ "horsePower=" + horsePower
+ ", manufacturer='" + manufacturer + '\''
+ '}';
public int hashCode() {
return Objects.hash(horsePower, manufacturer);
}
}
36 changes: 29 additions & 7 deletions src/main/java/core/basesyntax/Wheel.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
package core.basesyntax;

public class Wheel {
private int radius;
import java.util.Objects;

//implement this class
public final class Wheel {
private final int radius;

public Wheel(int radius) {
this.radius = radius;
}

public Wheel(Wheel other) {
this.radius = other.radius;
}

public int getRadius() {
return radius;
}

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

@Override
public String toString() {
return "Wheel{"
+ "radius=" + radius
+ '}';
public int hashCode() {
return Objects.hash(radius);
}
}
Loading