Kshitij P2 Part2
Kshitij P2 Part2
shopping cart. Use arithmetic operators to calculate the total price of items added to
the cart. Implement logical operators to apply discounts or promotions based on certain
conditions. Present the results of arithmetic and logical operations on the user interface.
Enable user interaction through input forms, buttons, or other interactive elements.
· Condition 1: Applying a discount to a total when the quantity is 5 or more.
· Condition 2: Free shipping is provided if the total is $50 or more; otherwise, there's a
flat shipping fee of $5.
· Condition 3: An additional discount is applied for premium users (in this case, 5% off
the discounted total).
Code-
ShoppingCart.js:-
import React, { useState } from 'react';
import './Cart.css'
if (isPremiumUser) {
discount += 0.05;
}
return (
<div className='Parent'>
<h1>Virtual Shopping Cart</h1>
<div>
</div>
<div className='Buttons'>
<button onClick={() => addItem('airbuds', 5)}>Airbuds
($5)</button>
</div>
<div>
<div className="cart">
<label for="labl">Premium User</label>
<input
id='labl'
type="checkbox"
checked={isPremiumUser}
onChange={handlePremiumChange}
/>
<h2>Items in Cart</h2>
<ul>
{items.map((item, index) => (
<li key={index}>{item.name} - $
{item.price.toFixed(1)}</li>
))}
</ul>
</div>
<p>Subtotal: ${total}</p>
<p>Shipping: ${shipping}</p>
<p>Total: ${parseFloat(total)+parseInt(shipping)}</p>
</div>
</div>
);
};
function App() {
return (
<div className="App">
<ShoppingCart />
</div>
</div>
</div>
);
}