added lab1 ex1
This commit is contained in:
parent
f2fb3f7e47
commit
206843abb1
22
src/lab1_decorator/ex1/AirbagDecorator.java
Normal file
22
src/lab1_decorator/ex1/AirbagDecorator.java
Normal 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";
|
||||
}
|
||||
}
|
22
src/lab1_decorator/ex1/AntiSlidingSystemDecorator.java
Normal file
22
src/lab1_decorator/ex1/AntiSlidingSystemDecorator.java
Normal 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";
|
||||
}
|
||||
}
|
22
src/lab1_decorator/ex1/BreakingSystemDecorator.java
Normal file
22
src/lab1_decorator/ex1/BreakingSystemDecorator.java
Normal 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";
|
||||
}
|
||||
}
|
7
src/lab1_decorator/ex1/Car.java
Normal file
7
src/lab1_decorator/ex1/Car.java
Normal file
@ -0,0 +1,7 @@
|
||||
package lab1_decorator.ex1;
|
||||
|
||||
public interface Car {
|
||||
String getDescription();
|
||||
double getCost();
|
||||
double getSecurityLevel();
|
||||
}
|
18
src/lab1_decorator/ex1/CarBerlin.java
Normal file
18
src/lab1_decorator/ex1/CarBerlin.java
Normal 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;
|
||||
}
|
||||
}
|
18
src/lab1_decorator/ex1/CarBreak.java
Normal file
18
src/lab1_decorator/ex1/CarBreak.java
Normal 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;
|
||||
}
|
||||
}
|
24
src/lab1_decorator/ex1/CarDecorator.java
Normal file
24
src/lab1_decorator/ex1/CarDecorator.java
Normal 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();
|
||||
}
|
||||
}
|
38
src/lab1_decorator/ex1/CarDecoratorLauncher.java
Normal file
38
src/lab1_decorator/ex1/CarDecoratorLauncher.java
Normal 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();
|
||||
}
|
||||
}
|
18
src/lab1_decorator/ex1/CarSport.java
Normal file
18
src/lab1_decorator/ex1/CarSport.java
Normal 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;
|
||||
}
|
||||
}
|
18
src/lab1_decorator/ex1/ColorDecorator.java
Normal file
18
src/lab1_decorator/ex1/ColorDecorator.java
Normal 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";
|
||||
}
|
||||
}
|
17
src/lab1_decorator/ex1/EngineDecorator.java
Normal file
17
src/lab1_decorator/ex1/EngineDecorator.java
Normal 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";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user