In: Computer Science
C++! Becareful following the test case.
7. Inventory
Class
Design an Inventory class that can hold information for an item in
a retail store’s inventory.
The class should have the following private member variables.
Variable
Name Description
itemNumber An int that holds the item’s number.
quantity
An int that holds the quantity of the item on hand.
cost A double that holds the wholesale per-unit cost
of the item
The class should have the following public member functions
Member
Function
Description
default
constructor
Sets all the member variables to 0.
constructor #2
Accepts an item’s number, quantity, and cost as arguments. Calls
other class functions to copy these values into the appropriate
member variables. Then calls the setTotalCost function.
setItemNumber Accepts an int argument and
copies it into the itemNumber member variable.
setQuantity
Accepts an int argument and copies it into the
quantity member variable.
setCost Accepts a double argument and copies it into
the cost member variable.
getItemNumber Returns the value in
itemNumber.
getQuantity
Returns the
value in quantity.
getCost
Returns the value in cost.
getTotalCost Computes and returns the totalCost.
Demonstrate the class
by writing a simple program that uses it. This program should
validate
the user inputs to ensure that negative values are not accepted for
item number, quantity, or cost.
Here, your objective is to Create three objects and display their contents. See the following code segment. Here is detail explaination.
1. You create the First object which uses the default constructor. The default constructor initializes all data to 0. You can use the following code to do this.
Inventory part1; // This uses default constructor
2. Second object is created and initialized with the programmer hard coded data as follows:
int i = 1234;
int q = 10;
double p = 2.5;
Inventory part2(i, q, p);
3. To create the third object, you need to collect the data from the user. Once you collect the data from the user, insert this data into the object.
Once you create all the above three objects, all you have to do is to display the contents of the object.
Here are the test cases:
Test Case1:
Enter data for the new
item
Item number: 123
Quantity: 30
Price: 20
Part Number : 0
Units On Hand : 0
Price : $0.00
Total Cost : $0.00
Part Number :
1234
Units On Hand : 10
Price : $2.50
Total Cost : $25.00
Part Number :
123
Units On Hand : 30
Price : $20.00
Total Cost : $600.00
Test Case2:
Enter data for the new
item
Item number: 342
Quantity: -2
Value must be greater than zero: 50
Price: -20
Value must be greater than zero: 30
Part Number : 0
Units On Hand : 0
Price : $0.00
Total Cost : $0.00
Part Number :
1234
Units On Hand : 10
Price : $2.50
Total Cost : $25.00
Part Number :
342
Units On Hand : 50
Price : $30.00
Total Cost : $1500.00
C++ Program:
#include
#include
using namespace std;
//Class definition
class Inventory
{
//Data Members
private:
int itemNumber;
int quantity;
double cost;
//Public Methods
public:
//Default Constructor
Inventory()
{
itemNumber=0;
quantity=0;
cost=0.0;
}
//Argument Constructor
Inventory(int iNum, int qty, double c)
{
itemNumber = iNum;
quantity = qty;
cost = c;
}
//Setter Methods
void setItemNumber(int iNum)
{
itemNumber = iNum;
}
void setQuantity(int qty)
{
quantity = qty;
}
void setCost(double c)
{
cost = c;
}
//Getter Methods
int getItemNumber()
{
return itemNumber;
}
int getQuantity()
{
return quantity;
}
double getCost()
{
return cost;
}
//Returns total cost
double getTotalCost()
{
return (cost * quantity);
}
};
//Main function
int main()
{
//Crating objects
Inventory part1; // This uses default constructor
int i = 1234;
int q = 10;
double p = 2.5;
Inventory part2(i, q, p);
//Reading data from user
cout << "Enter data for the new item" << endl;
cout << "Item number: ";
cin >> i;
//Validating value
while(i < 0)
{
cout << "Value must be greater than zero: ";
cin >> i;
}
cout << "Quantity: ";
cin >> q;
//Validating value
while(q < 0)
{
cout << "Value must be greater than zero: ";
cin >> q;
}
cout << "Price: ";
cin >> p;
//Validating value
while(p < 0)
{
cout << "Value must be greater than zero: ";
cin >> p;
}
//Creating third object
Inventory part3(i, q, p);
//Setting precision to two decimal places
cout << fixed << setprecision(2);
//Printing values
cout << "Part Number : " << part1.getItemNumber()
<< endl;
cout << "Units On Hand : " << part1.getQuantity()
<< endl;
cout << "Price : $" << part1.getCost() <<
endl;
cout << "Total Cost : $" << part1.getTotalCost()
<< endl;
cout << "Part Number : " << part2.getItemNumber()
<< endl;
cout << "Units On Hand : " << part2.getQuantity()
<< endl;
cout << "Price : $" << part2.getCost() <<
endl;
cout << "Total Cost : $" << part2.getTotalCost()
<< endl;
cout << "Part Number : " << part3.getItemNumber()
<< endl;
cout << "Units On Hand : " << part3.getQuantity()
<< endl;
cout << "Price : $" << part3.getCost() <<
endl;
cout << "Total Cost : $" << part3.getTotalCost()
<< endl;
cout << endl;
return 0;
}
____________________________________________________________________________________________
Sample Run: