diff --git a/src/lab8_builder/Car.java b/src/lab8_builder/Car.java new file mode 100644 index 0000000..4e20a7f --- /dev/null +++ b/src/lab8_builder/Car.java @@ -0,0 +1,70 @@ +package lab8_builder; + +public class Car { + private String power; + private String engine; + private String breaks; + private String seats; + private String windows; + private String fuelType; + private final String carType; + public Car (String carType){ + this.carType = carType; + } + public String getPower() { + return power; + } + public void setPower(String power) { + this.power = power; + } + public String getEngine() { + return engine; + } + public void setEngine(String engine) { + this.engine = engine; + } + public String getBreaks() { + return breaks; + } + public void setBreaks(String breaks) { + this.breaks = breaks; + } + public String getSeats() { + return seats; + } + public void setSeats(String seats) { + this.seats = seats; + } + public String getWindows() { + return windows; + } + public void setWindows(String windows) { + this.windows = windows; + } + public String getFuelType() { + return fuelType; + } + public void setFuelType(String fuelType) { + this.fuelType = fuelType; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("--------------"+carType+"--------------------- \n"); + sb.append("\n Power: "); + sb.append(power); + sb.append("\n Engine: "); + sb.append(engine); + sb.append("\n Breaks: "); + sb.append(breaks); + sb.append("\n Seats: "); + sb.append(seats); + sb.append("\n Windows: "); + sb.append(windows); + sb.append("\n Fuel Type: "); + sb.append(fuelType); + + return sb.toString(); + } +} diff --git a/src/lab8_builder/CarBuilder.java b/src/lab8_builder/CarBuilder.java new file mode 100644 index 0000000..6357e1e --- /dev/null +++ b/src/lab8_builder/CarBuilder.java @@ -0,0 +1,20 @@ +package lab8_builder; + +public abstract class CarBuilder { + protected Car car; + + protected abstract String getCarType(); + + public Car getCar() { + return car; + } + + public void setCar(Car car) { + this.car = car; + } + + public abstract void buildEngine(); + public abstract void buildBreaks(); + public abstract void buildSeats(); + public abstract void buildWindows(); +} diff --git a/src/lab8_builder/CarFactory.java b/src/lab8_builder/CarFactory.java new file mode 100644 index 0000000..332c302 --- /dev/null +++ b/src/lab8_builder/CarFactory.java @@ -0,0 +1,20 @@ +package lab8_builder; + +public class CarFactory { + private CarBuilder builder; + private Car car; + + public void setBuilder(CarBuilder builder) { + this.builder = builder; + } + + public Car construct() { + car = new Car(builder.getCarType()); + builder.setCar(car); + builder.buildEngine(); + builder.buildBreaks(); + builder.buildSeats(); + builder.buildWindows(); + return builder.getCar(); + } +} diff --git a/src/lab8_builder/Main.java b/src/lab8_builder/Main.java new file mode 100644 index 0000000..d9cc941 --- /dev/null +++ b/src/lab8_builder/Main.java @@ -0,0 +1,22 @@ +package lab8_builder; + +import lab8_builder.builders.BerlinBuilder; +import lab8_builder.builders.SportBuilder; + +public class Main { + public static void main(String[] args) { + CarFactory factory = new CarFactory(); + + CarBuilder builder1 = new BerlinBuilder(); + CarBuilder builder2 = new SportBuilder(); + + factory.setBuilder(builder1); + Car car1 = factory.construct(); + + factory.setBuilder(builder2); + Car car2 = factory.construct(); + + System.out.println(car1); + System.out.println(car2); + } +} diff --git a/src/lab8_builder/builders/BerlinBuilder.java b/src/lab8_builder/builders/BerlinBuilder.java new file mode 100644 index 0000000..4087250 --- /dev/null +++ b/src/lab8_builder/builders/BerlinBuilder.java @@ -0,0 +1,32 @@ +package lab8_builder.builders; + +import lab8_builder.CarBuilder; + +public class BerlinBuilder extends CarBuilder { + @Override + protected String getCarType() { + return "Berlin"; + } + + @Override + public void buildEngine() { + car.setEngine("V12"); + car.setFuelType("Diesel"); + car.setPower("12'000 Horses"); + } + + @Override + public void buildBreaks() { + car.setBreaks("Premium Double Tungsten-Platinum Alloy"); + } + + @Override + public void buildSeats() { + car.setSeats("5"); + } + + @Override + public void buildWindows() { + car.setWindows("Tinted bullet-proof"); + } +} diff --git a/src/lab8_builder/builders/SportBuilder.java b/src/lab8_builder/builders/SportBuilder.java new file mode 100644 index 0000000..3887189 --- /dev/null +++ b/src/lab8_builder/builders/SportBuilder.java @@ -0,0 +1,32 @@ +package lab8_builder.builders; + +import lab8_builder.CarBuilder; + +public class SportBuilder extends CarBuilder { + @Override + protected String getCarType() { + return "Sports"; + } + + @Override + public void buildEngine() { + car.setEngine("V8"); + car.setFuelType("Unleaded 95"); + car.setPower("7'000 horses"); + } + + @Override + public void buildBreaks() { + car.setBreaks("Holo-magnetic and semi-cyclonic"); + } + + @Override + public void buildSeats() { + car.setSeats("2"); + } + + @Override + public void buildWindows() { + car.setWindows("Standard"); + } +}