Inventory class code, due in 3 hours
Oct 28, 2015 at 3:35am UTC
-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.
-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.
So we have to create a separate file "Inventory.h" and our main file.
I am not sure how to compile and get my main file to draw info from my .h file.
This is my main file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include "inventory.h"
#include <iostream>
using namespace std;
int main()
{
int itemNumber; // declare variables for inventory number, quantity, cost
int quantity;
double cost;
double totalCost;
Inventory(); // declare the Inventory object using the default constructor
{
itemNumber = 0;
quantity = 0;
cost = 0;
totalCost = 0;
}
cout << "Enter item number: " ; // collect and validate item number
cin >> itemNumber;
while (itemNumber < 0)
{
cout << "Error. Please enter a positive value for the item number: " ;
cin >> itemNumber;
}
cout << "Enter item quantity: " ; // collect and validate quantity
cin >> quantity;
while (quantity < 0)
{
cout << "Error. Please enter a positive value for the quantity: " ;
cin >> quantity;
}
cout << "Enter item cost: " ; // collect and validate cost
cin >> cost;
while (cost < 0)
{
cout << "Error. Please enter a positive value for the cost: " ;
cin >> cost;
}
Inventory information(itemNumber, quantity, cost);
totalCost = information.getTotalCost();
itemNumber = information.getItemNumber(); // change the inventory number using the setItemNumber() member function
quantity = information.getQuantity(); // change the quantity using the setQuantity() member function
cost = information.getCost(); // change the cost using the setCost() member function
cout<< "Inventory Summary\n" ; // display the contents of Inventory item using getItemNumber(), getQuantity(), and getCost()
cout << "=================\n" ;
cout << "Item number: " << itemNumber << endl;
cout << "Item quantity: " << quantity << endl;
cout << "Item cost: " << cost << endl;
cout << "-----------------\n" ;
cout << "Total Cost: " << totalCost << endl; // display the total cost using getTotalCost() member function (which returns cost * quantity)
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
class Inventory
{
private :
int itemNumber;
int quantity;
double cost;
double totalCost;
public :
Inventory() // declare the Inventory object using the default constructor
{
itemNumber = 0;
quantity = 0;
cost = 0;
totalCost = 0;
}
Inventory(int newItemNumber, int newQuantity, double newCost)
{
itemNumber = newItemNumber;
quantity = newQuantity;
cost = newCost;
setTotalCost(quantity, cost);
}
void setItemNumber(int )
{
itemNumber = itemNumber;
}
void setQuantity(int )
{
quantity = quantity;
}
void setCost(double )
{
cost = cost;
}
void setTotalCost(int , double )
{
totalCost = quantity * cost;
}
int getItemNumber()
{
return itemNumber;
}
int getQuantity()
{
return quantity;
}
double getCost()
{
return cost;
}
double getTotalCost()
{
return totalCost;
}
};
Oct 28, 2015 at 3:47am UTC
Assuming you are using an IDE like VS or CodeBlocks all you need to do is create a blank project and add the two files to it via the menu (something like add existing item or file) then compile the project by pressing the run (>) button in the toolbar.
FWIW You can run it here as just one big file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
//#include "inventory.h" //you don't need this because it's all one big file for this demo
#include <iostream>
using namespace std;
class Inventory
{
private :
int itemNumber;
int quantity;
double cost;
double totalCost;
public :
Inventory() // declare the Inventory object using the default constructor
{
itemNumber = 0;
quantity = 0;
cost = 0;
totalCost = 0;
}
Inventory(int newItemNumber, int newQuantity, double newCost)
{
itemNumber = newItemNumber;
quantity = newQuantity;
cost = newCost;
setTotalCost(quantity, cost);
}
void setItemNumber(int )
{
itemNumber = itemNumber;
}
void setQuantity(int )
{
quantity = quantity;
}
void setCost(double )
{
cost = cost;
}
void setTotalCost(int , double )
{
totalCost = quantity * cost;
}
int getItemNumber()
{
return itemNumber;
}
int getQuantity()
{
return quantity;
}
double getCost()
{
return cost;
}
double getTotalCost()
{
return totalCost;
}
};
int main()
{
int itemNumber; // declare variables for inventory number, quantity, cost
int quantity;
double cost;
double totalCost;
Inventory(); // declare the Inventory object using the default constructor
{
itemNumber = 0;
quantity = 0;
cost = 0;
totalCost = 0;
}
cout << "Enter item number: " ; // collect and validate item number
cin >> itemNumber;
while (itemNumber < 0)
{
cout << "Error. Please enter a positive value for the item number: " ;
cin >> itemNumber;
}
cout << "Enter item quantity: " ; // collect and validate quantity
cin >> quantity;
while (quantity < 0)
{
cout << "Error. Please enter a positive value for the quantity: " ;
cin >> quantity;
}
cout << "Enter item cost: " ; // collect and validate cost
cin >> cost;
while (cost < 0)
{
cout << "Error. Please enter a positive value for the cost: " ;
cin >> cost;
}
Inventory information(itemNumber, quantity, cost);
totalCost = information.getTotalCost();
itemNumber = information.getItemNumber(); // change the inventory number using the setItemNumber() member function
quantity = information.getQuantity(); // change the quantity using the setQuantity() member function
cost = information.getCost(); // change the cost using the setCost() member function
cout<< "Inventory Summary\n" ; // display the contents of Inventory item using getItemNumber(), getQuantity(), and getCost()
cout << "=================\n" ;
cout << "Item number: " << itemNumber << endl;
cout << "Item quantity: " << quantity << endl;
cout << "Item cost: " << cost << endl;
cout << "-----------------\n" ;
cout << "Total Cost: " << totalCost << endl; // display the total cost using getTotalCost() member function (which returns cost * quantity)
return 0;
}
Enter item number: 2
Enter item quantity: 6
Enter item cost: 8
Inventory Summary
=================
Item number: 2
Item quantity: 6
Item cost: 8
-----------------
Total Cost: 48
Exit code: 0 (normal program termination)
Topic archived. No new replies allowed.