Difference between revisions of "6CC001 Workshop - week 02"

From mi-linux
Jump to navigationJump to search
Line 1: Line 1:
 
[[Main Page]] >> [[CP3207|Web Application Development]] >> [[Web Application Developpment - Workbook|Workbook]] >> Week 02
 
[[Main Page]] >> [[CP3207|Web Application Development]] >> [[Web Application Developpment - Workbook|Workbook]] >> Week 02
 +
 +
<pre>
 +
class ShopProduct
 +
{
 +
  public $title;
 +
  public $producerMainName;
 +
  public $producerFirstName;
 +
  public $price;
 +
 +
  function __construct($title, $firstName, $mainName, $price)
 +
  {
 +
    $this->title = $title;
 +
    $this->producerFirstName = $firstName;
 +
    $this->producerMainName = $mainName;
 +
    $this->price = $price;
 +
  }
 +
 +
  public function getProducer()
 +
  {
 +
    return "$this->producerFirstName
 +
            $this->producerMainName";
 +
  }
 +
 +
  public function getProductDetails()
 +
  {
 +
    $s = "$this->title ($this->producerFirstName $this->producerMainName)";
 +
    return $s;
 +
  }
 +
}
 +
</pre>
 +
 +
<pre>
 +
class CdProduct extends ShopProduct
 +
{
 +
  public $playLength;
 +
 
 +
  // Let's override the constructor
 +
  function __construct($title, $firstName, $mainName, $price, $playLength)
 +
  {
 +
    parent::__construct($title, $firstName, $mainName, $price);
 +
    $this->playLength = $playLength;
 +
  }
 +
 +
function getPlayLength()
 +
  {
 +
    return $this->playLength;
 +
  }
 +
 
 +
  // Let's override getProductDetails
 +
  function getProductDetails()
 +
  {
 +
    $s = parent::getProductDetails();
 +
    $s.= " - playing time : $this->playLength";
 +
    return $s;
 +
  }
 +
}
 +
</pre>
 +
 +
<pre>
 +
class BookProduct extends ShopProduct
 +
{
 +
  public $numPages;
 +
 
 +
  // Let's override the constructor
 +
  function __construct($title, $firstName, $mainName, $price, $numPages)
 +
  {
 +
    parent::__construct($title, $firstName, $mainName, $price);
 +
    $this->numPages = $numPages;
 +
  }
 +
 +
function getNumberOfPages()
 +
  {
 +
    return $this->numPages;
 +
  } 
 +
 
 +
  // Let's override getProductDetails
 +
  function getProductDetails()
 +
  {
 +
    $s = parent::getProductDetails();
 +
    $s.= " - number of pages : $this->numPages";
 +
    return $s;
 +
  } 
 +
}
 +
</pre>
 +
 +
<pre>
 +
$CD1 = new CdProduct("Back to black", "Amy", "Winehouse", 7.99, 63);
 +
$CD2 = new CdProduct("Back to bedlam", "James", "Blunt", 7.99, 51);
 +
 +
$Book1 = new BookProduct("1984", "George", "Orwell", 8.99, 352);
 +
$Book2 = new BookProduct("Never Let", "Kazuo", "Ishiguro", 7.99, 276);
 +
 +
echo $CD1->getProductDetails()."<br>";
 +
echo $CD2->getProductDetails()."<br>";
 +
echo $Book1->getProductDetails()."<br>";
 +
echo $Book2->getProductDetails()."<br>";
 +
</pre>

Revision as of 13:48, 11 September 2009

Main Page >> Web Application Development >> Workbook >> Week 02

class ShopProduct
{
  public $title;
  public $producerMainName;
  public $producerFirstName;
  public $price;
 
  function __construct($title, $firstName, $mainName, $price)
  {
    $this->title = $title;
    $this->producerFirstName = $firstName;
    $this->producerMainName = $mainName;
    $this->price = $price;
  }

  public function getProducer()
  {
    return "$this->producerFirstName 
            $this->producerMainName";
  }

  public function getProductDetails()
  {
    $s = "$this->title ($this->producerFirstName $this->producerMainName)";
    return $s;
  }
}
class CdProduct extends ShopProduct
{
  public $playLength;
  
  // Let's override the constructor
  function __construct($title, $firstName, $mainName, $price, $playLength)
  {
    parent::__construct($title, $firstName, $mainName, $price);
    $this->playLength = $playLength;
  }

 function getPlayLength()
  {
    return $this->playLength;
  }
  
  // Let's override getProductDetails
  function getProductDetails()
  {
    $s = parent::getProductDetails();
    $s.= " - playing time : $this->playLength";
    return $s;
  }
}
class BookProduct extends ShopProduct
{
  public $numPages;
  
  // Let's override the constructor
  function __construct($title, $firstName, $mainName, $price, $numPages)
  {
    parent::__construct($title, $firstName, $mainName, $price);
    $this->numPages = $numPages;
  } 

 function getNumberOfPages()
  {
    return $this->numPages;
  }  
  
  // Let's override getProductDetails
  function getProductDetails()
  {
    $s = parent::getProductDetails();
    $s.= " - number of pages : $this->numPages";
    return $s;
  }  
}
$CD1 = new CdProduct("Back to black", "Amy", "Winehouse", 7.99, 63);
$CD2 = new CdProduct("Back to bedlam", "James", "Blunt", 7.99, 51);

$Book1 = new BookProduct("1984", "George", "Orwell", 8.99, 352);
$Book2 = new BookProduct("Never Let", "Kazuo", "Ishiguro", 7.99, 276);

echo $CD1->getProductDetails()."<br>";
echo $CD2->getProductDetails()."<br>";
echo $Book1->getProductDetails()."<br>";
echo $Book2->getProductDetails()."<br>";