-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.js
149 lines (132 loc) · 3.85 KB
/
cart.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"use strict";
let products = [];
let avProducts = ["product-1", "product-2","product-3","product-4","product-5","product-6","product-7","product-8","product-9"];
const addProduct = function () {
const productName = document.getElementById("product-name").value;
const price = document.getElementById("price").value;
const quantity = document.getElementById("quantity").value;
if (!validateInputs(productName, price, quantity)) {
alert("Invalid Inputs");
return;
}
const prodcut = {
productName: productName,
price: price,
quantity: quantity,
getTotal: function () {
return this.price * this.quantity;
},
};
products.push(prodcut);
display();
};
const display = function () {
displayProducts();
displaySubTotal();
displayShipping();
displayTotal();
};
const displayProducts = function () {
document.getElementById("products").innerHTML = "";
products.forEach((p, i) => {
document.getElementById("products").innerHTML += ` <tr>
<td class="align-middle">
<img src="img/${p.productName}.jpg" alt="" style="width: 50px" />
${p.productName}
</td>
<td class="align-middle">EGP ${p.price}</td>
<td class="align-middle">
<div
class="input-group quantity mx-auto"
style="width: 100px"
>
<div class="input-group-btn">
<button class="btn btn-sm btn-primary btn-minus" onclick="decQuantity(${i})">
<i class="fa fa-minus"></i>
</button>
</div>
<input
type="text"
class="form-control form-control-sm bg-secondary border-0 text-center"
value="${p.quantity}"
/>
<div class="input-group-btn">
<button class="btn btn-sm btn-primary btn-plus" onclick="incQuantity(${i})">
<i class="fa fa-plus"></i>
</button>
</div>
</div>
</td>
<td class="align-middle">EGP ${p.getTotal()}</td>
<td class="align-middle">
<button class="btn btn-sm btn-danger" onclick="remove(${i})">
<i class="fa fa-times"></i>
</button>
</td>
</tr>`;
});
};
const displaySubTotal = function () {
document.getElementById("sub-total").innerHTML = `EGP ${calculateSubTotal()}`;
};
const displayShipping = function () {
let shipping = calculateShipping();
document.getElementById("shipping").innerHTML = `EGP ${shipping}`;
};
const calculateSubTotal = function () {
let total = 0;
// for (let i = 0; i < products.length; i++) {
// total += products[i].total;
// }
//continue
//break
products.forEach((p) => {
total += p.getTotal();
});
return total;
};
const displayTotal = function () {
let total = calculateSubTotal() + calculateShipping();
document.getElementById("total").innerHTML = `EGP ${total}`;
};
const calculateShipping = function () {
return Math.round(calculateSubTotal() * 0.1);
};
const validateInputs = function (productName, price, quantity) {
if (productName == "" || price == "" || quantity == "") {
return false;
}
if (Number(quantity) <= 0 || Number(price) <=0){
return false;
}
if (!isNaN(Number(productName))) {
return false;
}
if(!avProducts.includes(productName)){
return false;
}
if (isNaN(Number(price)) || isNaN(Number(quantity))) {
return false;
}
if (!Number.isInteger(Number(quantity))) {
return false;
}
console.log("reached");
return true;
};
const decQuantity = function (i) {
if (products[i].quantity > 1) {
products[i].quantity -= 1;
display();
}
};
const incQuantity = function (i) {
products[i].quantity = Number(products[i].quantity) + 1;
display();
};
const remove = function (i) {
if (confirm("Are you sure?")) {
products.splice(i, 1);
display();
}
};