For this assignment, implement a class called Product that will be used to represent a product that might be found in a novelty shop.
class Product
The Product class definition should be placed at the top of a source code file while the method implementation should be done after the closing curly brace for main().
Data Members
The Product class should contain four private data members. They are:
an array of characters that will hold the product identification code. It should have room for 11 characters plus a null terminator.
an array of characters that will hold the product name. It should have room for 45 characters plus a null terminator.
a float (or double) variable to hold the price of the product
an integer to hold the quantity of the product that is in stock
Constructors
Constructor 1:
The first constructor for the Product class (the default constructor) takes no arguments. Like all C++ constructors, it does not have a return data type.
It should set the product code and name data members to "null strings". This can be done by copying a null string literal ("") into a character array using strcpy() or by setting the first element of the array to a null character ('\0'). The price and quantity in stock data members should be set to 0.
Constructor 2:
The second constructor for the Product class should take four arguments: an array of character that represents a product code, an array of character that represents a product name, a float (or double) that represents a price for the product, and an integer that represents the quantity in stock for the product. DO NOT GIVE THESE ARGUMENTS THE SAME NAMES AS YOUR DATA MEMBERS. Like all C++ constructors, this constructor does not have a return data type.
Call the various "set" methods to set the value for all 4 data members.
Methods
getPrice
This accessor method returns the price data member. It takes no arguments and returns a float (or double).
getQuantity
This accessor method returns the quantity in stock data member. It takes no arguments and returns an integer.
setProductCode
This method will set the product code for an object. It takes one argument: an array of characters that holds a new product code. It returns nothing. The method should use the strcpy function to copy the new product code into the product code data member.
setName
This method will set the product name for an object. It takes one argument: an array of characters that holds a new product name. It returns nothing. The method should use the strcpy function to copy the new product name into the product name data member.
setPrice
This method will set the price for an object. It takes one argument: a double that holds a new price. It returns nothing. The method should check if the new price is greater than or equal to 0. If it is, it should set the price data member to the new price. Otherwise, it should set the price data member to 0.
setQuantity
This method will set the quantity in stock for an object. It takes one argument: an integer argument that holds a new quantity in stock. It returns nothing. The method should check if the new quantity is greater than or equal to 0. If it is, it should set the quantity data member to the new quantity. Otherwise, it should set the quantity data member to 0.
fulfillOrder
This method will process an order. It takes one argument: an integer that represents the quantity of this product that has been ordered. It returns an integer, which is the quantity of the product that the store is actually able to ship at this time.
The logic for this method should be as follows:
If the order quantity is less than zero, the order is in error. The number shipped should be zero. Do not alter the quantity in stock for the product.
If the order quantity is less than or equal to the quantity in stock, the order can be completely filled. The number shipped should be the same as the order quantity, and the order quantity should be subtracted from the quantity in stock.
Otherwise, this order can not be completely filled. The number shipped should be the quantity in stock, and the quantity in stock should be set to zero.
print
This method displays the contents of an object. It takes no arguments and returns nothing. It should print the product code, name, price, and quantity members on a single line. Use setw() to line the printed values up in columns (a width of 14 for the product code, 46 for the name, 6 for the price, and 6 for the quantity will match the sample output). The product code and name should be left-justified; the price and quantity should be right justified. The price should be printed using fixed-point notation with two places after the decimal point.
main()
In main(), create 3 Product objects. They should contain the values:
The first product should be created with the default constructor (ie. the one that takes no arguments)
The second product should be created with the second constructor. The product code should be "22222222222", product name "Virtual Education Pack", price of $0.99, and quantity in stock of 31.
The third product should be created with the second constructor. The product code should be "33333333333", product name "Dehydrated Water Bed", price of $-12.99, and quantity in stock of -6.
The rest of main() will include using the various methods on each of the 3 Product objects.
For each of the products, call the print method to display the product information. Before each call to print, display a short title such as "Product 1".
For product 1, call the set methods to set the product code to "11111111111", product name to "Flowbee Pet Groomer", price to $339.99, and the quantity in stock to 28. Call the print method to display the updated product 1 information. As before, make sure to include a short title such as "Product 1"
For product 2, call the getPrice and getQuantity methods and display the price and quantity in stock with appropriate labels.
For product 1, call the print method to display the initial product information. Call the fulfillOrder method and attempt to order -5 product 1s. Display the number of product 1s that were shipped. Call the print method again to display the updated product information.
For product 1, call the print method to display the initial product information. Call the fulfillOrder method and attempt to order 12 product 1s. Display the number of product 1s that were shipped. Call the print method again to display the updated product information.
For product 2, call the print method to display the initial product information. Call the fulfillOrder method and attempt to order 4 product 2s. Display the number of product 2s that were shipped. Call the print method again to display the updated product information.
For product 2, call the print method to display the initial product information. Call the fulfillOrder method and attempt to order 45 product 2s. Display the number of product 2s that were shipped. Call the print method again to display the updated product information.
Product product2=Product{"22222222222","Virtual Education Pack",0.99,31};
Product product3=Product{"33333333333","Dehydrated Water Bed",-12.99,-6};
should be
Product product2=Product("22222222222","Virtual Education Pack",0.99,31);
Product product3=Product("33333333333","Dehydrated Water Bed",-12.99,-6);
add this to the end of int main()
system("pause");
return 0;
and dont forget setw() and setprecision()
in the Product::print method