index
index
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Grocery Management Website</title>
<style>
/* General Styles */
body {
font-family: 'Arial', sans-serif;
margin: 0;
padding: 0;
background-color: #1e272e; /* Carbonistic dark background */
color: #f1f2f6; /* Light text for contrast */
}
.container {
width: 80%;
margin: auto;
overflow: hidden;
padding: 20px 0;
}
/* Navigation Bar */
nav {
background-color: #2d3436; /* Darker nav background */
color: #fff;
padding: 10px 0;
position: sticky;
top: 0;
}
nav ul {
padding: 0;
list-style: none;
text-align: center;
}
nav li {
display: inline;
margin: 0 20px;
}
nav a {
color: #fff;
text-decoration: none;
font-weight: bold;
transition: color 0.3s ease;
}
nav a:hover {
color: #a4b0af; /* Hover color */
}
/* Form Styles */
form {
background: #2d3436; /* Form background */
padding: 20px;
border-radius: 8px;
margin-bottom: 20px;
}
form label {
display: block;
margin-top: 10px;
color: #a4b0af;
}
form input[type="text"],
form input[type="email"],
form input[type="password"],
form input[type="number"],
form textarea {
width: 100%;
padding: 8px;
margin-top: 5px;
margin-bottom: 10px;
border: 1px solid #485460; /* Input border */
background-color: #353b48; /* Input background */
color: #f1f2f6;
border-radius: 4px;
box-sizing: border-box; /* Ensures padding is included in width */
}
form button {
background-color: #485460; /* Button background */
color: #fff;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
form button:hover {
background-color: #353b48; /* Button hover background */
}
.product {
width: 200px;
margin: 20px;
padding: 15px;
background: #2d3436; /* Product card background */
border-radius: 8px;
text-align: center;
}
.product button {
background-color: #485460; /* Add to cart button */
color: #fff;
padding: 8px 12px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
margin-top: 10px;
}
.product button:hover {
background-color: #353b48; /* Button hover background */
}
/* Cart Styles */
#cart {
margin-top: 20px;
background: #2d3436; /* Cart background */
padding: 20px;
border-radius: 8px;
}
#cart ul {
list-style: none;
padding: 0;
}
#cart li {
padding: 10px 0;
border-bottom: 1px solid #485460;
}
#cart li:last-child {
border-bottom: none;
}
#cart button {
background-color: #c23616; /* Delete button */
color: #fff;
padding: 5px 8px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
#cart button:hover {
background-color: #e55039; /* Delete button hover */
}
/* Footer Style */
footer {
text-align: center;
padding: 20px;
background-color: #2d3436; /* Footer background */
color: #a4b0af;
}
/* Utility Classes */
.hidden {
display: none;
}
.error {
color: #c23616;
margin-top: 5px;
}
</style>
</head>
<body>
<nav>
<div class="container">
<ul>
<li id="navHome"><a href="#" onclick="loadHomePage(); return
false;">Home</a></li>
<li id="navLogin"><a href="#" onclick="showLoginForm(); return
false;">Login</a></li>
<li id="navRegister"><a href="#" onclick="showRegistrationForm();
return false;">Register</a></li>
<li id="navProfile" class="hidden"><a href="#"
onclick="loadProfilePage(); return false;">My Profile</a></li>
<li id="navCart"><a href="#" onclick="loadCartPage(); return
false;">Cart</a></li>
<li id="navLogout" class="hidden"><a href="#" onclick="logout();
return false;">Logout</a></li>
</ul>
</div>
</nav>
<div class="container">
<!-- Registration Form -->
<section id="registrationPage">
<h2>Register</h2>
<form id="registrationForm" onsubmit="return validateRegistration()">
<label for="name">Name:</label>
<input type="text" id="name" name="name" maxlength="50">
<div class="error" id="nameError"></div>
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<div class="error" id="emailError"></div>
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<div class="error" id="passwordError"></div>
<label for="address">Address:</label>
<textarea id="address" name="address" maxlength="100"></textarea>
<div class="error" id="addressError"></div>
<button type="submit">Register</button>
</form>
</section>
<!-- Login Form -->
<section id="loginPage">
<h2>Login</h2>
<form id="loginForm" onsubmit="return validateLogin()">
<label for="customerID">Customer ID:</label>
<input type="text" id="customerID" name="customerID">
<div class="error" id="customerIDError"></div>
<label for="loginPassword">Password:</label>
<input type="password" id="loginPassword" name="loginPassword">
<div class="error" id="loginPasswordError"></div>
<button type="submit">Login</button>
<p>
<a href="#" onclick="showRegistrationForm(); return
false;">Register Yourself</a>
</p>
</form>
</section>
<h3>Products</h3>
<div class="products" id="productsList">
<!-- Product items will be dynamically added here -->
</div>
<h3>Cart</h3>
<div id="cart">
<ul id="cartItems">
<!-- Cart items will be dynamically added here -->
</ul>
<p><strong>Summary: $<span id="cartTotal">0.00</span></strong></p>
<button onclick="checkout()">Checkout</button>
</div>
</section>
<footer>
<p>© 2024 Grocery Management System</p>
</footer>
<script>
// --- State Management ---
let isLoggedIn = false;
let currentUser = null;
let cart = [];
let products = [
{ id: 1, name: "Apples", price: 2.00 },
{ id: 2, name: "Bananas", price: 1.50 },
{ id: 3, name: "Milk", price: 3.50 },
{ id: 4, name: "Bread", price: 2.50 }
];
function validateRegistration() {
let isValid = true;
resetErrors();
if (isValid) {
registerUser();
return false; // Prevent form submission
} else {
return false; // Prevent form submission
}
}
function registerUser() {
// Basic user object creation for demonstration
currentUser = {
name: document.getElementById('name').value,
email: document.getElementById('email').value,
password: document.getElementById('password').value,
address: document.getElementById('address').value,
contact: document.getElementById('contact').value,
customerID: 'CUST' + Date.now() // Generate a unique Customer ID
};
alert('Registration Successful! Your Customer ID is: ' +
currentUser.customerID);
showLoginForm(); // Redirect to login after successful registration
}
function resetErrors() {
let errors = document.querySelectorAll('.error');
errors.forEach(error => error.innerText = '');
}
function validateLogin() {
let customerID = document.getElementById('customerID').value;
let password = document.getElementById('loginPassword').value;
if (!customerID) {
document.getElementById('customerIDError').innerText = 'Customer ID
is required';
return false;
}
if (!password) {
document.getElementById('loginPasswordError').innerText = 'Password
is required';
return false;
}
function updateNav() {
document.getElementById('navLogin').classList.toggle('hidden',
isLoggedIn);
document.getElementById('navRegister').classList.toggle('hidden',
isLoggedIn);
document.getElementById('navProfile').classList.toggle('hidden', !
isLoggedIn);
document.getElementById('navLogout').classList.toggle('hidden', !
isLoggedIn);
}
updateNav();
showSection('homePage');
document.getElementById('homeHeader').innerText = `Hello $
{currentUser.name} to Online Grocery Store`;
displayProducts();
}
function displayProducts() {
let productsList = document.getElementById('productsList');
productsList.innerHTML = ''; // Clear existing products
products.forEach(product => {
let productDiv = document.createElement('div');
productDiv.className = 'product';
productDiv.innerHTML = `
<h3>${product.name}</h3>
<p>Price: $${product.price.toFixed(2)}</p>
<button onclick="addToCart(${product.id})">Add to Cart</button>
`;
productsList.appendChild(productDiv);
});
}
function updateCartDisplay() {
let cartItemsList = document.getElementById('cartItems');
let cartTotalDisplay = document.getElementById('cartTotal');
let total = 0;
cart.forEach(item => {
let listItem = document.createElement('li');
listItem.innerHTML = `
${item.name} - $${item.price.toFixed(2)}
<button onclick="deleteFromCart(${item.id})">Delete</button>
`;
cartItemsList.appendChild(listItem);
total += item.price;
});
cartTotalDisplay.innerText = total.toFixed(2);
}
function deleteFromCart(productId) {
cart = cart.filter(item => item.id !== productId);
updateCartDisplay();
}
function loadCartPage() {
if (!isLoggedIn) {
showLoginForm();
return;
}
updateNav();
showSection('homePage'); // Ensure home page is displayed
updateCartDisplay(); // Refresh cart display
// Optionally scroll to the cart section
document.getElementById('cart').scrollIntoView({ behavior: 'smooth' });
}
function checkout() {
cart = []; // Clear the cart
updateCartDisplay();
showSection('orderConfirmation');
}
showSection('profilePage');
let profileDetailsDiv = document.getElementById('profileDetails');
profileDetailsDiv.innerHTML = `
`;
loadProfileDetails();
}
function loadProfileDetails(){
document.getElementById('profileDetails').innerHTML = `
<p><strong>Name:</strong> ${currentUser.name}</p>
<p><strong>Email:</strong> ${currentUser.email}</p>
<p><strong>Address:</strong> ${currentUser.address}</p>
<p><strong>Contact:</strong> ${currentUser.contact}</p>
<p><strong>Customer ID:</strong> ${currentUser.customerID}</p>
`;
}
function validateContact(){
const updatedContact = document.getElementById('updateContact').value;
if (updatedContact.length < 10 ) {
document.getElementById('updateContactError').innerText = 'Contact
must be 10 digits';
} else if(updatedContact.length > 10){
document.getElementById('updateContactError').innerText = 'Contact
must be 10 digits';
} else {
document.getElementById('updateContactError').innerText = '';
}
}
document.getElementById('updateProfileForm').classList.remove('hidden');
let isValid=true;
if(!updatedName) {
document.getElementById('updateNameError').innerText = 'Name is
required';
isValid = false;
} else if (!/^[a-zA-Z\s]+$/.test(updatedName)) {
document.getElementById('updateNameError').innerText = 'Name must
contain only alphabets';
isValid = false;
}
if(!updatedEmail) {
document.getElementById('updateEmailError').innerText = 'Email is
required';
isValid = false;
} else if (!/@/.test(updatedEmail)) {
document.getElementById('updateEmailError').innerText = 'Email must
contain @';
isValid = false;
}
if(!updatedAddress) {
document.getElementById('updateAddressError').innerText = 'Address
is required';
isValid = false;
}
if(!updatedContact) {
document.getElementById('updateContactError').innerText = 'Contact
is required';
isValid = false;
}else if (updatedContact.length < 10) {
document.getElementById('updateContactError').innerText = 'Contact
must be 10 digits';
isValid = false;
}else if (updatedContact.length > 10) {
isValid=false;
}
if(isValid){
currentUser.name = updatedName;
currentUser.email = updatedEmail;
currentUser.address= updatedAddress;
currentUser.contact= updatedContact;
// Hide the update form and show the updated profile details
document.getElementById('profileDetails').classList.remove('hidden');
document.getElementById('updateProfileForm').classList.add('hidden');
loadProfilePage();
}else{
return false;
}
</body>
</html>