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
102 changes: 88 additions & 14 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 {
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;
if (wheels == null) {
throw new NullPointerException("Wheels cannot be null");
}
this.wheels = copyWheels(wheels);
this.engine = engine == null
? null
: new Engine(engine.getHorsePower(), engine.getManufacturer());
}

public int getYear() {
return year;
}

public String getColor() {
return color;
}

@Override
public int hashCode() {
int result = 31 * this.year;
result = 31 * result + (this.color == null ? 0 : this.color.hashCode());
result = 31 * result + (this.wheels == null ? 0 : this.wheels.hashCode());
result = 31 * result + (this.engine == null ? 0 : this.engine.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
} else if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
Car newCar = (Car) obj;
return (this.year == newCar.year)
&& Objects.equals(this.color, newCar.color)
&& this.wheels.equals(newCar.wheels)
&& this.engine.equals(newCar.engine);
}

public List<Wheel> getWheels() {
return new ArrayList<>(copyWheels(this.wheels));
}

public Engine getEngine() {
return this.engine == null
? null
: new Engine(engine.getHorsePower(), engine.getManufacturer());
}

@Override
public String toString() {
return "Car{"
+ "year=" + year
+ ", color='" + color + '\''
+ ", wheels=" + wheels
+ ", engine=" + engine
+ '}';
+ "year=" + year
+ ", color='" + color + '\''
+ ", wheels=" + wheels
+ ", engine=" + engine
+ '}';
}

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

public Car changeEngine(Engine otherMaker) {
Engine newEngine = new Engine(otherMaker.getHorsePower(), otherMaker.getManufacturer());
return new Car(this.year, this.color, this.wheels, newEngine);
}

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

private List<Wheel> copyWheels(List<Wheel> wheels) {
List<Wheel> copy = new ArrayList<>();
for (Wheel size: wheels) {
copy.add(new Wheel(size.getRadius()));
}
return copy;
}
}
57 changes: 53 additions & 4 deletions src/main/java/core/basesyntax/Engine.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,65 @@
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 void setHorsePower(int i) {
this.horsePower = i;
}

public String getManufacturer() {
return manufacturer;
}

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

@Override
public String toString() {
return "Engine{"
+ "horsePower=" + horsePower
+ ", manufacturer='" + manufacturer + '\''
+ '}';
+ "horsePower=" + horsePower
+ ", manufacturer='" + manufacturer + '\''
+ '}';
}

@Override
protected Engine clone() {
try {
return (Engine)super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}

}

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

@Override
public int hashCode() {
int result = 31 * horsePower;
result = 31 * result + (manufacturer == null ? 0 : manufacturer.hashCode());
return result;
}
}
42 changes: 38 additions & 4 deletions src/main/java/core/basesyntax/Wheel.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,48 @@
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 i) {
this.radius = i;
}

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

@Override
public int hashCode() {
return Integer.hashCode(this.radius);
}

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

@Override
protected Wheel clone() {
try {
return (Wheel) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
}
2 changes: 1 addition & 1 deletion src/test/java/core/basesyntax/CarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void set_isEngineInCarChanged() {
}

@Test
public void carConstructor_checkWheelsAfterAddingToCar() {
public void carConstructor_AftecheckWheelsrAddingToCar() {
List<Wheel> expected = List.of(new Wheel(20), new Wheel(13));
Car car = new Car(1999, null, expected, testEngine);
List<Wheel> actual = car.getWheels();
Expand Down
Loading