Question: i got below code from here and its working fine in PHP and i am getting the expected results. However when i open the web

i got below code from here and its working fine in PHP and i am getting the expected results. However when i open the web browser in ampps, the estatics is not looking good
name = $name;
$this->discount = $discount;
}
}
// Product class to represent a product
class Product {
public $name;
public $price;
public $quantity;
public function __construct($name, $price, $quantity){
$this->name = $name;
$this->price = $price;
$this->quantity = $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);
$this->category = $category;
}
}
// Function to calculate the final price after applying the category discount
function calculateDiscountedPrice($discountedProduct){
$discountedPrice = $discountedProduct->price *(1- $discountedProduct->category->discount);
return $discountedPrice;
}
// Create category objects
$clothingCategory = new Category("Clothing",0.10); //10% discount
$electronicsCategory = new Category("Electronics",0.20); //20% discount
$booksCategory = new Category("Books",0.05); //5% discount
// Create product objects and categorize them
$products =[
"Clothing" =>[
new DiscountedProduct("T-Shirt", 20.00,50, $clothingCategory),
new DiscountedProduct("Jeans",40.00,30, $clothingCategory)
],
"Electronics" =>[
new DiscountedProduct("Smartphone",300.00,20, $electronicsCategory),
new DiscountedProduct("Laptop",1000.00,10, $electronicsCategory)
],
"Books" =>[
new DiscountedProduct("PHP Guide", 15.00,100, $booksCategory),
new DiscountedProduct("JavaScript Guide", 25.00,60, $booksCategory)
]
];
// Output product details
foreach ($products as $categoryName => $productArray){
foreach ($productArray as $product){
$finalPrice = calculateDiscountedPrice($product);
echo "Product: ". $product->name ."
";
echo "Original Price: $". number_format($product->price, 2)."
";
echo "Category: ". $product->category->name ."
";
echo "Discount: ".($product->category->discount *100)."%
";
echo "Final Price after Discount: $". number_format($finalPrice, 2)."
";
}
}
?
i get below view with ampps brower. Can you help me improve it so it can be spaced well "Iterate through the $products array and for each
product"
i got below code from here and its working fine

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!