Skip to content
Open

1 #1259

Show file tree
Hide file tree
Changes from 1 commit
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
99 changes: 84 additions & 15 deletions src/main/java/core/basesyntax/Car.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,94 @@
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
// constructor with deep copy
public Car(int year, String color, List<Wheel> wheels, Engine engine) {
this.year = year;
this.color = color;

// deep copy of list
this.wheels = new ArrayList<>();
if (wheels != null) {
for (Wheel w : wheels) {
this.wheels.add(new Wheel(w));
}
}

// deep copy of engine
this.engine = engine == null ? null : new Engine(engine);
}

public int getYear() {
return year;
}

public String getColor() {
return color;
}

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

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

// "change engine" = new Car
public Car changeEngine(Engine newEngine) {
return new Car(this.year, this.color, this.wheels, newEngine);
}

// "change color" = new Car
public Car changeColor(String newColor) {
return new Car(this.year, newColor, this.wheels, this.engine);
}

// "add wheel" = new Car
public Car addWheel(Wheel newWheel) {
List<Wheel> newWheels = new ArrayList<>(this.wheels);
newWheels.add(new Wheel(newWheel));
return new Car(this.year, this.color, newWheels, this.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
+ '}';
return "Car{" +
"year=" + year +
", color='" + color + '\'' +
", wheels=" + wheels +
", engine=" + engine +
'}';
}
}
51 changes: 43 additions & 8 deletions src/main/java/core/basesyntax/Engine.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,51 @@
package core.basesyntax;

public class Engine {
private int horsePower;
private String manufacturer;
public final class Engine {
private final int horsePower;
private final String manufacturer;

//implement this class
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;
}

// "upgrade engine" = create new instance
public Engine changeEngine(Engine engine) {
return new Engine(engine);
}

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

@Override
public int hashCode() {
return java.util.Objects.hash(horsePower, manufacturer);
}

@Override
public String toString() {
return "Engine{"
+ "horsePower=" + horsePower
+ ", manufacturer='" + manufacturer + '\''
+ '}';
return "Engine{" +
"horsePower=" + horsePower +
", manufacturer='" + manufacturer + '\'' +
'}';
}
}
33 changes: 27 additions & 6 deletions src/main/java/core/basesyntax/Wheel.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
package core.basesyntax;

public class Wheel {
private int radius;
public final class Wheel {
private final int radius;

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

public int getRadius() {
return radius;
}

public Wheel(Wheel other) {
this.radius = other.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 int hashCode() {
return Integer.hashCode(radius);
}

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