-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproductClass.js
51 lines (51 loc) · 1.94 KB
/
productClass.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class CreatProduct{
constructor(title, price, category, shipment, description, madeInCountry, inStock, availablity){
this.title = title;
this.price = price;
this.category= category;
this.shipment = shipment;
this.description = description;
this.madeInCountry = madeInCountry;
this.inStock = inStock;
this.availablity = availablity;
}
buy(numberAsked, submittedCode = ``){
let totalPrice = 0;
let discountCode = `576CGR`;
submittedCode = submittedCode.toUpperCase();
try {
if (numberAsked > this.availablity){
throw new TypeError()
}
else{
this.availablity = this.availablity - numberAsked;
if(this.availablity == 0){
this.inStock = !this.inStock;
}
totalPrice = this.price * numberAsked;
if (submittedCode == discountCode){
totalPrice = totalPrice - (totalPrice * 15 / 100)
console.log(`Discounted price is ${totalPrice}`)
}
else if (submittedCode != `` && submittedCode != discountCode){
console.log(`Overall price is ${totalPrice} Because your discount code is invalid`)
}
else if (submittedCode == ``){
console.log(`Overall price is ${totalPrice}`)
}
}
}catch(err){
console.log(`Error! Currently we don't have ${numberAsked} in stock`)
}
}
addToStock (howMany){
if (this.availablity == 0){
this.inStock = !this.inStock
}
this.availablity = this.availablity + howMany;
}
}
let product1 = new CreatProduct(`Men's Running Shoes Nike`, 149.99, `Shoes`, `Premium`, `Great Running Shoes!`, `Italy`, true, 5);
product1.addToStock(5)
product1.buy(5,`576C1GR`);
console.log(product1)