<!
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 Listing Styles */
.products {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
.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>
<label for="contact">Contact Number:</label>
<input type="number" id="contact" name="contact">
<div class="error" id="contactError"></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>
<!-- Home Page -->
<section id="homePage" class="hidden">
<h2 id="homeHeader">Hello to Online Grocery Store</h2>
<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>
<!-- Profile Page -->
<section id="profilePage" class="hidden">
<h2>My Profile</h2>
<div id="profileDetails">
<!-- Profile details will be dynamically added here -->
</div>
<!-- Update Profile Form (hidden by default) -->
<form id="updateProfileForm" class="hidden">
<label for="updateName">Name:</label>
<input type="text" id="updateName" name="updateName">
<div class="error" id="updateNameError"></div>
<label for="updateEmail">Email:</label>
<input type="email" id="updateEmail" name="updateEmail">
<div class="error" id="updateEmailError"></div>
<label for="updateAddress">Address:</label>
<textarea id="updateAddress" name="updateAddress"></textarea>
<div class="error" id="updateAddressError"></div>
<label for="updateContact">Contact Number:</label>
<input type="number" id="updateContact" name="updateContact"
oninput="validateContact()">
<div class="error" id="updateContactError"></div>
<button type="submit" onclick="saveProfileChanges(); return
false;">Save Changes</button>
</form>
<button onclick="updateProfile()">Update</button>
</section>
<!-- Order Confirmation Message -->
<section id="orderConfirmation" class="hidden">
<h2>Order Placed Successfully!</h2>
</section>
</div>
<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 }
];
// --- Utility Functions ---
function showSection(sectionId) {
const sections = ['registrationPage', 'loginPage', 'homePage',
'profilePage', 'orderConfirmation'];
[Link](id => {
[Link](id).[Link]('hidden');
});
[Link](sectionId).[Link]('hidden');
}
// --- Registration Functions ---
function showRegistrationForm() {
showSection('registrationPage');
}
function validateRegistration() {
let isValid = true;
resetErrors();
// --- Name Validation ---
let name = [Link]('name').value;
if (!name) {
[Link]('nameError').innerText = 'Name is
required';
isValid = false;
} else if (!/^[a-zA-Z\s]+$/.test(name)) {
[Link]('nameError').innerText = 'Name must contain
only alphabets';
isValid = false;
}
// --- Email Validation ---
let email = [Link]('email').value;
if (!email) {
[Link]('emailError').innerText = 'Email is
required';
isValid = false;
} else if (!/@/.test(email)) {
[Link]('emailError').innerText = 'Email must
contain @';
isValid = false;
}
// --- Password Validation ---
let password = [Link]('password').value;
if (!password) {
[Link]('passwordError').innerText = 'Password is
required';
isValid = false;
} else if ([Link] < 5 || !/[A-Z]/.test(password) || !/[0-
9]/.test(password) || !/[^a-zA-Z0-9]/.test(password)) {
[Link]('passwordError').innerText = 'Password must
be at least 5 characters long and contain 1 uppercase letter, 1 number, and 1
special character';
isValid = false;
}
// --- Address Validation ---
let address = [Link]('address').value;
if (!address) {
[Link]('addressError').innerText = 'Address is
required';
isValid = false;
}
// --- Contact Validation ---
let contact = [Link]('contact').value;
if (!contact) {
[Link]('contactError').innerText = 'Contact is
required';
isValid = false;
} else if (!/^\d{10}$/.test(contact)) {
[Link]('contactError').innerText = 'Contact must
be 10 digits';
isValid = false;
}
if (isValid) {
registerUser();
return false; // Prevent form submission
} else {
return false; // Prevent form submission
}
}
function registerUser() {
// Basic user object creation for demonstration
currentUser = {
name: [Link]('name').value,
email: [Link]('email').value,
password: [Link]('password').value,
address: [Link]('address').value,
contact: [Link]('contact').value,
customerID: 'CUST' + [Link]() // Generate a unique Customer ID
};
alert('Registration Successful! Your Customer ID is: ' +
[Link]);
showLoginForm(); // Redirect to login after successful registration
}
function resetErrors() {
let errors = [Link]('.error');
[Link](error => [Link] = '');
}
// --- Login Functions ---
function showLoginForm() {
showSection('loginPage');
}
function validateLogin() {
let customerID = [Link]('customerID').value;
let password = [Link]('loginPassword').value;
if (!customerID) {
[Link]('customerIDError').innerText = 'Customer ID
is required';
return false;
}
if (!password) {
[Link]('loginPasswordError').innerText = 'Password
is required';
return false;
}
// --- Authentication Logic ---
if (currentUser && [Link] === customerID &&
[Link] === password) {
isLoggedIn = true;
loadHomePage();
return false;
} else {
alert('Invalid Login');
return false;
}
}
function updateNav() {
[Link]('navLogin').[Link]('hidden',
isLoggedIn);
[Link]('navRegister').[Link]('hidden',
isLoggedIn);
[Link]('navProfile').[Link]('hidden', !
isLoggedIn);
[Link]('navLogout').[Link]('hidden', !
isLoggedIn);
}
// --- Home Page Functions ---
function loadHomePage() {
if (!isLoggedIn) {
showLoginForm();
return;
}
updateNav();
showSection('homePage');
[Link]('homeHeader').innerText = `Hello $
{[Link]} to Online Grocery Store`;
displayProducts();
}
function displayProducts() {
let productsList = [Link]('productsList');
[Link] = ''; // Clear existing products
[Link](product => {
let productDiv = [Link]('div');
[Link] = 'product';
[Link] = `
<h3>${[Link]}</h3>
<p>Price: $${[Link](2)}</p>
<button onclick="addToCart(${[Link]})">Add to Cart</button>
`;
[Link](productDiv);
});
}
// --- Cart Functions ---
function addToCart(productId) {
let product = [Link](p => [Link] === productId);
if (product) {
[Link](product);
updateCartDisplay();
}
}
function updateCartDisplay() {
let cartItemsList = [Link]('cartItems');
let cartTotalDisplay = [Link]('cartTotal');
[Link] = ''; // Clear existing items
let total = 0;
[Link](item => {
let listItem = [Link]('li');
[Link] = `
${[Link]} - $${[Link](2)}
<button onclick="deleteFromCart(${[Link]})">Delete</button>
`;
[Link](listItem);
total += [Link];
});
[Link] = [Link](2);
}
function deleteFromCart(productId) {
cart = [Link](item => [Link] !== 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
[Link]('cart').scrollIntoView({ behavior: 'smooth' });
}
function checkout() {
cart = []; // Clear the cart
updateCartDisplay();
showSection('orderConfirmation');
}
// --- Profile Functions ---
function loadProfilePage() {
if (!isLoggedIn) {
showLoginForm();
return;
}
updateNav();
showSection('profilePage');
let profileDetailsDiv = [Link]('profileDetails');
[Link] = `
`;
loadProfileDetails();
}
function loadProfileDetails(){
[Link]('profileDetails').innerHTML = `
<p><strong>Name:</strong> ${[Link]}</p>
<p><strong>Email:</strong> ${[Link]}</p>
<p><strong>Address:</strong> ${[Link]}</p>
<p><strong>Contact:</strong> ${[Link]}</p>
<p><strong>Customer ID:</strong> ${[Link]}</p>
`;
}
function validateContact(){
const updatedContact = [Link]('updateContact').value;
if ([Link] < 10 ) {
[Link]('updateContactError').innerText = 'Contact
must be 10 digits';
} else if([Link] > 10){
[Link]('updateContactError').innerText = 'Contact
must be 10 digits';
} else {
[Link]('updateContactError').innerText = '';
}
}
// Function to show the update profile form
function updateProfile() {
// Hide profile details and show the update form
[Link]('profileDetails').[Link]('hidden');
[Link]('updateProfileForm').[Link]('hidden');
// Populate the form fields with current user data
[Link]('updateName').value = [Link];
[Link]('updateEmail').value = [Link];
[Link]('updateAddress').value = [Link];
[Link]('updateContact').value = [Link];
resetErrors();
}
// Function to save the profile changes
function saveProfileChanges() {
// Get the updated values from the form
const updatedName = [Link]('updateName').value;
const updatedEmail = [Link]('updateEmail').value;
const updatedAddress = [Link]('updateAddress').value;
const updatedContact = [Link]('updateContact').value;
let isValid=true;
if(!updatedName) {
[Link]('updateNameError').innerText = 'Name is
required';
isValid = false;
} else if (!/^[a-zA-Z\s]+$/.test(updatedName)) {
[Link]('updateNameError').innerText = 'Name must
contain only alphabets';
isValid = false;
}
if(!updatedEmail) {
[Link]('updateEmailError').innerText = 'Email is
required';
isValid = false;
} else if (!/@/.test(updatedEmail)) {
[Link]('updateEmailError').innerText = 'Email must
contain @';
isValid = false;
}
if(!updatedAddress) {
[Link]('updateAddressError').innerText = 'Address
is required';
isValid = false;
}
if(!updatedContact) {
[Link]('updateContactError').innerText = 'Contact
is required';
isValid = false;
}else if ([Link] < 10) {
[Link]('updateContactError').innerText = 'Contact
must be 10 digits';
isValid = false;
}else if ([Link] > 10) {
isValid=false;
}
if(isValid){
[Link] = updatedName;
[Link] = updatedEmail;
[Link]= updatedAddress;
[Link]= updatedContact;
// Hide the update form and show the updated profile details
[Link]('profileDetails').[Link]('hidden');
[Link]('updateProfileForm').[Link]('hidden');
loadProfilePage();
}else{
return false;
}
// --- Logout Function ---
function logout() {
isLoggedIn = false;
currentUser = null;
showLoginForm();
updateNav();
}
// --- Initialization ---
[Link]('DOMContentLoaded', function () {
showLoginForm(); // Show login form on page load
});
</script>
</body>
</html>