| 1 | // A disorganized shopping cart implementation |
| 2 | // Difficulty: 3 - Moderate refactoring needed |
| 3 | |
| 4 | // Product catalog |
| 5 | const PRODUCTS = { |
| 6 | 1: { |
| 7 | id: 1, |
| 8 | name: "Laptop", |
| 9 | price: 999.99, |
| 10 | stock: 10, |
| 11 | category: "electronics", |
| 12 | }, |
| 13 | 2: { |
| 14 | id: 2, |
| 15 | name: "Smartphone", |
| 16 | price: 699.99, |
| 17 | stock: 15, |
| 18 | category: "electronics", |
| 19 | }, |
| 20 | 3: { |
| 21 | id: 3, |
| 22 | name: "Headphones", |
| 23 | price: 149.99, |
| 24 | stock: 20, |
| 25 | category: "electronics", |
| 26 | }, |
| 27 | 4: { id: 4, name: "Desk", price: 249.99, stock: 5, category: "furniture" }, |
| 28 | 5: { id: 5, name: "Chair", price: 179.99, stock: 8, category: "furniture" }, |
| 29 | }; |
| 30 | |
| 31 | // Discount codes |
| 32 | const DISCOUNT_CODES = { |
| 33 | SAVE10: { percentage: 10, minPurchase: 0 }, |
| 34 | SAVE20: { percentage: 20, minPurchase: 500 }, |
| 35 | FURNITURE15: { percentage: 15, minPurchase: 0, category: "furniture" }, |
| 36 | }; |
| 37 | |
| 38 | // Cart state |
| 39 | let cartItems: { productId: number; quantity: number; price: number }[] = []; |
| 40 | let cartTotal = 0; |
| 41 | let appliedDiscountCode: string | null = null; |
| 42 | |
| 43 | // User info |
| 44 | let userInfo: { |
| 45 | name: string; |
| 46 | email: string; |
| 47 | address?: { |
| 48 | street: string; |
| 49 | city: string; |
| 50 | zipCode: string; |
| 51 | country: string; |
| 52 | }; |
| 53 | } | null = null; |
| 54 | |
| 55 | // Add product to cart |
| 56 | function addToCart(productId: number, quantity: number = 1) { |
| 57 | // Check if product exists |
| 58 | if (!PRODUCTS[productId]) { |
| 59 | console.error("Product not found!"); |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | // Check if enough stock |
| 64 | if (PRODUCTS[productId].stock < quantity) { |
| 65 | console.error("Not enough stock!"); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | // Check if product already in cart |
| 70 | let existingItem = false; |
| 71 | for (let i = 0; i < cartItems.length; i++) { |
| 72 | if (cartItems[i].productId === productId) { |
| 73 | // Update quantity |
| 74 | cartItems[i].quantity += quantity; |
| 75 | existingItem = true; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Add new item if not already in cart |
| 81 | if (!existingItem) { |
| 82 | cartItems.push({ |
| 83 | productId, |
| 84 | quantity, |
| 85 | price: PRODUCTS[productId].price, |
| 86 | }); |
| 87 | } |
| 88 | |
| 89 | // Update stock |
| 90 | PRODUCTS[productId].stock -= quantity; |
| 91 | |
| 92 | // Recalculate cart total |
| 93 | calculateCartTotal(); |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | // Remove product from cart |
| 99 | function removeFromCart(productId: number, quantity: number = 1) { |
| 100 | for (let i = 0; i < cartItems.length; i++) { |
| 101 | if (cartItems[i].productId === productId) { |
| 102 | // Determine quantity to remove |
| 103 | const qtyToRemove = Math.min(cartItems[i].quantity, quantity); |
| 104 | |
| 105 | // Return stock |
| 106 | PRODUCTS[productId].stock += qtyToRemove; |
| 107 | |
| 108 | // Update cart item |
| 109 | cartItems[i].quantity -= qtyToRemove; |
| 110 | |
| 111 | // Remove item if quantity is 0 |
| 112 | if (cartItems[i].quantity === 0) { |
| 113 | cartItems.splice(i, 1); |
| 114 | } |
| 115 | |
| 116 | // Recalculate cart total |
| 117 | calculateCartTotal(); |
| 118 | |
| 119 | return true; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | console.error("Product not in cart!"); |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | // Calculate cart total |
| 128 | function calculateCartTotal() { |
| 129 | let total = 0; |
| 130 | |
| 131 | // Sum all items |
| 132 | for (let i = 0; i < cartItems.length; i++) { |
| 133 | const item = cartItems[i]; |
| 134 | total += item.price * item.quantity; |
| 135 | } |
| 136 | |
| 137 | // Apply discount if any |
| 138 | if (appliedDiscountCode) { |
| 139 | const discount = DISCOUNT_CODES[appliedDiscountCode]; |
| 140 | |
| 141 | // Check if discount applies to specific category |
| 142 | if (discount.category) { |
| 143 | let categoryTotal = 0; |
| 144 | for (let i = 0; i < cartItems.length; i++) { |
| 145 | const item = cartItems[i]; |
| 146 | if (PRODUCTS[item.productId].category === discount.category) { |
| 147 | categoryTotal += item.price * item.quantity; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // Apply discount to category total |
| 152 | const discountAmount = categoryTotal * (discount.percentage / 100); |
| 153 | total -= discountAmount; |
| 154 | } else { |
| 155 | // Apply discount to entire cart |
| 156 | const discountAmount = total * (discount.percentage / 100); |
| 157 | total -= discountAmount; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | cartTotal = total; |
| 162 | return cartTotal; |
| 163 | } |
| 164 | |
| 165 | // Apply discount code |
| 166 | function applyDiscountCode(code: string) { |
| 167 | // Check if discount code exists |
| 168 | if (!DISCOUNT_CODES[code]) { |
| 169 | console.error("Invalid discount code!"); |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | const discount = DISCOUNT_CODES[code]; |
| 174 | |
| 175 | // Check minimum purchase requirement |
| 176 | if (cartTotal < discount.minPurchase) { |
| 177 | console.error( |
| 178 | `Minimum purchase of $${discount.minPurchase} required for this code!`, |
| 179 | ); |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | // Apply discount code |
| 184 | appliedDiscountCode = code; |
| 185 | |
| 186 | // Recalculate total |
| 187 | calculateCartTotal(); |
| 188 | |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | // Set user information |
| 193 | function setUserInfo( |
| 194 | name: string, |
| 195 | email: string, |
| 196 | address?: { street: string; city: string; zipCode: string; country: string }, |
| 197 | ) { |
| 198 | userInfo = { name, email, address }; |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | // Checkout |
| 203 | function checkout() { |
| 204 | // Validate cart |
| 205 | if (cartItems.length === 0) { |
| 206 | console.error("Cart is empty!"); |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | // Validate user info |
| 211 | if (!userInfo || !userInfo.name || !userInfo.email) { |
| 212 | console.error("User information incomplete!"); |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | // Process payment (simplified) |
| 217 | console.log("Processing payment for $" + cartTotal); |
| 218 | console.log("Items:", cartItems); |
| 219 | console.log("User:", userInfo); |
| 220 | |
| 221 | // Clear cart after successful checkout |
| 222 | cartItems = []; |
| 223 | cartTotal = 0; |
| 224 | appliedDiscountCode = null; |
| 225 | |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | // Get cart summary |
| 230 | function getCartSummary() { |
| 231 | return { |
| 232 | items: cartItems, |
| 233 | total: cartTotal, |
| 234 | discountCode: appliedDiscountCode, |
| 235 | user: userInfo, |
| 236 | }; |
| 237 | } |
| 238 | |
| 239 | // Example usage |
| 240 | addToCart(1, 2); // Add 2 laptops |
| 241 | addToCart(3, 1); // Add 1 headphones |
| 242 | applyDiscountCode("SAVE10"); |
| 243 | setUserInfo("John Doe", "john@example.com", { |
| 244 | street: "123 Main St", |
| 245 | city: "Anytown", |
| 246 | zipCode: "12345", |
| 247 | country: "USA", |
| 248 | }); |
| 249 | |
| 250 | console.log(getCartSummary()); |
| 251 | |
| 252 | export { |
| 253 | addToCart, |
| 254 | applyDiscountCode, |
| 255 | calculateCartTotal, |
| 256 | checkout, |
| 257 | DISCOUNT_CODES, |
| 258 | getCartSummary, |
| 259 | PRODUCTS, |
| 260 | removeFromCart, |
| 261 | setUserInfo, |
| 262 | }; |