Last active
July 10, 2018 07:01
-
-
Save platinize/a086910089ec8db2fe367d0670b21289 to your computer and use it in GitHub Desktop.
task 10.07.18
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Через Interface | |
//разбить на логические блоки | |
class Customer { // psr-2 "{" c новой строки | |
public $name; | |
public $rentals; | |
public function __construct($name) { | |
$this->name = $name; | |
} | |
public function addRental(Rental $rental) { | |
$this->rentals[] = $rental; | |
} | |
public function getName() { | |
return $this->name; | |
} | |
public function statement() { | |
$totalAmount = 0; | |
$frequentRenterPoints = 0; | |
$result = "Rental Record for " . $this->getName() . "\n"; | |
foreach ($this->rentals as $each) { // вынести в отдельный метод | |
$thisAmount = 0; | |
switch ($each->movie->getPriceCode()) { | |
case Movie::REGULAR: | |
$thisAmount += 2; | |
if ($each->getDaysRented() > 2) { | |
$thisAmount += ($each->getDaysRented() - 2) * 1.5; | |
} | |
break; | |
case Movie::NEW_RELEASE: | |
$thisAmount += $each->getDaysRented() * 3; | |
break; | |
case Movie::CHILDRENS: | |
$thisAmount += 1.5; | |
if ($each->getDaysRented() > 3) { | |
$thisAmount += ($each->getDaysRented() - 3) * 1.5; | |
} | |
break; | |
} | |
$frequentRenterPoints++; | |
// add bonus for a two day release rental | |
if (($each->movie->getPriceCode() == Movie::NEW_RELEASE) && ($each->getDaysRented() > 1)) { | |
$frequentRenterPoints++; | |
} | |
$result .= "\t" . $each->movie->getTitle() . "\t" . $thisAmount . "\n"; | |
$totalAmount += $thisAmount; | |
} | |
// add footer lines | |
$result .= "Amount owed is " . $totalAmount . "\n"; //вынести в отдельный метод | |
$result .= "You earned " . $frequentRenterPoints . " frequent renter points"; | |
return $result; | |
} | |
} | |
class Movie { // psr-2 "{" c новой строки | |
const CHILDRENS = 2; | |
const REGULAR = 0; | |
const NEW_RELEASE = 1; | |
public $title; | |
public $priceCode; | |
public function __construct($title, $priceCode) { | |
$this->title = $title; | |
$this->setPriceCode($priceCode); | |
} | |
public function getPriceCode() { | |
return $this->priceCode; | |
} | |
public function setPriceCode($priceCode) { | |
$this->priceCode = $priceCode; | |
} | |
public function getTitle() { | |
return $this->title; | |
} | |
} | |
class Rental { // psr-2 "{" c новой строки | |
public $movie; | |
public $daysRented; | |
public function __construct(Movie $movie, $daysRented) { | |
$this->movie = $movie; | |
$this->daysRented = $daysRented; | |
} | |
public function getDaysRented() { | |
return $this->daysRented; | |
} | |
public function getMovie() { | |
return $this->movie; | |
} | |
} | |
// define customer | |
$customer = new Customer('Adam Culp'); | |
// choose movie to be rented, define rental, add it to the customer | |
$movie = new Movie('Gladiator', 0); | |
$rental = new Rental($movie, 1); | |
$customer->addRental($rental); | |
// choose 2nd movie to be rented, define rental, add it to the customer | |
$movie = new Movie('Spiderman', 1); | |
$rental = new Rental($movie, 2); | |
$customer->addRental($rental); | |
// print the statement | |
echo $customer->statement(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment