Question

In: Computer Science

with PHP Create a class called Invoice that a hardware store might use to represent an...

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.

Solutions

Expert Solution

// 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


Related Solutions

Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - 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. If the quantity passed to the constructor is...
Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - 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. If the quantity passed to the constructor is...
3.12 (Invoice Class) Create a class called Invoice that a hardware store might use to represent...
3.12 (Invoice Class) 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...
With PHP Create a class called Account that a bank might use to represent customers' bank...
With PHP Create a class called Account that a bank might use to represent customers' bank accounts. Your class should include one data member of type int to represent the account balance. Your class should provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it is greater than or equal to 0. If not, the balance should be set to 0 and the...
----------------------------------------------------------------------------------------------- Create a class called MathOperations that a teacher might use to represent the basic type...
----------------------------------------------------------------------------------------------- Create a class called MathOperations that a teacher might use to represent the basic type of mathematical operations that may be performed. The class should include the following: Three double private variables as instance variables, number1, number2, and result. Your class should have a default constructor that initializes the three instance variables to zero. Your class should also have a constructor that initializes the two instance variables (number1 and number2) to the value entered by the user from keyboard....
Please solve using jupyter notebook . 10.10- (Invoice Class) Create a class called Invoice that a...
Please solve using jupyter notebook . 10.10- (Invoice Class) 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 data attributes—a part number (a string), a part description (a string), a quantity of the item being purchased (an int) and a price per item (a Decimal). Your class should have an __init__ method that initializes the four data attributes....
with PHP Create a class called Employee that includes three instance variables—a first name (type String),...
with PHP Create a class called Employee that includes three instance variables—a first name (type String), a last name (type String) and a monthly salary int). Provide a constructor that initializes the three instance data member. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its 0. Write a test app named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary....
Create a class named UsedFurnitureItem to represent a used furniture item that the store sells. Private...
Create a class named UsedFurnitureItem to represent a used furniture item that the store sells. Private data members of a UsedFurnitureItem are: age (double) // age in years – default value for brandNewPrice (double) // the original price of the item when it was brand new description (string) // a string description of the item condition (char) // condition of the item could be A, B, or C. size (double) // the size of the item in cubic inches. weight...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not reduce fractions, do not use "const," do not provide any constructors, do not use three separate files. You will not receive credit for the assignment if you do any of these things. In your single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. It is required that you provide these member...
. Create a class called Book to represent a book. A Book should include four pieces...
. Create a class called Book to represent a book. A Book should include four pieces of information as instance variables‐a book name, an ISBN number, an author name and a publisher. Your class should have a constructor that initializes the four instance variables. Provide a mutator method and accessor method (query method) for each instance variable. Inaddition, provide a method named getBookInfo that returns the description of the book as a String (the description should include all the information...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT