Skip to content
Open

1 #1259

Show file tree
Hide file tree
Changes from 4 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
94 changes: 84 additions & 10 deletions src/main/java/core/basesyntax/Car.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,99 @@
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 {
public final class Car {
private int year;
private String color;
private List<Wheel> wheels;
private 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();
}

// ✅ REQUIRED GETTERS (fix your error)

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;
}

// "immutable-style" updates (as tests expect)
public Car changeEngine(Engine engine) {
this.engine = engine.clone();
return this;
}

public Car changeColor(String newColor) {
this.color = newColor;
return this;
}

public Car addWheel(Wheel newWheel) {
this.wheels.add(newWheel.clone());
return this;
}

@Override
public Car clone() {
return new Car(year, color, wheels, engine);
}

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

// TESTS REQUIRE setters
public void setHorsePower(int horsePower) {
this.horsePower = horsePower;
}

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

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

public Engine changeEngine(Engine engine) {
return engine.clone();
}

@Override
public String toString() {
return "Engine{"
+ "horsePower=" + horsePower
+ ", manufacturer='" + manufacturer + '\''
+ '}';
+ "horsePower=" + horsePower
+ ", manufacturer='" + manufacturer + '\''
+ '}';
}
}
24 changes: 20 additions & 4 deletions src/main/java/core/basesyntax/Wheel.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
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;
}

// TESTS REQUIRE setter
public void setRadius(int radius) {
this.radius = radius;
}

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

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