// Sample book data (in a real app, fetch from a server)
const books = {
fiction: [
{ id: 1, title: "The Great Gatsby", author: "F. Scott Fitzgerald", price: 10.99, image: "https://via.placeholder.com/200x300?text=Gatsby" },
{ id: 2, title: "To Kill a Mockingbird", author: "Harper Lee", price: 12.99, image: "https://via.placeholder.com/200x300?text=Mockingbird" }
],
nonfiction: [
{ id: 3, title: "Sapiens", author: "Yuval Noah Harari", price: 15.99, image: "https://via.placeholder.com/200x300?text=Sapiens" },
{ id: 4, title: "Educated", author: "Tara Westover", price: 14.99, image: "https://via.placeholder.com/200x300?text=Educated" }
],
scifi: [
{ id: 5, title: "Dune", author: "Frank Herbert", price: 13.99, image: "https://via.placeholder.com/200x300?text=Dune" },
{ id: 6, title: "Neuromancer", author: "William Gibson", price: 11.99, image: "https://via.placeholder.com/200x300?text=Neuromancer" }
]
};
let cart = [];
let cartCount = 0;
let cartTotal = 0;
// Function to display books in a category
function showCategory(category) {
const content = document.getElementById('content');
content.innerHTML = '';
books[category].forEach(book => {
const bookDiv = document.createElement('div');
bookDiv.className = 'book';
bookDiv.innerHTML = `
by ${book.author}
$${book.price}
`; content.appendChild(bookDiv); }); } // Function to add book to cart function addToCart(id, title, price) { cart.push({ id, title, price }); cartCount++; cartTotal += price; document.getElementById('cart-count').textContent = cartCount; alert(`${title} added to cart!`); } // Function to show cart modal function showCart() { const cartItems = document.getElementById('cart-items'); cartItems.innerHTML = ''; cart.forEach(item => { const li = document.createElement('li'); li.textContent = `${item.title} - $${item.price}`; cartItems.appendChild(li); }); document.getElementById('cart-total').textContent = cartTotal.toFixed(2); document.getElementById('cart-modal').style.display = 'block'; } // Function to close cart modal function closeCart() { document.getElementById('cart-modal').style.display = 'none'; } // Function to simulate placing an order function placeOrder() { if (cart.length === 0) { alert('Your cart is empty!'); return; } alert(`Order placed! Total: $${cartTotal.toFixed(2)}. (This is a simulation.)`); cart = []; cartCount = 0; cartTotal = 0; document.getElementById('cart-count').textContent = cartCount; closeCart(); } // Load fiction by default showCategory('fiction');