Professor marked me down because my code was lacking. What do you think of my code?

I'm very upset right now as I'm nearly failing my C++ part 2 class (I took part 1 last semester and got an A). So my C++ professor gave me a low score on a homework assignment because my coding was hard to follow, and it was an ad hoc solution (program "designed at the keyboard"). He also said it does not use Object-Oriented principles and structure. I used all of the knowledge I learned in my previous C++ class. How should my code have looked like if I wanted to meet his needs? Here was the problem:


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. Create 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.


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
#include <iostream>
using namespace std;

class inventory
	{
		private:
				int itemNumber;
				int quantity;
				double cost;
				double totalCost;
		public:
			inventory()
			{
				itemNumber = 0;
				quantity = 0;
				cost = 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(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;

	inventory();       // declare the Inventory object using the default constructor
	{
		itemNumber = 0;
		quantity = 0;
		cost = 0;
		totalCost = 0;
	}

	cout << "What is the item number\n";
	cin >> itemNumber;
	while (itemNumber < 0)
		cout << "error\n";

	cout << "What is the quantity\n";
	cin >> quantity;
	while (quantity < 0)
		cout << "error\n";

	cout << "What is the cost\n\n";
	cin >> cost;
	while (cost < 0)
		cout << "error\n";

	inventory information(itemNumber, quantity, cost);

	totalCost = information.getTotalCost();
	itemNumber = information.getItemNumber();   
	quantity = information.getQuantity(); 
	cost = information.getCost();

	cout << "Summary\n";   
	cout << "--------\n";
	cout << "Item number: " << itemNumber << endl;
	cout << "Item quantity: " << quantity << endl;
	cout << "Item cost: " << cost << endl;
	cout << "--------\n";
	cout << "Total Cost: " << totalCost << endl;


	return 0;
}
I think it may have helped if you created another class or struct named "Item" and make it an attribute inside Inventory class.

Following the OOAD principle, it does not make sense that Inventory has itemNumber, quantity, cost, and totalCost. It makes more sense if class Item has these attributes and class Inventory has Items. Basically, try to design your classes in a way that they model the real world objects.

Should look something like this and even this is very simplistic. This is what your professor may have meant by "Not using Object-Oriented principles and structure." ??
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
class Item
{
public:
    // Add whatever operations you want to perform on Item
    // Such as
    // 1. Return itemNumber, quantity, cost, totalCost, and etc
    // 2. Change values for itemNumber, quantity, cost, totalCost and etc

private:
    int itemNumber;
    int quantity;
    double cost
    double totalCost;
};

class Inventory
{
public:
    // Add whatever operations you want to perform on Inventory
    // Such as:
    // 1. Search Item by itemNumber
    // 2. Return total number of items in the inventory
    // 3. Remove an item
    // 4. Add an item
    // 5. Function to allow user to change attributes of a single item


private:
    std::vector<Item> items;
};


When designing objects, programmers utilize a number of tools and techniques carefully considering how class objects would interact with each other.

EDIT:
In the Inventory class, you can even specify how many of each item should be present in the inventory and inform user which items need to be restocked. You can do a lot of things with Inventory that I haven't even mentioned.
Last edited on
Thanks. Could you provide an example of the green #1 in class Item (return itemNumber), and also an example of the green #1 in class Inventory (Search Item by itemNumber)? I wanted to know what the syntax would look like.

Also, what does vector do?
vector is a data structure. It is a sequence container that represents an array.
http://www.cplusplus.com/reference/vector/vector/

I will show you an example of Item class first. Then I will build Inventory class.
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <iomanip>
#include <string>

/**
*   Description: Item Class. Will be used in Inventory
*   Must provide Unique Item Name, Identifier, quantity,
*       and cost to construct this object
*   Target Quantity is set to 10 by default
*/
class Item
{
public:
    /**
     *  Description: Constructor for Item class. Updates totalCost
     *  @param int itemNumber , int quantity , double cost
     */
    Item( std::string name , int itemNum , int theQuantity , double theCost )
    : itemName{ name } , itemNumber{ itemNum } , quantity{ theQuantity } , cost{ theCost }
    {
        totalCost = quantity * cost;
    }

    /**
     *  Description: Public getter function for Item Name
     *  @param none
     *  @return string Item Name
     */
     std::string getItemName() const
     { return itemName; }

    /**
     *  Description: Public getter function for itemNumber
     *  @param none
     *  @return int Item Identifier
     */
    int getItemNumber() const
    { return itemNumber; }

    /**
     *  Description: Public getter function for quantity
     *  @param none
     *  @return int Quantity of the item
     */
    int getQuantity() const
    { return quantity; }

    /**
     *  Description: Public getter function for cost
     *  @param none
     *  @return double Cost of the item
     */
    double getCost() const
    { return cost; }

    /**
     *  Description: Public getter function for totalCost
     *  @param none
     *  @return double Total Cost of the item
     */
    double getTotalCost() const
    { return totalCost; }

    /**
     *  Description: Public getter function for Target Item Quantity
     *  @param none
     *  @return int Item Target Quantity
     */
    int getTargetQuantity() const
    { return targetQuantity; }

    /**
     *  Description: Public setter function for Item Name
     *  @param string Item Name
     *  @return none
     */
    void setItemName( std::string name )
    { itemName = name; }

    /**
     *  Description: Public setter function for itemNumber
     *  @param int Item Identifier
     *  @return none
     */
    void setItemNumber( int itemNum )
    { itemNumber = itemNum; }

    /**
     *  Description: Public setter function for quantity. Update totalCost
     *  @param int Item Quantity
     *  @return none
     */
    void setQuantity( int theQuantity )
    {
        quantity = theQuantity;
        totalCost = quantity * cost;
    }

    /**
     *  Description: Public setter function for cost. Update totalCost
     *  @param double Item Cost
     *  @return none
     */
    void setCost( double theCost )
    {
        cost = theCost;
        totalCost = quantity * cost;
    }

    /**
     *  Description: Public setter function for targetQuantity.
     *  @param int Target Item Quantity to be considered stocked
     *  @return none
     */
    void setTargetQuantity( int tQuantity )
    { targetQuantity = tQuantity; }

    /**
     *  Description: Return true if this item needs to be restocked
     *  @param none
     *  @return true if item quantity is less than target quantity
     */
    bool requireRestock() const
    { return quantity < targetQuantity; }

    /**
     *  Description: Return true if this item is over-stocked
     *  @param none
     *  @return true if Item Quantity is over twice the target quantity
     */
    bool isOverStocked() const
    { return quantity > targetQuantity * 2; }

private:
    // Item Name
    std::string itemName;

    // This is a unique identifier for this item
    int itemNumber;

    int quantity;
    double cost;

    // cost X quantity.
    double totalCost;

    // Store must have at least this quantity for the item to
    // be considered adequately stocked
    int targetQuantity = 10;
};

/**
 *  Overloaded operator<< to format how item object is printed
 */
std::ostream & operator<< ( std::ostream &out , const Item &item )
{
    out << "\n------------------------\n";
    out << "Name: " << item.getItemName() << std::endl;
    out << "Item#: " << item.getItemNumber() << std::endl;
    out << "Quantity: " << item.getQuantity() << std::endl;
    out << std::setprecision( 2 ) << std::fixed;
    out << "Cost: " << item.getCost() << std::endl;
    out << "Total Cost: " << item.getTotalCost() << std::endl;

    if( item.requireRestock() ) {
        out << "Requires Restock: " << "Yes\n";
    }
    else {
        out << "Requires Restock: " << "No\n";
    }

    if( item.isOverStocked() ) {
        out << "Overstocked: " << "Yes\n";
    }
    else {
        out << "Overstocked: " << "No\n";
    }

    return out;
}

int main()
{
    Item soap{ "Soap" , 101 , 10 , 4.56 };
    Item toothpaste{ "Toothpaste" , 102 , 10 , 2.00 };
    Item razor{ "Razor" , 103 , 8 , 2.00 };
    Item pencil{ "Pencil" , 104 , 30 , 2.00 };
    Item eraser{ "Eraser" , 105 , 1 , 2.00 };

    std::cout << soap << toothpaste << razor << pencil << eraser;
}




------------------------
Name: Soap
Item#: 101
Quantity: 10
Cost: 4.56
Total Cost: 45.60
Requires Restock: No
Overstocked: No

------------------------
Name: Toothpaste
Item#: 102
Quantity: 10
Cost: 2.00
Total Cost: 20.00
Requires Restock: No
Overstocked: No

------------------------
Name: Razor
Item#: 103
Quantity: 8
Cost: 2.00
Total Cost: 16.00
Requires Restock: Yes
Overstocked: No

------------------------
Name: Pencil
Item#: 104
Quantity: 30
Cost: 2.00
Total Cost: 60.00
Requires Restock: No
Overstocked: Yes

------------------------
Name: Eraser
Item#: 105
Quantity: 1
Cost: 2.00
Total Cost: 2.00
Requires Restock: Yes
Overstocked: No

Process returned 0 (0x0) execution time : 0.061 s
Press any key to continue.
Last edited on
Here is the Inventory class implementation.
By carefully designing class objects, you can implement some useful features without too much trouble. Even this design is very simplistic like I said.

This is the basic implementation of inventory. I did not thoroughly test the program so there may be some issues, but try playing around with it and change it as necessary.
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
 *  Description: Inventory Class for storing items
 *  Default Constructor: Does not require any parameters
 *      Build an empty inventory
 *
 */
class Inventory
{
public:
    /**
     *  Description: Default Constructor with no parameters
     *      Build an empty inventory
     */
    Inventory() {}

    /**
     *  Description: Add an item to the inventory
     *  @param Item item to be added
     *  @return none
     *  This function will check if the item already exists
     *      by comparing item name and item number. If the
     *      item already exists, the quantity will be updated.
     */
    void addItem( const Item &itemToAdd )
    {
        // Search the existing item and see if this item already exists
        // If the item already exists, simply update the quantity
        for( auto &x : items ) {
            if( x.getItemNumber() == itemToAdd.getItemNumber() &&
                x.getItemName() == itemToAdd.getItemName() )
            {
                x.setQuantity( itemToAdd.getQuantity() + x.getQuantity() );
                return;
            }
        }

        // Add as a new item
        items.push_back( itemToAdd );
    }

    /**
     *  Description: Search an item by name. If found, print item info
     *      No action taken if item is not found
     *  @param string Item Name
     *  @return none
     */
    void printItemByName( std::string name ) const
    {
        for( auto x : items ) {
            if( x.getItemName() == name ) {
                std::cout << x;
            }
        }
    }

    /**
     *  Description: Search an item by item number. If found, print item info
     *      No Action taken if item is not found.
     *  @param int Item Number
     *  @return none
     */
    void printItemByNumber( int itemNumber ) const
    {
        for( auto x : items ) {
            if( x.getItemNumber() == itemNumber ) {
                std::cout << x;
            }
        }
    }

    /**
     *  Description: Search an item by name. If found, return true
     *  @param string Item Name
     *  @return true if item is found
     */
    bool searchItemByName( std::string name ) const
    {
        for( auto x : items ) {
            if( x.getItemName() == name ) {
                return true;
            }
        }

        return false;
    }

    /**
     *  Description: Search an item by item number. If found, return true
     *  @param int Item Number
     *  @return true if item is found
     */
    bool searchItemByNumber( int itemNumber ) const
    {
        for( auto x : items ) {
            if( x.getItemNumber() == itemNumber ) {
                return true;
            }
        }

        return false;
    }

    /**
     *  Description: Print all Understocked Items
     *  @param none
     *  @return none
     */
    void printAllUnderstocked() const
    {
        for( auto x : items ) {
            if( x.requireRestock() ) {
                std::cout << x;
            }
        }
    }

    /**
     *  Description: Print all Overstocked Items
     *  @param none
     *  @return none
     */
    void printAllOverstocked() const
    {
        for( auto x : items ) {
            if( x.isOverStocked() ) {
                std::cout << x;
            }
        }
    }

    /**
     *  Description: Remove Item Info from Inventory.
     *      No action is taken if Item does not exist
     *  @param string Item Name
     *  @return none
     */
    void removeItem( std::string itemToRemove )
    {
        std::vector<Item>::iterator itr = items.begin();

        for( itr ; itr != items.end() ; itr++ ) {
            if( itr->getItemName() == itemToRemove ) {
                items.erase( itr );
                return;
            }
        }
    }

    /**
     *  Description: Remove Item Info from Inventory.
     *      No action is taken if Item does not exist
     *  @param int Item Number
     *  @return none
     */
    void removeItem( int itemToRemove )
    {
        std::vector<Item>::iterator itr = items.begin();

        for( itr ; itr != items.end() ; itr++ ) {
            if( itr->getItemNumber() == itemToRemove ) {
                items.erase( itr );
                return;
            }
        }
    }

    /**
     *  Description: Modify an Existing Item.
     *      Must provide an updated Item Object
     *  @param Item Updated Item Object. The name or item number
     *      has to match one of the existing item.
     *  @return true if the item was successfully modified.
     */
    bool modifyItemInfo( const Item &newItem )
    {
        for( int i = 0 ; i < items.size() ; i++ ) {
            if( items[i].getItemName() == newItem.getItemName()
                || items[i].getItemNumber() == newItem.getItemNumber() )
            {
                items[i] = newItem;
                return true;
            }
        }

        return false;
    }

    /**
     *  Description: Function to print all items in the inventory
     *      Will be used by overloaded operator<<
     *  @param std::ostream &out
     *  @return none
     */
    void printItems( std::ostream &out ) const
    {
        for( auto x : items ) {
            out << x;
        }
    }

private:
    std::vector<Item> items;
};

/**
 *  Overloaded operator<< to print all items in the inventory
 */
std::ostream & operator<< ( std::ostream &out , const Inventory &inventory )
{
    inventory.printItems( out );
    return out;
}

int main()
{
    Inventory storeInventory;

    // Add some items to the inventory
    // Parameter List - Name , Item Number , Quantity , Price
    storeInventory.addItem( Item{ "Pencil" , 101 , 10 , 1.00} );
    storeInventory.addItem( Item{ "Eraser" , 102 , 21 , 2.00} );
    storeInventory.addItem( Item{ "Toothpaste" , 103 , 10 , 1.00} );
    storeInventory.addItem( Item{ "Toothbrush" , 104 , 50 , 1.00} );

    // Print all items in the inventory after adding items
    std::cout << "\n***** This is the initial inventory *****" << storeInventory;

    // Remove the item with 104 Item Number
    storeInventory.removeItem( 104 );

    // Remove the item with the name, "Pencil"
    storeInventory.removeItem( "Pencil" );

    // Change the item name, quantity, and price of an item w/ 102 item number
    storeInventory.modifyItemInfo( Item{ "Pencil Eraser" , 102 , 50 , 0.25} );

    // Change the item number, quantity, and price of an item w/ Toothpaste item name
    storeInventory.modifyItemInfo( Item{ "Toothpaste" , 104 , 8 , 1.25} );

    // Print Updated Inventory
    std::cout << "\n\n***** This is the updated Inventory *****" << storeInventory;

    // Print all overstocked items
    std::cout << "\n\nHere are overstocked items";
    storeInventory.printAllOverstocked();

    // Print all understocked items
    std::cout << "\n\nHere are items that need to be restocked";
    storeInventory.printAllUnderstocked();

    // Search an item. If the item exists, print its information
    std::cout << "\n\n\nSearching for an item named Toothpaste ...";
    if( storeInventory.searchItemByName("Toothpaste") ) {
        std::cout << "\nItem was found, printing the item information...\n";
        storeInventory.printItemByName( "Toothpaste" );
    }
    else {
        std::cout << "\n\nThe Item was not found";
    }
}
Last edited on
Program Output

***** This is the initial inventory *****
------------------------
Name: Pencil
Item#: 101
Quantity: 10
Cost: 1.00
Total Cost: 10.00
Requires Restock: No
Overstocked: No

------------------------
Name: Eraser
Item#: 102
Quantity: 21
Cost: 2.00
Total Cost: 42.00
Requires Restock: No
Overstocked: Yes

------------------------
Name: Toothpaste
Item#: 103
Quantity: 10
Cost: 1.00
Total Cost: 10.00
Requires Restock: No
Overstocked: No

------------------------
Name: Toothbrush
Item#: 104
Quantity: 50
Cost: 1.00
Total Cost: 50.00
Requires Restock: No
Overstocked: Yes


***** This is the updated Inventory *****
------------------------
Name: Pencil Eraser
Item#: 102
Quantity: 50
Cost: 0.25
Total Cost: 12.50
Requires Restock: No
Overstocked: Yes

------------------------
Name: Toothpaste
Item#: 104
Quantity: 8
Cost: 1.25
Total Cost: 10.00
Requires Restock: Yes
Overstocked: No


Here are overstocked items
------------------------
Name: Pencil Eraser
Item#: 102
Quantity: 50
Cost: 0.25
Total Cost: 12.50
Requires Restock: No
Overstocked: Yes


Here are items that need to be restocked
------------------------
Name: Toothpaste
Item#: 104
Quantity: 8
Cost: 1.25
Total Cost: 10.00
Requires Restock: Yes
Overstocked: No



Searching for an item named Toothpaste ...
Item was found, printing the item information...

------------------------
Name: Toothpaste
Item#: 104
Quantity: 8
Cost: 1.25
Total Cost: 10.00
Requires Restock: Yes
Overstocked: No

Process returned 0 (0x0) execution time : 0.131 s
Press any key to continue.


I really hope this helped. If you have any questions about the design or implementation. Feel free to ask.

Once you understand these two classes, you can edit the code and add extra features to make the program more useful.
Last edited on
Topic archived. No new replies allowed.