added lab8 ex1
This commit is contained in:
parent
475afc0db5
commit
80b7f9e80d
70
src/lab8_builder/Car.java
Normal file
70
src/lab8_builder/Car.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
20
src/lab8_builder/CarBuilder.java
Normal file
20
src/lab8_builder/CarBuilder.java
Normal file
@ -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();
|
||||
}
|
20
src/lab8_builder/CarFactory.java
Normal file
20
src/lab8_builder/CarFactory.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
22
src/lab8_builder/Main.java
Normal file
22
src/lab8_builder/Main.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
32
src/lab8_builder/builders/BerlinBuilder.java
Normal file
32
src/lab8_builder/builders/BerlinBuilder.java
Normal file
@ -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");
|
||||
}
|
||||
}
|
32
src/lab8_builder/builders/SportBuilder.java
Normal file
32
src/lab8_builder/builders/SportBuilder.java
Normal file
@ -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");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user