added lab1 ex1

This commit is contained in:
Louis Heredero 2024-10-04 14:03:55 +02:00
parent f2fb3f7e47
commit 206843abb1
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
11 changed files with 224 additions and 0 deletions

View File

@ -0,0 +1,22 @@
package lab1_decorator.ex1;
public class AirbagDecorator extends CarDecorator {
public AirbagDecorator(Car car) {
super(car);
}
@Override
public double getCost() {
return 10 + super.getCost();
}
@Override
public double getSecurityLevel() {
return 10 + super.getSecurityLevel();
}
@Override
public String getDescription() {
return super.getDescription() + ", Airbag System";
}
}

View File

@ -0,0 +1,22 @@
package lab1_decorator.ex1;
public class AntiSlidingSystemDecorator extends CarDecorator {
public AntiSlidingSystemDecorator(Car car) {
super(car);
}
@Override
public double getCost() {
return 20 + super.getCost();
}
@Override
public double getSecurityLevel() {
return 5 + super.getSecurityLevel();
}
@Override
public String getDescription() {
return super.getDescription() + ", Anti Sliding System";
}
}

View File

@ -0,0 +1,22 @@
package lab1_decorator.ex1;
public class BreakingSystemDecorator extends CarDecorator {
public BreakingSystemDecorator(Car car) {
super(car);
}
@Override
public double getCost() {
return 6 + super.getCost();
}
@Override
public double getSecurityLevel() {
return 8 + super.getSecurityLevel();
}
@Override
public String getDescription() {
return super.getDescription() + ", Breaking System";
}
}

View File

@ -0,0 +1,7 @@
package lab1_decorator.ex1;
public interface Car {
String getDescription();
double getCost();
double getSecurityLevel();
}

View File

@ -0,0 +1,18 @@
package lab1_decorator.ex1;
public class CarBerlin implements Car {
@Override
public String getDescription() {
return "A beautiful Berlin";
}
@Override
public double getCost() {
return 100;
}
@Override
public double getSecurityLevel() {
return 2;
}
}

View File

@ -0,0 +1,18 @@
package lab1_decorator.ex1;
public class CarBreak implements Car {
@Override
public String getDescription() {
return "A family break";
}
@Override
public double getCost() {
return 50;
}
@Override
public double getSecurityLevel() {
return 0;
}
}

View File

@ -0,0 +1,24 @@
package lab1_decorator.ex1;
public abstract class CarDecorator implements Car {
public Car car;
public String name;
public CarDecorator(Car car) {
this.car = car;
}
public String getDescription() {
return car.getDescription();
}
@Override
public double getCost() {
return car.getCost();
}
@Override
public double getSecurityLevel() {
return car.getSecurityLevel();
}
}

View File

@ -0,0 +1,38 @@
package lab1_decorator.ex1;
public class CarDecoratorLauncher {
public static void main(String[] args) {
CarDecoratorLauncher launcher = new CarDecoratorLauncher();
launcher.launch();
}
public void launch() {
Car car1 = new CarBerlin();
car1 = new AirbagDecorator(car1);
car1 = new AirbagDecorator(car1);
car1 = new EngineDecorator(car1);
car1 = new ColorDecorator(car1);
printCarInfos(car1);
Car car2 = new CarSport();
car2 = new AntiSlidingSystemDecorator(car2);
car2 = new BreakingSystemDecorator(car2);
printCarInfos(car2);
Car car3 = new CarBreak();
car3 = new AirbagDecorator(car3);
car3 = new EngineDecorator(car3);
car3 = new ColorDecorator(car3);
car3 = new AntiSlidingSystemDecorator(car3);
car3 = new BreakingSystemDecorator(car3);
printCarInfos(car3);
}
public void printCarInfos(Car car) {
System.out.println("Car description : " + car.getDescription());
System.out.println("Car cost : " + car.getCost());
System.out.println("Car security level : " + car.getSecurityLevel());
System.out.println();
}
}

View File

@ -0,0 +1,18 @@
package lab1_decorator.ex1;
public class CarSport implements Car {
@Override
public String getDescription() {
return "A robust sport car";
}
@Override
public double getCost() {
return 300;
}
@Override
public double getSecurityLevel() {
return 1;
}
}

View File

@ -0,0 +1,18 @@
package lab1_decorator.ex1;
public class ColorDecorator extends CarDecorator {
public ColorDecorator(Car car) {
super(car);
}
@Override
public double getCost() {
return 3 + super.getCost();
}
@Override
public String getDescription() {
return super.getDescription() + ", Color";
}
}

View File

@ -0,0 +1,17 @@
package lab1_decorator.ex1;
public class EngineDecorator extends CarDecorator {
public EngineDecorator(Car car) {
super(car);
}
@Override
public double getCost() {
return 20 + super.getCost();
}
@Override
public String getDescription() {
return super.getDescription() + ", Engine";
}
}