sdfxs

Untitled

Jun 1st, 2021
910
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Equipment {
  2.     private price: number = 0;
  3.     private name: string = '';
  4.  
  5.     getPrice() {
  6.         return this.price || 0;
  7.     }
  8.  
  9.     getName() {
  10.         return this.name;
  11.     }
  12.  
  13.     setName(name: string) {
  14.         this.name = name;
  15.     }
  16.  
  17.     setPrice(price: number) {
  18.         this.price = price;
  19.     }
  20. }
  21.  
  22. class Engine extends Equipment {
  23.     constructor() {
  24.         super();
  25.         this.setName('Engine');
  26.         this.setPrice(800);
  27.     }
  28. }
  29.  
  30. class Body extends Equipment {
  31.     constructor() {
  32.         super();
  33.         this.setName('Body');
  34.         this.setPrice(3000);
  35.     }
  36. }
  37.  
  38. class Tools extends Equipment {
  39.     constructor() {
  40.         super();
  41.         this.setName('Tools');
  42.         this.setPrice(4000);
  43.     }
  44. }
  45.  
  46. class Composite extends Equipment {
  47.     private equipments: Equipment[];
  48.     constructor() {
  49.         super();
  50.         this.equipments = [];
  51.     }
  52.  
  53.     add(equipment: Equipment) {
  54.         this.equipments.push(equipment);
  55.         return this;
  56.     }
  57.  
  58.     getPrice() {
  59.         return this.equipments
  60.             .map(equipment => equipment.getPrice())
  61.             .reduce((a, b) => a + b);
  62.     }
  63. }
  64.  
  65. class Car extends Composite {
  66.     constructor() {
  67.         super();
  68.         this.setName('Audi');
  69.     }
  70. }
  71.  
  72. const myCar = new Car().add(new Engine()).add(new Body()).add(new Tools());
  73.  
  74. console.log(`${myCar.getName()} price is ${myCar.getPrice()}`);
RAW Paste Data