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...
2. Create a class called Invoice that a store might use to represent an invoice for...
2. Create a class called Invoice that a store might use to represent an invoice for an item sold at the store. An Invoice should include four data members—the ID for the item sold (type string), name of item (type string), item description (type string) and the price of the item (type int). Your class should have a constructor that initializes the four data members. A constructor that receives multiple arguments. Example: ClassName( TypeName1 parameterName1, TypeName2 parameterName2, ... ) Provide...
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....
12. Create a class called bMoney. It should store money amounts as long doubles. Use the...
12. Create a class called bMoney. It should store money amounts as long doubles. Use the function mstold() to convert a money string entered as input into a long double, and the function ldtoms() to convert the long double to a money string for display. (See Exercises 6 and 10.) You can call the input and output member functions getmoney() and putmoney(). Write another member function that adds two bMoney amounts; you can call it madd(). Adding bMoney objects is...
IN C++ Create a class called TextInt. The purpose of a TextInt is to store an...
IN C++ Create a class called TextInt. The purpose of a TextInt is to store an integer and convert it to the English text form of the integer when needed, such as ‘zero’ for 0, ‘one’ for 1, and so on, up to ‘nine thousand nine hundred ninety nine’ for 9999. You do NOT need punctuation (commas, hyphens, ‘and’, etc.) The TextInt class should have its own .h and .cpp files. At least the translate function should be implemented in...
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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT