Question: I got below PHP code here, can you modify it to only apply discount per category e . g 1 0 % to all the
I got below PHP code here, can you modify it to only apply discount per category eg to all the product under clothing.
The code seems to be applying additional discount per product. Eg this code line has additional which seems to be an additional discount ontop of the discount thats suposed to apply to all the clothing producsts
new DiscountedProductTShirt", $clothingCategory
name $name;
$thisdiscount $discount;
Product class to represent a product
class Product
public $name;
public $price;
public $quantity;
public function construct$name, $price, $quantity
$thisname $name;
$thisprice $price;
$thisquantity $quantity;
DiscountedProduct class inheriting from Product, with an additional category property
class DiscountedProduct extends Product
public $category;
public function construct$name, $price, $quantity, $category
parent::construct$name, $price, $quantity;
$thiscategory $category;
Function to calculate the final price after applying the category discount
function calculateDiscountedPrice$discountedProduct
$discountedPrice $discountedProductprice $discountedProductcategorydiscount;
return $discountedPrice;
Create category objects
$clothingCategory new CategoryClothing; discount
$electronicsCategory new CategoryElectronics; discount
$booksCategory new CategoryBooks; discount
Create product objects and categorize them
$products
"Clothing"
new DiscountedProductTShirt", $clothingCategory
new DiscountedProductJeans $clothingCategory
"Electronics"
new DiscountedProductSmartphone $electronicsCategory
new DiscountedProductLaptop $electronicsCategory
"Books"
new DiscountedProductPHP Guide", $booksCategory
new DiscountedProductJavaScript Guide", $booksCategory
;
Output product details
foreach $products as $categoryName $productArray
foreach $productArray as $product
$finalPrice calculateDiscountedPrice$product;
echo "Product: $productname
;
echo "Original Price: $ numberformat$productprice,
;
echo "Category: $productcategoryname
;
echo "Discount: $productcategorydiscount
;
echo "Final Price after Discount: $ numberformat$finalPrice,
;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
