CODE FOR CANTEEN FOOD ORDERING SYSTEM HTML, CSS AND JAVASCRIPT

<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<title>Canteen Food Ordering System</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
color: #333;
margin: 0;
padding: 20px;
}

.container {
max-width: 800px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

header {
text-align: center;
margin-bottom: 20px;
}

h1 {
margin: 0;
font-size: 24px;
}

.menu-search {
margin-bottom: 20px;
text-align: center;
}

.menu-search input {
width: 80%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
margin-bottom: 20px;
}

.menu {
margin-bottom: 20px;
}

.menu-item {
display: flex;
justify-content: space-between;
padding: 10px 0;
border-bottom: 1px solid #ddd;
align-items: center;
}

.quantity-selector {
display: flex;
align-items: center;
}

.quantity-selector input {
width: 50px;
text-align: center;
margin: 0 5px;
padding: 5px;
border: 1px solid #ccc;
border-radius: 4px;
}

.add-btn {
background-color: #28a745;
color: #fff;
border: none;
padding: 5px 10px;
border-radius: 4px;
cursor: pointer;
}

.add-btn:hover {
background-color: #218838;
}

.price {
font-weight: bold;
}

.order-summary {
margin-bottom: 20px;
}

#order-list {
list-style: none;
padding: 0;
margin: 0;
}

.order-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 5px 0;
border-bottom: 1px solid #ddd;
}

.order-item span {
flex-grow: 1;
}

.remove-btn {
background-color: #dc3545;
color: #fff;
border: none;
padding: 5px;
border-radius: 4px;
cursor: pointer;
}

.remove-btn:hover {
background-color: #c82333;
}

.total {
margin-top: 10px;
font-size: 18px;
}

#checkout-btn {
display: block;
width: 100%;
background-color: #007bff;
color: #fff;
padding: 10px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
margin-top: 20px;
}

#checkout-btn:hover {
background-color: #0056b3;
}

.checkout-form {
display: none;
}

.checkout-form h2 {
margin-top: 0;
}

#order-form label {
display: block;
margin-bottom: 5px;
}

#order-form input {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}

#order-form button {
width: 100%;
padding: 10px;
background-color: #28a745;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}

#order-form button:hover {
background-color: #218838;
}
</style>
</head>
<body>
<div class=”container”>
<header>
<h1>Canteen Food Ordering System</h1>
</header>

<div class=”menu-search”>
<input type=”text” id=”search-bar” placeholder=”Search for a dish…”>
</div>

<section class=”menu”>
<h2>Menu</h2>
<div class=”menu-item” data-price=”5″>
<span>Burger</span>
<div class=”quantity-selector”>
<label for=”burger-qty”>Qty:</label>
<input type=”number” id=”burger-qty” value=”1″ min=”1″>
</div>
<button class=”add-btn”>Add</button>
<span class=”price”>$5</span>
</div>
<div class=”menu-item” data-price=”3″>
<span>Fries</span>
<div class=”quantity-selector”>
<label for=”fries-qty”>Qty:</label>
<input type=”number” id=”fries-qty” value=”1″ min=”1″>
</div>
<button class=”add-btn”>Add</button>
<span class=”price”>$3</span>
</div>
<div class=”menu-item” data-price=”2″>
<span>Drink</span>
<div class=”quantity-selector”>
<label for=”drink-qty”>Qty:</label>
<input type=”number” id=”drink-qty” value=”1″ min=”1″>
</div>
<button class=”add-btn”>Add</button>
<span class=”price”>$2</span>
</div>
</section>

<section class=”order-summary”>
<h2>Your Order</h2>
<ul id=”order-list”></ul>
<div class=”total”>
<strong>Total: $</strong><span id=”total-price”>0</span>
</div>
<button id=”checkout-btn”>Checkout</button>
</section>

<section class=”checkout-form” id=”checkout-form”>
<h2>Checkout</h2>
<form id=”order-form”>
<label for=”name”>Name:</label>
<input type=”text” id=”name” required>

<label for=”email”>Email:</label>
<input type=”email” id=”email” required>

<label for=”phone”>Phone:</label>
<input type=”tel” id=”phone” required>

<button type=”submit”>Place Order</button>
</form>
</section>

</div>

<script>
document.addEventListener(“DOMContentLoaded”, function () {
const addButtons = document.querySelectorAll(“.add-btn”);
const orderList = document.getElementById(“order-list”);
const totalPriceElem = document.getElementById(“total-price”);
const checkoutBtn = document.getElementById(“checkout-btn”);
const checkoutForm = document.getElementById(“checkout-form”);
const searchBar = document.getElementById(“search-bar”);
let totalPrice = 0;

addButtons.forEach(button => {
button.addEventListener(“click”, function () {
const menuItem = this.parentElement;
const itemName = menuItem.querySelector(“span:first-child”).textContent;
const itemPrice = parseFloat(menuItem.dataset.price);
const itemQty = parseInt(menuItem.querySelector(“input[type=’number’]”).value);

// Add item to order list
const listItem = document.createElement(“li”);
listItem.classList.add(“order-item”);
listItem.innerHTML = `<span>${itemName} x ${itemQty} – $${(itemPrice * itemQty).toFixed(2)}</span>
<button class=”remove-btn”>Remove</button>`;
orderList.appendChild(listItem);

// Update total price
totalPrice += itemPrice * itemQty;
totalPriceElem.textContent = totalPrice.toFixed(2);

// Remove item event
listItem.querySelector(“.remove-btn”).addEventListener(“click”, function () {
orderList.removeChild(listItem);
totalPrice -= itemPrice * itemQty;
totalPriceElem.textContent = totalPrice.toFixed(2);
});
});
});

checkoutBtn.addEventListener(“click”, function () {
if (totalPrice > 0) {
checkoutForm.style.display = “block”;
} else {
alert(“Please add some items to your order.”);
}
});

const orderForm = document.getElementById(“order-form”);
orderForm.addEventListener(“submit”, function (e) {
e.preventDefault();
alert(“Thank you for your order!”);
// Reset form and order
orderList.innerHTML = “”;
totalPrice = 0;
totalPriceElem.textContent = “0”;
checkoutForm.style.display = “none”;
orderForm.reset();
});

// Search functionality
searchBar.addEventListener(“input”, function () {
const searchTerm = searchBar.value.toLowerCase();
const menuItems = document.querySelectorAll(“.menu-item”);

menuItems.forEach(item => {
const itemName = item.querySelector(“span:first-child”).textContent.toLowerCase();
if (itemName.includes(searchTerm)) {
item.style.display = “flex”;
} else {
item.style.display = “none”;
}
});
});
});
</script>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *

Resize text
Scroll to Top