130 lines
4.3 KiB
HTML
130 lines
4.3 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Levi McLean Midterm Part B</title>
|
|
<meta charset="utf-8" lang="en-us">
|
|
</head>
|
|
|
|
<body>
|
|
<h1>Levi McLean Midterm Part B</h1>
|
|
<script>
|
|
const TAX_RATE = 0.13;
|
|
const DISCOUNT_RATE = 0.1;
|
|
|
|
var item1 = {
|
|
name: "apple",
|
|
price: 1.1,
|
|
qty: 4
|
|
};
|
|
|
|
var item2 = {
|
|
name: "banana",
|
|
price: 0.59,
|
|
qty: 2
|
|
};
|
|
|
|
var item3 = {
|
|
name: "pear",
|
|
price: 1.25,
|
|
qty: 6
|
|
};
|
|
|
|
var item4 = {
|
|
name: "grapes",
|
|
price: 2.25,
|
|
qty: 8
|
|
};
|
|
|
|
var item5 = {
|
|
name: "kiwi",
|
|
price: 3,
|
|
qty: 1
|
|
};
|
|
|
|
var cart = [item1, item2, item3, item4, item5];
|
|
|
|
function searchItem() {
|
|
var itemName = window.prompt("Enter an item name: ").toLowerCase();
|
|
var foundItem;
|
|
|
|
if (itemName == null) {
|
|
window.alert("Item name cannot be null.");
|
|
}
|
|
|
|
for (var i=0; i < cart.length; i++) {
|
|
if (cart[i].name === itemName) {
|
|
window.alert("Item(s) found.");
|
|
foundItem = cart[i];
|
|
return foundItem;
|
|
}
|
|
}
|
|
window.alert("Item(s) not found.");
|
|
return undefined;
|
|
}
|
|
|
|
function updateItemQuantity() {
|
|
var updateItem = window.prompt("Enter item name to update: ").toLowerCase();
|
|
|
|
if (updateItem == null) {
|
|
window.alert("Invalid item name.");
|
|
}
|
|
|
|
for (var i=0; i < cart.length; i++) {
|
|
if (cart[i].name === updateItem) {
|
|
var updateQty = parseInt(window.prompt("How many items do we add? "), 10);
|
|
cart[i].qty += updateQty;
|
|
console.log(cart);
|
|
return updateItem;
|
|
}
|
|
}
|
|
window.alert("Item does not exist in cart.");
|
|
return undefined;
|
|
}
|
|
|
|
function clearCart() {
|
|
for (var i = 0; i < cart.length; i++) {
|
|
cart[i].name = "";
|
|
cart[i].qty = 0;
|
|
}
|
|
console.log(cart);
|
|
window.alert("Cart has been cleared!");
|
|
}
|
|
|
|
function calulateFinalPriceWithDiscount() {
|
|
var subtotal = 0;
|
|
for (var i = 0; i < cart.length; i++) {
|
|
subtotal += cart[i].price * cart[i].qty;
|
|
}
|
|
var tax = subtotal * TAX_RATE;
|
|
var discount = subtotal * DISCOUNT_RATE;
|
|
var total = subtotal - discount + tax;
|
|
window.alert(
|
|
"Subtotal: $" + subtotal.toFixed(2) + "\n" +
|
|
"Tax: $" + tax.toFixed(2) + "\n" +
|
|
"Discount: $" + discount.toFixed(2) + "\n" +
|
|
"Total: $" + total.toFixed(2)
|
|
);
|
|
}
|
|
|
|
var notDone = true;
|
|
while(notDone === true) {
|
|
var userChoice = window.prompt("Please make a selection (s)earch, (u)pdate, (c)lear, (d)iscount: ").toLowerCase();
|
|
if (userChoice === "s") {
|
|
searchItem();
|
|
notDone = window.confirm("Do you wish to continue?");
|
|
} else if (userChoice === "u") {
|
|
updateItemQuantity();
|
|
notDone = window.confirm("Do you wish to continue?");
|
|
} else if (userChoice === "c") {
|
|
clearCart();
|
|
notDone = window.confirm("Do you wish to continue?");
|
|
} else if (userChoice === "d") {
|
|
calulateFinalPriceWithDiscount();
|
|
notDone = window.confirm("Do you wish to continue?");
|
|
} else {
|
|
window.alert("Invalid selection, try again.");
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
</html> |