From d631629d75ad474efa4b659613d41cba90014c21 Mon Sep 17 00:00:00 2001 From: ShGulnaz Date: Sat, 3 Apr 2021 03:41:31 +0300 Subject: [PATCH 1/5] add patterns --- patterns/basket.js | 37 ++++++++++++++++++++++++++++++++ patterns/card.js | 51 ++++++++++++++++++++++++++++++++++++++++++++ patterns/check.js | 30 ++++++++++++++++++++++++++ patterns/main.html | 33 ++++++++++++++++++++++++++++ patterns/products.js | 27 +++++++++++++++++++++++ 5 files changed, 178 insertions(+) create mode 100644 patterns/basket.js create mode 100644 patterns/card.js create mode 100644 patterns/check.js create mode 100644 patterns/main.html create mode 100644 patterns/products.js diff --git a/patterns/basket.js b/patterns/basket.js new file mode 100644 index 0000000..48b0ed6 --- /dev/null +++ b/patterns/basket.js @@ -0,0 +1,37 @@ +//Singleton +class Basket { + constructor() { + if (typeof Basket.instance === 'object') { + return Basket.instance; + } + this.products = []; + this.totalPrice = 0; + Basket.instance = this; + return this; + } + + calculatePrice(){ + let sum = 0; + this.products.forEach(function (elem){ + sum = sum + elem.getPrice() + }) + this.totalPrice = sum + } + + getTotalPrice() { + return this.totalPrice; + } + + setTotalPrice(totalPrice) { + this.totalPrice = totalPrice; + } + + getProducts() { + return this.products; + } + + 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..fe6c356 --- /dev/null +++ b/patterns/card.js @@ -0,0 +1,51 @@ +//Strategy +class CardStrategy { + constructor(card) { + this.card = card + } + + chooseStrategy(){ + switch (this.card) { + case "SilverCardStrategy": + return new SilverCardStrategy() + case "GoldCardStrategy": + return new GoldCardStrategy() + case "NoCardStrategy": + return new NoCardStrategy() + default: + return + } + } +} + +class SilverCardStrategy{ + + makeDiscount() { + myBasket.setTotalPrice(myBasket.getTotalPrice() * 0.9) + } +} + +class GoldCardStrategy{ + constructor() { + this.points = 0; + } + + makeDiscount() { + myBasket.setTotalPrice(myBasket.getTotalPrice() * 0.8) + this.getPoints(); + } + + getPoints() { + this.points = this.points * 0.1 + } + + usePoints() { + myBasket.setTotalPrice(myBasket.getTotalPrice() - this.points) + } +} + +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..1323675 --- /dev/null +++ b/patterns/check.js @@ -0,0 +1,30 @@ +class Check extends Basket { + constructor() { + super(); + } +} + +//Decorator +class Letter { + constructor(check) { + this.check = check; + } + + 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..9316397 --- /dev/null +++ b/patterns/main.html @@ -0,0 +1,33 @@ + + + + + Title + + + + + + + + + + \ No newline at end of file diff --git a/patterns/products.js b/patterns/products.js new file mode 100644 index 0000000..4b88ed7 --- /dev/null +++ b/patterns/products.js @@ -0,0 +1,27 @@ +//Factory pattern +class Product { + constructor(name, number, price) { + this.name = name; + this.number = number; + this.price = price; + } + + getPrice() { + return this.price; + } + + getNumber() { + return this.number + } +} + +class ProductFactory { + take(type, number) { + if (type === "Apple") + return new Product(type, number, number*10); + if (type === "Bottle of water") + return new Product(type, number, number*20); + if (type === "Chocolate") + return new Product(type, number, number*40); + } +} \ No newline at end of file From b987348df5e6a07faa1ae453b5cc7e5d82aea9bd Mon Sep 17 00:00:00 2001 From: ShGulnaz Date: Fri, 9 Apr 2021 02:33:56 +0300 Subject: [PATCH 2/5] add comments --- patterns/basket.js | 11 +++++++++++ patterns/card.js | 18 +++++++++++++----- patterns/check.js | 3 +++ patterns/main.html | 19 ++++++++++++++++++- patterns/products.js | 8 ++++++++ 5 files changed, 53 insertions(+), 6 deletions(-) diff --git a/patterns/basket.js b/patterns/basket.js index 48b0ed6..66b4131 100644 --- a/patterns/basket.js +++ b/patterns/basket.js @@ -1,6 +1,12 @@ //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; } @@ -10,6 +16,7 @@ class Basket { return this; } + //method for calculating the price of all products in the basket calculatePrice(){ let sum = 0; this.products.forEach(function (elem){ @@ -18,18 +25,22 @@ class Basket { 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() diff --git a/patterns/card.js b/patterns/card.js index fe6c356..a782460 100644 --- a/patterns/card.js +++ b/patterns/card.js @@ -1,49 +1,57 @@ //Strategy +//class for defining strategies for different cards class CardStrategy { constructor(card) { this.card = card } + //method for determining the strategy, depending on the chosen card chooseStrategy(){ switch (this.card) { case "SilverCardStrategy": return new SilverCardStrategy() case "GoldCardStrategy": return new GoldCardStrategy() - case "NoCardStrategy": - return new NoCardStrategy() default: - return + return new NoCardStrategy() } } } - +//strategy for the silver card class SilverCardStrategy{ + //method for getting 10% discount makeDiscount() { myBasket.setTotalPrice(myBasket.getTotalPrice() * 0.9) } } +//strategy for the gold card class GoldCardStrategy{ constructor() { this.points = 0; } + //method for getting 20% discount makeDiscount() { myBasket.setTotalPrice(myBasket.getTotalPrice() * 0.8) this.getPoints(); } + //method for getting points + //on the card(2 points are awarded for each purchase) getPoints() { - this.points = this.points * 0.1 + this.points = this.points + 2 } + //method for using points on a card usePoints() { myBasket.setTotalPrice(myBasket.getTotalPrice() - this.points) + this.points = 0 } } +//strategy if there is no card class NoCardStrategy{ makeDiscount() { console.log("Sorry, you don't have card") diff --git a/patterns/check.js b/patterns/check.js index 1323675..2607b04 100644 --- a/patterns/check.js +++ b/patterns/check.js @@ -1,3 +1,4 @@ +//class inherited from the basket class Check extends Basket { constructor() { super(); @@ -5,11 +6,13 @@ class Check extends Basket { } //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() } diff --git a/patterns/main.html b/patterns/main.html index 9316397..5228eb5 100644 --- a/patterns/main.html +++ b/patterns/main.html @@ -13,20 +13,37 @@ diff --git a/patterns/products.js b/patterns/products.js index 4b88ed7..b741580 100644 --- a/patterns/products.js +++ b/patterns/products.js @@ -1,21 +1,29 @@ //Factory pattern +//super class, on the basis of which the same +//objects with different values are created class Product { + //a constructor that takes the name of the + //product, the quantity of products and the price of the product constructor(name, number, price) { this.name = name; this.number = number; this.price = price; } + //method for getting the price of a product getPrice() { return this.price; } + //method for getting the quantity of a product getNumber() { return this.number } } +//factory class for products class ProductFactory { + //a product is created depending on the type and quantity take(type, number) { if (type === "Apple") return new Product(type, number, number*10); From a7bfe78f980375b621d28d2f71342ed2c4583b84 Mon Sep 17 00:00:00 2001 From: ShGulnaz Date: Fri, 9 Apr 2021 23:30:37 +0300 Subject: [PATCH 3/5] add getName of product --- patterns/products.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/patterns/products.js b/patterns/products.js index b741580..01749aa 100644 --- a/patterns/products.js +++ b/patterns/products.js @@ -19,6 +19,11 @@ class Product { getNumber() { return this.number } + + //method for getting the type of a product + getName() { + return this.name + } } //factory class for products From b078ba7d88667e36d9bd7b8a89b0dce26ddda79b Mon Sep 17 00:00:00 2001 From: ShGulnaz Date: Fri, 9 Apr 2021 23:57:13 +0300 Subject: [PATCH 4/5] fix card and add basket in constructor --- patterns/card.js | 19 ++++++++++++------- patterns/main.html | 2 +- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/patterns/card.js b/patterns/card.js index a782460..e2a1c4e 100644 --- a/patterns/card.js +++ b/patterns/card.js @@ -1,17 +1,18 @@ //Strategy //class for defining strategies for different cards class CardStrategy { - constructor(card) { + 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() + return new SilverCardStrategy(this.basket) case "GoldCardStrategy": - return new GoldCardStrategy() + return new GoldCardStrategy(this.basket) default: return new NoCardStrategy() } @@ -19,22 +20,26 @@ class CardStrategy { } //strategy for the silver card class SilverCardStrategy{ + constructor(basket) { + this.basket = basket; + } //method for getting 10% discount makeDiscount() { - myBasket.setTotalPrice(myBasket.getTotalPrice() * 0.9) + this.basket.setTotalPrice(this.basket.getTotalPrice() * 0.9) } } //strategy for the gold card class GoldCardStrategy{ - constructor() { + constructor(basket) { this.points = 0; + this.basket = basket; } //method for getting 20% discount makeDiscount() { - myBasket.setTotalPrice(myBasket.getTotalPrice() * 0.8) + this.basket.setTotalPrice(this.basket.getTotalPrice() * 0.8) this.getPoints(); } @@ -46,7 +51,7 @@ class GoldCardStrategy{ //method for using points on a card usePoints() { - myBasket.setTotalPrice(myBasket.getTotalPrice() - this.points) + this.basket.setTotalPrice(this.basket.getTotalPrice() - this.points) this.points = 0 } } diff --git a/patterns/main.html b/patterns/main.html index 5228eb5..8e37f28 100644 --- a/patterns/main.html +++ b/patterns/main.html @@ -29,7 +29,7 @@ myBasket.addProduct(chocolate); //use the gold card strategy - const cardStrategy = new CardStrategy("GoldCardStrategy").chooseStrategy(); + const cardStrategy = new CardStrategy("GoldCardStrategy", myBasket).chooseStrategy(); console.log(myBasket.getTotalPrice()); //get a discount on a gold card From 197439104cec1db115a7ce80c6bc393587ac5d41 Mon Sep 17 00:00:00 2001 From: Yulia Mihaylova Date: Sat, 10 Apr 2021 02:08:22 +0300 Subject: [PATCH 5/5] test for shop --- patterns/test.html | 100 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 patterns/test.html diff --git a/patterns/test.html b/patterns/test.html new file mode 100644 index 0000000..dd546ea --- /dev/null +++ b/patterns/test.html @@ -0,0 +1,100 @@ + + + + + Title + + + + + + + +
+
+ + + + + \ No newline at end of file