Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions patterns/basket.js
Original file line number Diff line number Diff line change
@@ -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()
}
}
64 changes: 64 additions & 0 deletions patterns/card.js
Original file line number Diff line number Diff line change
@@ -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")
}
}
33 changes: 33 additions & 0 deletions patterns/check.js
Original file line number Diff line number Diff line change
@@ -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)
);
}
}
50 changes: 50 additions & 0 deletions patterns/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="basket.js"></script>
<script src="products.js"></script>
<script src="check.js"></script>
<script src="card.js"></script>
<script src="https://smtpjs.com/v3/smtp.js"></script>
</head>
<body>
</body>
<script>

//create a shopping basket
const myBasket = new Basket();

//create a food factory and get three apples,
// two bottles of water and 2 chocolates
const factory = new ProductFactory();
const apple = factory.take("Apple", 3);
const water = factory.take("Bottle of water", 2);
const chocolate = factory.take("Chocolate", 2);

//add products to basket
myBasket.addProduct(apple);
myBasket.addProduct(water);
myBasket.addProduct(chocolate);

//use the gold card strategy
const cardStrategy = new CardStrategy("GoldCardStrategy", myBasket).chooseStrategy();
console.log(myBasket.getTotalPrice());

//get a discount on a gold card
cardStrategy.makeDiscount();

//get a list of products and a total price
console.log(myBasket.getProducts());
console.log(myBasket.getTotalPrice());

//create a check and a letter
let check = new Check();
check = new Letter(check)

//send the check to the mail
check.sendCheck()

</script>
</html>
40 changes: 40 additions & 0 deletions patterns/products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//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
}

//method for getting the type of a product
getName() {
return this.name
}
}

//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);
if (type === "Bottle of water")
return new Product(type, number, number*20);
if (type === "Chocolate")
return new Product(type, number, number*40);
}
}
100 changes: 100 additions & 0 deletions patterns/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="basket.js"></script>
<script src="products.js"></script>
<script src="check.js"></script>
<script src="card.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.14.1.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="https://code.jquery.com/qunit/qunit-2.14.1.js"></script>

<script>

//create two baskets
const myBasket1 = new Basket();
const myBasket2 = new Basket();


//create a food factory
//add different products to a different basket
const factory = new ProductFactory();
const apple = factory.take("Apple", 3);
const water = factory.take("Bottle of water", 2);

//add products to baskets
myBasket1.addProduct(apple);
myBasket2.addProduct(water);

//singleton test (basket).
QUnit.module('Singleton testing', () => {
//when creating two baskets,
// the second basket must refer to the first one
QUnit.test('should be one basket', (assert) => {
assert.deepEqual(myBasket1, myBasket2)
});
//when creating two baskets and adding products to them,
// the second basket must refer to the first one and when
// calculating the total price, the price should be equal
// to the sum of all products added to the two baskets.
QUnit.test('should be one total price', (assert) => {
assert.equal(myBasket1.getTotalPrice(), myBasket2.getTotalPrice())
})
})

//test products
QUnit.module('Product list testing', () => {
//check the products added to the basket and their quantity
QUnit.test('should be apple', (assert) => {
assert.propEqual(myBasket1.getProducts()[0], {
name: "Apple",
number: 3,
price: 30
})
});
QUnit.test('should be bottle of water', (assert) => {
assert.propEqual(myBasket2.getProducts()[1], {
name: "Bottle of water",
number: 2,
price: 40
})
});
})

//check the card
QUnit.module('Card testing', () => {

//use the gold card strategy
const cardStrategy = new CardStrategy("GoldCardStrategy", myBasket1).chooseStrategy();

//get a discount on a gold card
cardStrategy.makeDiscount();

//check if the discount is valid
QUnit.test('price should be 56 after discount', (assert) => {

//gold card has 20% discount
//the price should be 70 - 20%
assert.equal(myBasket1.getTotalPrice(), 56)
});

//check using points
QUnit.test('price should be 54 after using points in card', (assert) => {

//use points from the card
cardStrategy.usePoints();

//two points are awarded for each purchase,
//after using the points, the price should be 56 - 2
assert.equal(myBasket1.getTotalPrice(), 54)
})
})

</script>

</body>