-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaterials.js
More file actions
67 lines (53 loc) · 2.09 KB
/
materials.js
File metadata and controls
67 lines (53 loc) · 2.09 KB
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
let materials = [
{ name: "Cement", basePrice: 400, price: 400, rating: 4.5, stock: true },
{ name: "PPC Cement", basePrice: 370, price: 370, rating: 4.3, stock: true },
{ name: "Sand", basePrice: 1700, price: 1700, rating: 4.2, stock: false },
{ name: "M-Sand", basePrice: 1400, price: 1400, rating: 4.4, stock: true },
{ name: "Steel", basePrice: 60, price: 60, rating: 4.3, stock: true }
];
function updatePrices() {
materials.forEach(mat => {
let demandFactor = Math.random() * 0.1; // up to 10%
mat.price = Math.round(mat.basePrice + (mat.basePrice * demandFactor));
});
}
const materialList = document.getElementById("materialList");
const budgetInput = document.getElementById("budget");
function displayMaterials() {
materialList.innerHTML = "";
const budget = budgetInput.value;
materials.forEach(mat => {
const card = document.createElement("div");
card.className = "material-card";
let message = "";
let alternative = findAlternative(mat, budget);
if (!mat.stock) {
message = `<p class="out-stock">Out of stock</p>`;
} else if (budget && mat.price > budget && alternative) {
message = `<p style="color:orange">
Too expensive. Suggested alternative: <b>${alternative.name}</b> (₹${alternative.price})
</p>`;
}
card.innerHTML = `
<h3>${mat.name}</h3>
<p>Price: ₹${mat.price}</p>
<p>Rating: ⭐ ${mat.rating}</p>
${message}
${mat.stock ? `<button onclick="orderMaterial('${mat.name}')">Order</button>` : ""}
`;
materialList.appendChild(card);
});
}
function findAlternative(material, budget) {
return materials.find(m =>
m.name !== material.name &&
m.stock &&
(!budget || m.price <= budget)
);
}
function orderMaterial(name) {
alert(name + " added to cart!");
}
budgetInput.addEventListener("input", displayMaterials);
// Initial load
displayMaterials();