design an inventory class that can hold information for an item in a retail stor
ID: 3541821 • Letter: D
Question
design an inventory class that can hold information for an item in a retail stores inventory. the class should have the following private member variables.
variable name
itemNumber--an int that holds the items 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
default constructor- sets all the member variables to 0.
constructor #2- accepts an items number, quantity, and cost 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. the program should validate the user inputs to ensure that negative values are not accepted for item number, quantity, or cost.
Explanation / Answer
#include<iostream>
using namespace std;
class inventory
{
private:
int itemNumber; //--an int that holds the items number
int quantity; // -- an int that holds the quantity of the item on hand
double cost; //-- a double that holds the wholesale per-unit cost of the item
double totalCost;
public:
inventory()
{
itemNumber = 0;
quantity = 0;
cost = 0.0;
totalCost= 0.0;
}
inventory(int in,int q,double c)
{
setItemNumber(in);
setQuantity(q);
setCost(c);
setTotalCost();
}
void setItemNumber(int in)
{
itemNumber = in;
}
void setQuantity(int q)
{
quantity = q;
}
void setCost(double c)
{
cost = c;
}
void setTotalCost()
{
totalCost = cost*quantity;
}
int getItemNumber() //--returns the value in ItemNumber
{
return itemNumber;
}
int getQuantity() // - returns the value in quantity
{
return quantity;
}
double getCost() //-- returns the value in cost
{
return cost;
}
double getTotalCost()//-- computes and returns the totalCost
{
return cost*quantity;
}
};
int main()
{
int itemNumber;
int quantity;
double cost;
cout << "enter item Number ";
cin >> itemNumber;
cout << endl;
while(itemNumber<=0)
{
cout << "Invalid input.enter item Number ";
cin >> itemNumber;
cout << endl;
}
cout << "enter quantity ";
cin >> quantity;
cout << endl;
while(quantity<=0)
{
cout << "Invalid input.enter quantity ";
cin >> quantity;
cout << endl;
}
cout << "enter cost of item ";
cin >> cost;
cout << endl;
while(cost<=0)
{
cout << "Invalid input.enter cost of item ";
cin >> cost;
cout << endl;
}
inventory inv1(itemNumber,quantity,cost);
cout << "Inventory total cost given by " << inv1.getTotalCost() << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.