diff --git a/patterns/basket.js b/patterns/basket.js new file mode 100644 index 0000000..66b4131 --- /dev/null +++ b/patterns/basket.js @@ -0,0 +1,48 @@ +//Singleton +//Basket in one instance +class Basket { + //saved the reference to the instance in a static constructor property + constructor() { + //In the constructor it is checked whether the + // instance exists and if it does not exist, + // create it and start referring to it, if it exists, + // then the constructor receives a reference to it + if (typeof Basket.instance === 'object') { + return Basket.instance; + } + this.products = []; + this.totalPrice = 0; + Basket.instance = this; + return this; + } + + //method for calculating the price of all products in the basket + calculatePrice(){ + let sum = 0; + this.products.forEach(function (elem){ + sum = sum + elem.getPrice() + }) + this.totalPrice = sum + } + + //method for getting the total price + getTotalPrice() { + return this.totalPrice; + } + + //method for changing total price + setTotalPrice(totalPrice) { + this.totalPrice = totalPrice; + } + + //method for getting an array of products in the basket + getProducts() { + return this.products; + } + + //method for adding a product to the basket + addProduct(product) { + this.products.push(product) + this.calculatePrice() + } +} \ No newline at end of file diff --git a/patterns/card.js b/patterns/card.js new file mode 100644 index 0000000..e2a1c4e --- /dev/null +++ b/patterns/card.js @@ -0,0 +1,64 @@ +//Strategy +//class for defining strategies for different cards +class CardStrategy { + constructor(card, basket) { + this.card = card + this.basket = basket + } + + //method for determining the strategy, depending on the chosen card + chooseStrategy(){ + switch (this.card) { + case "SilverCardStrategy": + return new SilverCardStrategy(this.basket) + case "GoldCardStrategy": + return new GoldCardStrategy(this.basket) + default: + return new NoCardStrategy() + } + } +} +//strategy for the silver card +class SilverCardStrategy{ + constructor(basket) { + this.basket = basket; + } + + //method for getting 10% discount + makeDiscount() { + this.basket.setTotalPrice(this.basket.getTotalPrice() * 0.9) + } +} + +//strategy for the gold card +class GoldCardStrategy{ + constructor(basket) { + this.points = 0; + this.basket = basket; + } + + //method for getting 20% discount + makeDiscount() { + this.basket.setTotalPrice(this.basket.getTotalPrice() * 0.8) + this.getPoints(); + } + + //method for getting points + //on the card(2 points are awarded for each purchase) + getPoints() { + this.points = this.points + 2 + } + + //method for using points on a card + usePoints() { + this.basket.setTotalPrice(this.basket.getTotalPrice() - this.points) + this.points = 0 + } +} + +//strategy if there is no card +class NoCardStrategy{ + makeDiscount() { + console.log("Sorry, you don't have card") + } +} \ No newline at end of file diff --git a/patterns/check.js b/patterns/check.js new file mode 100644 index 0000000..2607b04 --- /dev/null +++ b/patterns/check.js @@ -0,0 +1,33 @@ +//class inherited from the basket +class Check extends Basket { + constructor() { + super(); + } +} + +//Decorator +//class extending the possibilities of the check +class Letter { + constructor(check) { + this.check = check; + } + + //method for sending a check to the mail + sendCheck() { + this.sendEmail() + } + + sendEmail() { + Email.send({ + Host : "smtp.mail.ru", + Username : "11111@mail.ru", + Password : "11111", + To : '11111@mail.ru', + From : "11111@mail.ru", + Subject : "Your check", + Body : `The price of your products: ${this.check.getTotalPrice()}` + }).then( + message => alert(message) + ); + } +} \ No newline at end of file diff --git a/patterns/main.html b/patterns/main.html new file mode 100644 index 0000000..8e37f28 --- /dev/null +++ b/patterns/main.html @@ -0,0 +1,50 @@ + + +
+ +