@ 2015-12-03T14:00:27Z <?php
interface RechnungInterface {
/**
* @param \Discount $discount
* @return void
*/
public function applyDiscount(\Discount $discount);
/**
* @param \Product $product
* @return void
*/
public function addProduct(\Product $product);
/**
* @return float
*/
public function getPrice();
}
interface DiscountInterface {
/**
* @param string $name
* @param mixed $value
*/
public function addCondition($name, $value);
/**
* @param float $precentage
*/
public function setDiscount($precentage);
/**
* @param \Product $product
* @return float The new price which should be applied within the \Rechnung
*/
public function getPrice(\Product $product);
}
class Dsicount implements DiscountInterface {
/**
* A list of \Condition
* @var array[\Condition]
*/
private $conditions = [];
/**
* The discount given on a product which matches the conditions
* @var float
*/
private $discount;
/**
* @var string
*/
public $name = "";
/**
* {@inheritDoc}
*/
public function getPrice(\Product $product) {
$disc = false;
foreach($this->conditions as $cond) {
$disc = ($cond[1] == $product->{$cond[0]});
if(!$disc) {
return $product->price;
}
}
return $product->price * (1 + $this->discount);
}
/**
* {@inheritDoc}
*/
public function addCondition($name, $value) {
$arr = [$name, $value];
$this->conditions[] = $arr;
}
/**
* {@inheritDoc}
*/
public function setDiscount($discount) {
$this->discount = $discount;
}
public function __construct($name) {
$this->name = $name;
}
}
class Product {
/**
* @var float
*/
public $price = 0;
/**
* @var string
*/
public $name = "";
/**
* @var integer
*/
public $count = 1;
/**
* @param string
* @param float
*/
public function __construct($name, $price) {
$this->name = $name;
$this->price = $price;
}
}
class Rechnung implements RechungInterface {
/**
* A list of applied \Discount
* @var array[\Discount]
*/
private $discounts = [];
/**
* A list of \Product on this sheet
* @var array[\Product]
*/
private $products = [];
/**
* @param string $name
*/
public function __construct($name) {
$this->subject = $name;
}
/**
* {@inheritDoc}
*/
public function addProduct(\Product $product) {
if(!isset($this->products[$product->name])) {
$this->products[$product->name] = $product;
}
}
/**
* {@inheritDoc}
*/
public function applyDiscount(\Discount $discount) {
if(!isset($this->discounts[$discount->name])) {
$this->discounts[$discount->name] = $discount;
}
}
/**
* {@inheritDoc}
*/
public function getPrice() {
$total = 0;
foreach($this->products as $product) {
foreach($this->discounts as $discount) {
$product->price = $discount->getPrice($product);
}
$total += $product->price;
}
return $total;
}
}
$rechnung = new Rechnung("AJ");
$produkt = new Product("Auto", 800);
$rechnung->addProduct($produkt);
var_dump($rechnung);
$rabatt = new Discount("name", "Auto");
$rechnung->applyDiscount($rabatt);
echo $rechnung->getPrice();
var_dump($rechnung);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Fatal error: Uncaught Error: Interface "RechungInterface" not found in /in/XakKO:122
Stack trace:
#0 {main}
thrown in /in/XakKO on line 122
Process exited with code 255 . Output for 7.4.0 - 7.4.33 Fatal error: Uncaught Error: Interface 'RechungInterface' not found in /in/XakKO:122
Stack trace:
#0 {main}
thrown in /in/XakKO on line 122
Process exited with code 255 . Output for 5.5.24 - 5.5.35 , 5.6.8 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.10 , 7.2.0 - 7.2.33 , 7.3.16 - 7.3.33 Fatal error: Interface 'RechungInterface' not found in /in/XakKO on line 122
Process exited with code 255 . preferences:dark mode live preview
170.67 ms | 403 KiB | 180 Q