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
100 changes: 94 additions & 6 deletions src/main/java/core/basesyntax/Car.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,66 @@
package core.basesyntax;

import java.util.ArrayList;
import java.util.List;

/**
* 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 implements Cloneable {
private final int year;
private final String color;
private final List<Wheel> wheels = new ArrayList<>();
private final Engine engine;

//implement this class
public Car(int year, String color, List<Wheel> wheels, Engine engine) {
this.year = year;
this.color = color;
for (Wheel wheel : wheels) {
this.wheels.add(wheel.clone());
}
if (engine != null) {
this.engine = new Engine(engine);
} else {
this.engine = null;
}
}

public Car changeColor(String newColor) {
return new Car(year, newColor, wheels, engine);
}

public Car changeEngine(Engine engine) {
return new Car(year, color, wheels, engine);
}

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

public int getYear() {
return year;
}

public String getColor() {
return color;
}

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

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

@Override
public String toString() {
Expand All @@ -22,4 +71,43 @@ public String toString() {
+ ", engine=" + engine
+ '}';
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}

if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Car car = (Car) obj;

return car.color.equals(this.color)
&& car.wheels.equals(this.wheels)
&& car.engine.equals(this.engine)
&& this.year == car.year;
}

@Override
public int hashCode() {
int hash = 7;
hash = 37 * hash + year;
hash = 37 * hash + (color == null ? 0 : color.hashCode());
hash = 37 * hash + wheels.hashCode();
hash = 37 * hash + (engine == null ? 0 : engine.hashCode());
return hash;
}

@Override
protected Car clone() {
try {
return (Car) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException("Can't clone car", e);
}
}
}
59 changes: 57 additions & 2 deletions src/main/java/core/basesyntax/Engine.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
package core.basesyntax;

public class Engine {
public final 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 Engine(Engine engine) {
this.horsePower = engine.horsePower;
this.manufacturer = engine.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 String toString() {
Expand All @@ -13,4 +37,35 @@ public String toString() {
+ ", manufacturer='" + manufacturer + '\''
+ '}';
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}

if (obj == null) {
return false;
}

if (getClass() != obj.getClass()) {
return false;
}

Engine engine = (Engine) obj;
return engine.manufacturer.equals(manufacturer) && engine.horsePower == horsePower;
}

@Override
public int hashCode() {
int result = 5;
result = 31 * result + horsePower;
result = 31 * result + (manufacturer == null ? 0 : manufacturer.hashCode());
return result;
}

@Override
protected Engine clone() {
return new Engine(horsePower, manufacturer);
}
}
49 changes: 47 additions & 2 deletions src/main/java/core/basesyntax/Wheel.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,54 @@
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 Wheel(Wheel wheel) {
this.radius = wheel.radius;
}

public int getRadius() {
return radius;
}

public void setRadius(int radius) {
this.radius = radius;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}

if (obj == null) {
return false;
}

if (getClass() != obj.getClass()) {
return false;
}

Wheel wheel = (Wheel) obj;
return radius == wheel.radius;
}

@Override
public int hashCode() {
int result = 5;
result = 31 * result + radius;
return result;
}

@Override
protected Wheel clone() {
return new Wheel(radius);

}

@Override
public String toString() {
Expand Down
Loading