In: Computer Science
with PHP Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables — a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities.
// file: invoice.php
<?php
// Invoice class
class Invoice
{
public $part_number;
public $part_description;
public $quantity;
public $price_per_item;
// Constructor
public function __construct($part_number,
$part_description, $quantity, $price_per_item)
{
$this->part_number =
$part_number;
$this->part_description =
$part_description;
if($quantity < 0)
$this->quantity = 0;
else
$this->quantity = quantity;
if(price_per_item < 0)
$this->price_per_item = 0;
else
$this->price_per_item = price_per_item;
}
// getters
public function get_partnumber()
{
return $this->part_number;
}
public function get_partdescription()
{
return
$this->part_description;
}
public function get_quantity()
{
return $this->quantity;
}
public function get_priceperitem()
{
return
$this->price_per_item;
}
// setters
public function set_partnumber($part_number)
{
$this->part_number =
$part_number;
}
public function
set_partdescription($part_description)
{
$this->part_description =
$part_description;
}
public function set_quantity($quantity)
{
if($quantity > 0)
$this->quantity = $quantity;
else
$this->quantity = 0;
}
public function
set_priceperitem($price_per_item)
{
if($price_per_item > 0)
$this->price_per_item = price_per_item;
else
$this->price_per_item = 0;
}
// method to calculate the total invoice amount
public function getInvoiceAmount ()
{
return($this->quantity*$this->price_per_item);
}
}//end class Invoice
?>
//end of invoice.php
//file : invoiceTest.php
<?php
include("invoice.php");
$invoice1 = new Invoice('P001','Nut',10,2.5);
print('Part Number : '.$invoice1->get_partnumber().', Part
Description : '.$invoice1->get_partdescription().' Quantity :
'.$invoice1->get_quantity().' Price per item :
$'.$invoice1->get_priceperitem().' Invoice Amount :
$'.$invoice1->getInvoiceAmount());
?>
//end of invoiceTest.php