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
104 changes: 93 additions & 11 deletions src/main/java/core/basesyntax/Car.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,107 @@
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;
List<Wheel> newWheesls = new ArrayList<>();
for (Wheel wheel : wheels) {
newWheesls.add(wheel.clone());
}
this.wheels = newWheesls;
this.engine = engine == null ? null : engine.clone();
}

public String getColor() {
return color;
}

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

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

public int getYear() {
return year;
}

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

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

@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> newWheels = new ArrayList<>();
newWheels.addAll(wheels);
if (wheel == null) {
throw new NullPointerException();
} else {
newWheels.add(wheel);
}
return new Car(year, color, newWheels, engine);
}

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

public Car changeEngine(Engine otherMaker) {
if (otherMaker == null) {
throw new NullPointerException();
}
return new Car(year, color, wheels, otherMaker);
}
}
60 changes: 55 additions & 5 deletions src/main/java/core/basesyntax/Engine.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,66 @@
package core.basesyntax;

public class Engine {
import java.util.Objects;

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

public void setHorsePower(int horsePower) {
this.horsePower = horsePower;
}

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

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

@Override
public Engine clone() {
try {
Engine clonedEngine = (Engine) super.clone();
return clonedEngine;
} catch (CloneNotSupportedException e) {
throw new RuntimeException("Can't create Engine clone");
}
}

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

@Override
public int hashCode() {
return Objects.hash(getHorsePower(), getManufacturer());
}
}
58 changes: 54 additions & 4 deletions src/main/java/core/basesyntax/Wheel.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,64 @@
package core.basesyntax;

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

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 radius) {
this.radius = radius;
}

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

@Override
public Wheel clone() {
try {
return (Wheel) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException("Can't clone wheel", e);
}

}

private static List<Wheel> getCopy(List<Wheel> wheels) {
List<Wheel> wheelCopy = new ArrayList<>(wheels.size());
for (Wheel wheel : wheels) {
wheelCopy.add(wheel.clone());
}
return wheelCopy;
}

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

@Override
public int hashCode() {
return Objects.hashCode(getRadius());
}
}
Loading