From 206843abb1d392319a391ba223df656f4d3c305b Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Fri, 4 Oct 2024 14:03:55 +0200 Subject: [PATCH] added lab1 ex1 --- src/lab1_decorator/ex1/AirbagDecorator.java | 22 +++++++++++ .../ex1/AntiSlidingSystemDecorator.java | 22 +++++++++++ .../ex1/BreakingSystemDecorator.java | 22 +++++++++++ src/lab1_decorator/ex1/Car.java | 7 ++++ src/lab1_decorator/ex1/CarBerlin.java | 18 +++++++++ src/lab1_decorator/ex1/CarBreak.java | 18 +++++++++ src/lab1_decorator/ex1/CarDecorator.java | 24 ++++++++++++ .../ex1/CarDecoratorLauncher.java | 38 +++++++++++++++++++ src/lab1_decorator/ex1/CarSport.java | 18 +++++++++ src/lab1_decorator/ex1/ColorDecorator.java | 18 +++++++++ src/lab1_decorator/ex1/EngineDecorator.java | 17 +++++++++ 11 files changed, 224 insertions(+) create mode 100644 src/lab1_decorator/ex1/AirbagDecorator.java create mode 100644 src/lab1_decorator/ex1/AntiSlidingSystemDecorator.java create mode 100644 src/lab1_decorator/ex1/BreakingSystemDecorator.java create mode 100644 src/lab1_decorator/ex1/Car.java create mode 100644 src/lab1_decorator/ex1/CarBerlin.java create mode 100644 src/lab1_decorator/ex1/CarBreak.java create mode 100644 src/lab1_decorator/ex1/CarDecorator.java create mode 100644 src/lab1_decorator/ex1/CarDecoratorLauncher.java create mode 100644 src/lab1_decorator/ex1/CarSport.java create mode 100644 src/lab1_decorator/ex1/ColorDecorator.java create mode 100644 src/lab1_decorator/ex1/EngineDecorator.java diff --git a/src/lab1_decorator/ex1/AirbagDecorator.java b/src/lab1_decorator/ex1/AirbagDecorator.java new file mode 100644 index 0000000..e65b7c0 --- /dev/null +++ b/src/lab1_decorator/ex1/AirbagDecorator.java @@ -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"; + } +} diff --git a/src/lab1_decorator/ex1/AntiSlidingSystemDecorator.java b/src/lab1_decorator/ex1/AntiSlidingSystemDecorator.java new file mode 100644 index 0000000..23b9b7d --- /dev/null +++ b/src/lab1_decorator/ex1/AntiSlidingSystemDecorator.java @@ -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"; + } +} diff --git a/src/lab1_decorator/ex1/BreakingSystemDecorator.java b/src/lab1_decorator/ex1/BreakingSystemDecorator.java new file mode 100644 index 0000000..3fbb9a2 --- /dev/null +++ b/src/lab1_decorator/ex1/BreakingSystemDecorator.java @@ -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"; + } +} diff --git a/src/lab1_decorator/ex1/Car.java b/src/lab1_decorator/ex1/Car.java new file mode 100644 index 0000000..1686c5e --- /dev/null +++ b/src/lab1_decorator/ex1/Car.java @@ -0,0 +1,7 @@ +package lab1_decorator.ex1; + +public interface Car { + String getDescription(); + double getCost(); + double getSecurityLevel(); +} diff --git a/src/lab1_decorator/ex1/CarBerlin.java b/src/lab1_decorator/ex1/CarBerlin.java new file mode 100644 index 0000000..254dd1c --- /dev/null +++ b/src/lab1_decorator/ex1/CarBerlin.java @@ -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; + } +} diff --git a/src/lab1_decorator/ex1/CarBreak.java b/src/lab1_decorator/ex1/CarBreak.java new file mode 100644 index 0000000..572c94b --- /dev/null +++ b/src/lab1_decorator/ex1/CarBreak.java @@ -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; + } +} diff --git a/src/lab1_decorator/ex1/CarDecorator.java b/src/lab1_decorator/ex1/CarDecorator.java new file mode 100644 index 0000000..d9e4f88 --- /dev/null +++ b/src/lab1_decorator/ex1/CarDecorator.java @@ -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(); + } +} diff --git a/src/lab1_decorator/ex1/CarDecoratorLauncher.java b/src/lab1_decorator/ex1/CarDecoratorLauncher.java new file mode 100644 index 0000000..e289ed9 --- /dev/null +++ b/src/lab1_decorator/ex1/CarDecoratorLauncher.java @@ -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(); + } +} \ No newline at end of file diff --git a/src/lab1_decorator/ex1/CarSport.java b/src/lab1_decorator/ex1/CarSport.java new file mode 100644 index 0000000..55d1986 --- /dev/null +++ b/src/lab1_decorator/ex1/CarSport.java @@ -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; + } +} diff --git a/src/lab1_decorator/ex1/ColorDecorator.java b/src/lab1_decorator/ex1/ColorDecorator.java new file mode 100644 index 0000000..9246b53 --- /dev/null +++ b/src/lab1_decorator/ex1/ColorDecorator.java @@ -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"; + } +} diff --git a/src/lab1_decorator/ex1/EngineDecorator.java b/src/lab1_decorator/ex1/EngineDecorator.java new file mode 100644 index 0000000..5413080 --- /dev/null +++ b/src/lab1_decorator/ex1/EngineDecorator.java @@ -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"; + } +}