Inventory Class

Oct 16, 2013 at 6:18am
Design a Inventory Class that can hold information for an item in a retail store's Inventory. I have to create an Inventory class with the private member variables itemNumber, quantity, cost, and totalCost. I have to have public functions including a default constructor, a second constructor that accepts itemNumber, quantity, and cost as arguments.Calls other class functions to copy these values into the appropriate member variables.

Heres my code my output seems to come out wrong any idea what it is I can't seem to see it?
Thanks,

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
#include "stdafx.h"
#include <iostream>
using namespace std;

class Inventory
{
	private:
		int itemNumber;
		int quantity;
		double cost;
	    double totalCost;
	public:
		Inventory()
		{
			itemNumber = 0;
			quantity = 0;
			cost = 0;
			totalCost = 0;

		}
		Inventory(int itemNumber, int quantity, double cost)
		{
			itemNumber = getItemNumber();
			quantity = getQuantity();
			cost = getCost();
			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;
	int quantity;
	double cost;
	double totalCost;

	cout << "Enter the Item Number: ";
	cin >> itemNumber;
	while (itemNumber < 0)
	{
		cout << "Please enter a positive value for the Item Number: ";
		cin >> itemNumber;
	}
	cout << "Enter the Quantity of the item: ";
	cin >> quantity;
	while (quantity < 0)
	{
		cout << "Please enter a positive value for the Quantity of the item: ";
		cin >> quantity;
	}
	cout << "Enter the Cost of the item: ";
	cin >> cost;
	while (cost < 0)
	{
		cout << "Please enter a positive value for the Cost of the item: ";
		cin >> cost;
	}

	Inventory information(itemNumber, quantity, cost);

	totalCost = information.getTotalCost();
	itemNumber = information.getItemNumber();
	cost = information.getCost();
	quantity = information.getQuantity();
	cout << "The Item Number is: " << itemNumber << endl;
	cout << "The Quantity is: " << quantity << endl;
	cout << "The Cost is: " << cost << endl;
	cout << "The Total Cost is: " << totalCost << endl;
	
	return 0;
}


int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}
Last edited on Oct 16, 2013 at 6:19am
Oct 16, 2013 at 6:27am
Your overloaded constructor is wrong.
1
2
3
4
5
6
7
Inventory(int itemNumber, int quantity, double cost)
{
  itemNumber = getItemNumber(); // itemNumber has not been initialized, how can getitemNumber be called?
  quantity = getQuantity();
  cost = getCost();
  setTotalCost(quantity, cost);
}


Change it to:
1
2
3
4
5
6
7
Inventory(int newItemNumber, int newQuantity, double newCost)
{
  itemNumber = newItemNumber;
  quantity = newQuantity;
  cost = newCost;
  setTotalCost(quantity, cost); // this can be called as quantity and cost have now been initialized.
}
Topic archived. No new replies allowed.