diff --git a/src/main/java/core/basesyntax/Application.java b/src/main/java/core/basesyntax/Application.java index 244d1592..29f285ff 100644 --- a/src/main/java/core/basesyntax/Application.java +++ b/src/main/java/core/basesyntax/Application.java @@ -1,8 +1,20 @@ package core.basesyntax; +import java.util.Date; + public class Application { public static void main(String[] args) { - // test your code here ... + Engine engine = new Engine(); + engine.setModel("V12"); + engine.setManufactureDate(new Date()); + + Car car = new Car(); + car.setEngine(engine); + + Car carClone = car.clone(); + carClone.getEngine().setModel("V8"); + System.out.println(car); + System.out.println(carClone); } } diff --git a/src/main/java/core/basesyntax/Car.java b/src/main/java/core/basesyntax/Car.java index 33b438c2..fd734b26 100644 --- a/src/main/java/core/basesyntax/Car.java +++ b/src/main/java/core/basesyntax/Car.java @@ -1,5 +1,67 @@ package core.basesyntax; -public class Car { +public class Car implements Cloneable { + private String id; + private String model; + private int price; + private String color; + private Engine engine; + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public int getPrice() { + return price; + } + + public void setPrice(int price) { + this.price = price; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public Engine getEngine() { + return engine; + } + + public void setEngine(Engine engine) { + this.engine = engine; + } + + @Override + public Car clone() { + try { + Car carClone = (Car) super.clone(); + carClone.setEngine(engine.clone()); + return carClone; + } catch (CloneNotSupportedException e) { + throw new RuntimeException("Can not clone car.", e); + } + } + + @Override + public String toString() { + return "Car{" + + "id='" + id + '\'' + ", model='" + model + '\'' + ", price=" + price + + ", color='" + color + '\'' + ", engine=" + engine + '}'; + } } diff --git a/src/main/java/core/basesyntax/Engine.java b/src/main/java/core/basesyntax/Engine.java index 5ec863e9..8e1da5d3 100644 --- a/src/main/java/core/basesyntax/Engine.java +++ b/src/main/java/core/basesyntax/Engine.java @@ -1,5 +1,67 @@ package core.basesyntax; -public class Engine { +import java.util.Date; +public class Engine implements Cloneable { + private String id; + private String model; + private String type; + private int price; + private Date manufactureDate; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public int getPrice() { + return price; + } + + public void setPrice(int price) { + this.price = price; + } + + public Date getManufactureDate() { + return manufactureDate; + } + + public void setManufactureDate(Date manufactureDate) { + this.manufactureDate = manufactureDate; + } + + @Override + public Engine clone() { + try { + return (Engine) super.clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeException("Can not clone engine.", e); + } + } + + @Override + public String toString() { + return "Engine{" + + "id='" + id + '\'' + ", model='" + model + '\'' + ", type='" + type + '\'' + + ", price=" + price + ", manufactureDate=" + manufactureDate + '}'; + } }