29 October 2024 4:46:38 Dusan Rodina - softwareideas.net

Add Comment

Anonymous 29 October 2024 4:46:39

SDA

class Car {

private String color;

private String engineType;

private String transmission;

// Private constructor

private Car(CarBuilder builder) {

this.color = builder.color;

this.engineType = builder.engineType;

this.transmission = builder.transmission;

}

public static class CarBuilder {

private String color;

private String engineType;

private String transmission;

public CarBuilder setColor(String color) {

this.color = color;

return this;

}

public CarBuilder setEngineType(String engineType) {

this.engineType = engineType;

return this;

}

public CarBuilder setTransmission(String transmission) {

this.transmission = transmission;

return this;

}

public Car build() {

return new Car(this);

}

}

}

// Usage:

Car car = new Car.CarBuilder()

.setColor("Red")

.setEngineType("V8")

.setTransmission("Automatic")

.build();