BAEL-6079 Code examples from article (#13486)
* BAEL-6079 Code examples from article * BAEL-6079 Code examples from article * BAEL-6079 Code examples from article update * BAEL-6079 Code examples from article update
This commit is contained in:
parent
6cc24e0759
commit
c8cd042d64
|
@ -0,0 +1,98 @@
|
|||
package com.baeldung.methods;
|
||||
|
||||
public class Car {
|
||||
|
||||
private final String make;
|
||||
private final String model;
|
||||
private final int year;
|
||||
private final String color;
|
||||
private final boolean automatic;
|
||||
private final int numDoors;
|
||||
private final String features;
|
||||
|
||||
private Car(CarBuilder carBuilder) {
|
||||
this.make = carBuilder.make;
|
||||
this.model = carBuilder.model;
|
||||
this.year = carBuilder.year;
|
||||
this.color = carBuilder.color;
|
||||
this.automatic = carBuilder.automatic;
|
||||
this.numDoors = carBuilder.numDoors;
|
||||
this.features = carBuilder.features;
|
||||
}
|
||||
|
||||
public String getMake() {
|
||||
return make;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public int getYear() {
|
||||
return year;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public boolean isAutomatic() {
|
||||
return automatic;
|
||||
}
|
||||
|
||||
public int getNumDoors() {
|
||||
return numDoors;
|
||||
}
|
||||
|
||||
public String getFeatures() {
|
||||
return features;
|
||||
}
|
||||
|
||||
public static class CarBuilder {
|
||||
|
||||
private final String make;
|
||||
private final String model;
|
||||
private final int year;
|
||||
|
||||
private String color = "unknown";
|
||||
private boolean automatic = false;
|
||||
private int numDoors = 4;
|
||||
private String features = "";
|
||||
|
||||
public CarBuilder(String make, String model, int year) {
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
public CarBuilder color(String color) {
|
||||
this.color = color;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CarBuilder automatic(boolean automatic) {
|
||||
this.automatic = automatic;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CarBuilder numDoors(int numDoors) {
|
||||
this.numDoors = numDoors;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CarBuilder features(String features) {
|
||||
this.features = features;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Car build() {
|
||||
return new Car(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Car [make=" + make + ", model=" + model + ", year=" + year + ", color=" + color + ", automatic=" + automatic + ", numDoors=" + numDoors + ", features=" + features + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.methods;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Motorcycle extends Vehicle implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 5973661295933599929L;
|
||||
|
||||
private int year;
|
||||
private String features = "";
|
||||
|
||||
public Motorcycle() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Motorcycle(String make, String model, String color, int weight, boolean statusNew, int year) {
|
||||
super(make, model, color, weight, statusNew);
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
public Motorcycle(Vehicle vehicle, int year) {
|
||||
super(vehicle);
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
public int getYear() {
|
||||
return year;
|
||||
}
|
||||
|
||||
public void setYear(int year) {
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
public void setFeatures(String features) {
|
||||
this.features = features;
|
||||
}
|
||||
|
||||
public String getFeatures() {
|
||||
return features;
|
||||
}
|
||||
|
||||
public void addMotorcycleFeatures(String... features) {
|
||||
StringBuilder str = new StringBuilder(this.getFeatures());
|
||||
for (String feature : features) {
|
||||
if (!str.toString().isEmpty())
|
||||
str.append(", ");
|
||||
str.append(feature);
|
||||
}
|
||||
this.setFeatures(str.toString());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.methods;
|
||||
|
||||
public class Vehicle {
|
||||
|
||||
static String defaultValue = "DEFAULT";
|
||||
private String make = defaultValue;
|
||||
private String model = defaultValue;
|
||||
private String color = defaultValue;
|
||||
private int weight = 0;
|
||||
private boolean statusNew = true;
|
||||
|
||||
public Vehicle() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Vehicle(String make, String model, String color, int weight, boolean statusNew) {
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
this.color = color;
|
||||
this.weight = weight;
|
||||
this.statusNew = statusNew;
|
||||
}
|
||||
|
||||
public Vehicle(Vehicle vehicle) {
|
||||
this(vehicle.make, vehicle.model, vehicle.color, vehicle.weight, vehicle.statusNew);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Vehicle [make=" + make + ", model=" + model + ", color=" + color + ", weight=" + weight + ", statusNew=" + statusNew + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.methods;
|
||||
|
||||
public class VehicleProcessor {
|
||||
|
||||
Vehicle processVehicle(String make, String model, String color, int weight, boolean status) {
|
||||
return new Vehicle(make, model, color, weight, status);
|
||||
}
|
||||
|
||||
Vehicle processVehicle(Vehicle vehicle) {
|
||||
return new Vehicle(vehicle);
|
||||
}
|
||||
|
||||
Car processCar(Car car) {
|
||||
return new Car.CarBuilder(car.getMake(), car.getModel(), car.getYear()).color(car.getColor())
|
||||
.automatic(car.isAutomatic())
|
||||
.features(car.getFeatures())
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.baeldung.methods;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class VehicleProcessorUnitTest {
|
||||
|
||||
VehicleProcessor vehicleProcessor = new VehicleProcessor();
|
||||
Vehicle vehicle = new Vehicle("Ford", "Focus", "red", 2200, true);
|
||||
|
||||
@Test
|
||||
void givenAllArguments_whenMethodCall_thenVerifyCallIsDoneCorrectly() {
|
||||
Vehicle veh = vehicleProcessor.processVehicle("Ford", "Focus", "red", 2200, true);
|
||||
assertThat(veh.toString()).hasToString(vehicle.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenParameterObject_whenMethodCall_thenVerifyCallIsDoneCorrectly() {
|
||||
Vehicle veh = vehicleProcessor.processVehicle(vehicle);
|
||||
assertThat(veh.toString()).hasToString(vehicle.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJavaBeanPattern_whenMethodCall_thenVerifyCallIsDoneCorrectly() {
|
||||
Motorcycle motorcycle = new Motorcycle("Ducati", "Monster", "yellow", 235, true, 2023);
|
||||
motorcycle.setFeatures("GPS");
|
||||
|
||||
vehicleProcessor.processVehicle(motorcycle);
|
||||
assertThat(motorcycle.getFeatures()).isEqualToIgnoringCase("GPS");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJavaVarargs_whenMethodCall_thenAssertTheReturnedConcatenatedString() {
|
||||
Motorcycle motorcycle = new Motorcycle("Ducati", "Monster", "red", 350, true, 2023);
|
||||
motorcycle.addMotorcycleFeatures("abs");
|
||||
assertThat(motorcycle.getFeatures()).isEqualToIgnoringCase("abs");
|
||||
|
||||
motorcycle.addMotorcycleFeatures("navi", "charger");
|
||||
assertThat(motorcycle.getFeatures()).isEqualToIgnoringCase("abs, navi, charger");
|
||||
|
||||
motorcycle.addMotorcycleFeatures("wifi", "phone", "satellite");
|
||||
assertThat(motorcycle.getFeatures()).isEqualToIgnoringCase("abs, navi, charger, wifi, phone, satellite");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJavaBuilderPattern_whenMethodCall_thenVerifyCallIsDoneCorrectly() {
|
||||
Car car = new Car.CarBuilder("Ford", "Focus", 2023).color("blue")
|
||||
.automatic(true)
|
||||
.features("abs, navi, charger, wifi, phone, satellite")
|
||||
.build();
|
||||
|
||||
Car result = vehicleProcessor.processCar(car);
|
||||
|
||||
assertThat(result.toString()).hasToString(car.toString());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue