This project involves designing and creating a C++ program that will utilize the InventoryItem class. (The InventoryItem.h source code for this class is provided) Your program must not modify the class specification for the InventoryItem class.

The program must be organized as a "Command Loop" program. The program must implement the following interactive commands:

a Add parts: increase the units value for an existing inventory item.
h print Help text.
i Input inventory data from a file.
n create a New inventory Item.
o Output inventory data to a file.
p Print inventory list.
q Quit (end the program).
r Remove parts: reduce the units value for an existing inventory item.

(Specific requirements for each command are stated in the Requirements for Interactive Commands section of this document.)

The program must create an array of 100 InventoryItem objects (or a vector of InventoryItem objects).

Requirements for Interactive Commands

Command i

Input Inventory Data from File:

  • Output a prompt, asking the user to specify the name of the input file.
  • Read the data from the file into the InventoryItem array (or vector). Refer to the Data File Format section of this document.

Command n

Create a New Inventory Item:

  • Input (from the keyboard) values for the description, unit cost and initial quantity (units) for a new InventoryItem.
  • Be sure to use suitable prompts, so the user knows what input is expected.

Command o

Output Inventory Data to File:

  • Output a prompt, asking the user to specify the name of the output file.
  • Write the data from the InventoryItem array (or vector) to the output file, following the required file format.

Command p

Print Inventory Data to Screen:

Output the contents of the InventoryItem array (or vector) to the screen. (Refer to the Sample Output section of this document for formatting examples.)

Command q

Quit (exit) the Program

Command r

Remove Parts from Existing Inventory Item:

  • Output a prompt, asking the user to specify the desired item number.
    • If the user specifies an item number that is not in use (no item present in the data), output an error message.
  • Output a prompt, asking the user to specify how many units to remove.
  • If the input specified by the user is negative, or if the input specified by the user is greater than the units variable of the InventoryItem object, output an error message.

Data File Format

The "input" / output commands read / write data that is in a pipe-delimited text file.

The format of each line of text, in the data file, is described below:

inventory item number | description | cost | units

Explanation of Data Fields

  • inventory item number
    • For the output file, this number can be the same as the array (or vector) index.
    • For the input file, the contents of this field will be ignored, because the input data will be appended to the end of the "populated" portion of the InventoryItem array (or vector).
  • description
    • Description of the inventory item
  • cost
    • Cost per unit for the inventory item
  • units
    • Number of units present for the inventory item (must be greater than or equal to zero and less than or equal to 30).

When reading the data file, your program needs to read one line of text from the file at a time, break each line of text into separate fields, and convert each field to the correct data type.

  • Remember that the code to process the input file must ignore the first field of each line. There is no class variable in the InventoryItem class for inventory item number, and your program must not modify the InventoryItem class specification.

Important Design Requirements

  • When processing an input data file, be sure to remember that the first field of each line of data in the input file must be ignored.
  • The output file format must be the same as the input file format. That is, any file that your program creates with the "o" command must be readable by the i command of your program.
  • The units member variable of any InventoryItem object must never be negative and also must never be greater than the value of 30.

Sample Test Data

Four sample input files are provided: electrical.txt, fasteners.txt, miscellaneous.txt and plumbing.txt. The data files that your program creates must obey the same file format as these sample files. The program must work correctly with these files, as well as general files of similar format.

electrical.txt

0|Cable|5.00|18
1|Extension Cord (14/3, 25 ft)|27.95|6
2|Light switch (15 amp)|2.79|10
3|Ceiling Fan (52 inch)|79.95|3
4|Vinyl Electrical Tape (20 ft roll)|0.79|30
5|GFI Tester|9.35|5

fasteners.txt

0|Turnbuckle|3.80|25
1|Siding nails (box of 100)|4.00|20
2|Flat washer (box of 100)|2.80|30
3|Machine screw (box of 100)|3.20|10
4|Hex bolt (box of 100)|6.50|23
5|Hex nut (box of 100)|3.80|15
6|Sheet Metal Screw (qty 100)|1.50|28

miscellaneous.txt

0|Door Hinges (3-pack)|6.30|10
1|Rubber work boots (1 pair)|28.00|5
2|Leather Work Gloves (1 pair)|12.00|8
3|Long Handle Grass Shear|30.00|5

plumbing.txt

0|Pump|39.00|20
1|Gasket|1.50|29
2|Water Level Guage|12.99|30
3|Faucet Repair Kit|4.89|8
4|Teflon Thread Seal Tape (50 ft roll)|3.30|12
5|shutoff valve|6.50|10

Sample Interactive Session

In the sample data on the next several pages, what the user types is shown in bold. In actuality, what the user types would have the same text format as the rest of the output.

Command: h
Supported commands:
a Add parts.
h print Help text.
i Input inventory data from a file.
n New inventory Item.
o Output inventory data to a file.
p Print inventory list.
q quit (end the program).
r Remove parts.

Command: i
Enter name of input file: plumbing.txt
6 records loaded to array.
Command: p

Item Num Description                           Cost Quantity
-------- ----------- ---- --------
0 Pump 39.00 20
1 Gasket 1.50 29
2 Water Level Guage 12.99 30
3 Faucet Repair Kit 4.89 8
4 Teflon Thread Seal Tape (50 ft roll) 3.30 12
5 shutoff valve 6.50 10

6 records.
Command: i
Enter name of input file: electrical.txt
6 records loaded to array.
Command: p

Item Num Description                           Cost Quantity
-------- ----------- ---- --------
0 Pump 39.00 20
1 Gasket 1.50 29
2 Water Level Guage 12.99 30
3 Faucet Repair Kit 4.89 8
4 Teflon Thread Seal Tape (50 ft roll) 3.30 12
5 shutoff valve 6.50 10
6 Cable 5.00 18
7 Extension Cord (14/3, 25 ft) 27.95 6
8 Light switch (15 amp) 2.79 10
9 Ceiling Fan (52 inch) 79.95 3
10 Vinyl Electrical Tape (20 ft roll) 0.79 30
11 GFI Tester 9.35 5

12 records.
command: a

Choose a Item Number: 7
How many parts to add? 5
Command: p

Item Num Description                           Cost Quantity
-------- ----------- ---- --------
0 Pump 39.00 20
1 Gasket 1.50 29
2 Water Level Guage 12.99 30
3 Faucet Repair Kit 4.89 8
4 Teflon Thread Seal Tape (50 ft roll) 3.30 12
5 shutoff valve 6.50 10
6 Cable 5.00 18
7 Extension Cord (14/3, 25 ft) 27.95 11
8 Light switch (15 amp) 2.79 10
9 Ceiling Fan (52 inch) 79.95 3
10 Vinyl Electrical Tape (20 ft roll) 0.79 30
11 GFI Tester 9.35 5

12 records.
Command: r

Choose a Item Number: 9
How many parts to remove? 5

Error: You are attempting to remove more parts than the Item currently holds.
Command: r

Choose a Item Number: 9
How many parts to remove? 3
Command: p

Item Num Description                           Cost Quantity
-------- ----------- ---- --------
0 Pump 39.00 20
1 Gasket 1.50 29
2 Water Level Guage 12.99 30
3 Faucet Repair Kit 4.89 8
4 Teflon Thread Seal Tape (50 ft roll) 3.30 12
5 shutoff valve 6.50 10
6 Cable 5.00 18
7 Extension Cord (14/3, 25 ft) 27.95 11
8 Light switch (15 amp) 2.79 10
9 Ceiling Fan (52 inch) 79.95 0
10 Vinyl Electrical Tape (20 ft roll) 0.79 30
11 GFI Tester 9.35 5

12 records.

Command: o
Enter name of output file: testData01.txt
12 records written to file.
Command: i
Enter name of input file: testData01.txt
12 records loaded to array.
Command: p

Item Num Description                           Cost Quantity
-------- ----------- ---- --------
0 Pump 39.00 20
1 Gasket 1.50 29
2 Water Level Guage 12.99 30
3 Faucet Repair Kit 4.89 8
4 Teflon Thread Seal Tape (50 ft roll) 3.30 12
5 shutoff valve 6.50 10
6 Cable 5.00 18
7 Extension Cord (14/3, 25 ft) 27.95 11
8 Light switch (15 amp) 2.79 10
9 Ceiling Fan (52 inch) 79.95 0
10 Vinyl Electrical Tape (20 ft roll) 0.79 30
11 GFI Tester 9.35 5
12 Pump 39.00 20
13 Gasket 1.50 29
14 Water Level Guage 12.99 30
15 Faucet Repair Kit 4.89 8
16 Teflon Thread Seal Tape (50 ft roll) 3.30 12
17 shutoff valve 6.50 10
18 Cable 5.00 18
19 Extension Cord (14/3, 25 ft) 27.95 11
20 Light switch (15 amp) 2.79 10
21 Ceiling Fan (52 inch) 79.95 0
22 Vinyl Electrical Tape (20 ft roll) 0.79 30
23 GFI Tester 9.35 5

24 records.
Command: n
Enter description for new Item: Broom
Enter unit cost for new Item: 9.99
Enter initial quantity for the new Item: 12
Announcing a new inventory Item: Broom
We now have 25 different inventory Items in stock!
Command: p

Item Num Description                           Cost Quantity
-------- ----------- ---- --------
0 Pump 39.00 20
1 Gasket 1.50 29
2 Water Level Guage 12.99 30
3 Faucet Repair Kit 4.89 8
4 Teflon Thread Seal Tape (50 ft roll) 3.30 12
5 shutoff valve 6.50 10
6 Cable 5.00 18
7 Extension Cord (14/3, 25 ft) 27.95 11
8 Light switch (15 amp) 2.79 10
9 Ceiling Fan (52 inch) 79.95 0
10 Vinyl Electrical Tape (20 ft roll) 0.79 30
11 GFI Tester 9.35 5
12 Pump 39.00 20
13 Gasket 1.50 29
14 Water Level Guage 12.99 30
15 Faucet Repair Kit 4.89 8
16 Teflon Thread Seal Tape (50 ft roll) 3.30 12
17 shutoff valve 6.50 10
18 Cable 5.00 18
19 Extension Cord (14/3, 25 ft) 27.95 11
20 Light switch (15 amp) 2.79 10
21 Ceiling Fan (52 inch) 79.95 0
22 Vinyl Electrical Tape (20 ft roll) 0.79 30
23 GFI Tester 9.35 5
24 Broom 9.99 12

25 records.
Command: n
Enter description for new Item: Dust Pan
Enter unit cost for new Item: 5.99
Enter initial quantity for the new Item: 5
Announcing a new inventory Item: Dust Pan
We now have 26 different inventory Items in stock!
Command: p

Item Num Description                           Cost Quantity
-------- ----------- ---- --------
0 Pump 39.00 20
1 Gasket 1.50 29
2 Water Level Guage 12.99 30
3 Faucet Repair Kit 4.89 8
4 Teflon Thread Seal Tape (50 ft roll) 3.30 12
5 shutoff valve 6.50 10
6 Cable 5.00 18
7 Extension Cord (14/3, 25 ft) 27.95 11
8 Light switch (15 amp) 2.79 10
9 Ceiling Fan (52 inch) 79.95 0
10 Vinyl Electrical Tape (20 ft roll) 0.79 30
11 GFI Tester 9.35 5
12 Pump 39.00 20
13 Gasket 1.50 29
14 Water Level Guage 12.99 30
15 Faucet Repair Kit 4.89 8
16 Teflon Thread Seal Tape (50 ft roll) 3.30 12
17 shutoff valve 6.50 10
18 Cable 5.00 18
19 Extension Cord (14/3, 25 ft) 27.95 11
20 Light switch (15 amp) 2.79 10
21 Ceiling Fan (52 inch) 79.95 0
22 Vinyl Electrical Tape (20 ft roll) 0.79 30
23 GFI Tester 9.35 5
24 Broom 9.99 12
25 Dust Pan 5.99 5

26 records.
Command: o
Enter name of output file: testData02.txt
26 records written to file.
Command: n
Enter description for new Item: Gasoline Can
Enter unit cost for new Item: 8.99
Enter initial quantity for the new Item: 34
ERROR: initial quantity must be >= zero and <= 30.
Enter initial quantity for the new Item: 29
Announcing a new inventory Item: Gasoline Can
We now have 27 different inventory Items in stock!
Command: p

Item Num Description                           Cost Quantity
-------- ----------- ---- --------
0 Pump 39.00 20
1 Gasket 1.50 29
2 Water Level Guage 12.99 30
3 Faucet Repair Kit 4.89 8
4 Teflon Thread Seal Tape (50 ft roll) 3.30 12
5 shutoff valve 6.50 10
6 Cable 5.00 18
7 Extension Cord (14/3, 25 ft) 27.95 11
8 Light switch (15 amp) 2.79 10
9 Ceiling Fan (52 inch) 79.95 0
10 Vinyl Electrical Tape (20 ft roll) 0.79 30
11 GFI Tester 9.35 5
12 Pump 39.00 20
13 Gasket 1.50 29
14 Water Level Guage 12.99 30
15 Faucet Repair Kit 4.89 8
16 Teflon Thread Seal Tape (50 ft roll) 3.30 12
17 shutoff valve 6.50 10
18 Cable 5.00 18
19 Extension Cord (14/3, 25 ft) 27.95 11
20 Light switch (15 amp) 2.79 10
21 Ceiling Fan (52 inch) 79.95 0
22 Vinyl Electrical Tape (20 ft roll) 0.79 30
23 GFI Tester 9.35 5
24 Broom 9.99 12
25 Dust Pan 5.99 5
26 Gasoline Can 8.99 29

27 records.
Command: i
Enter name of input file: fasteners.txt
7 records loaded to array.
Command: i
Enter name of input file: miscellaneous.txt
4 records loaded to array.
Command: p

Item Num Description                           Cost Quantity
-------- ----------- ---- --------
0 Pump 39.00 20
1 Gasket 1.50 29
2 Water Level Guage 12.99 30
3 Faucet Repair Kit 4.89 8
4 Teflon Thread Seal Tape (50 ft roll) 3.30 12
5 shutoff valve 6.50 10
6 Cable 5.00 18
7 Extension Cord (14/3, 25 ft) 27.95 11
8 Light switch (15 amp) 2.79 10
9 Ceiling Fan (52 inch) 79.95 0
10 Vinyl Electrical Tape (20 ft roll) 0.79 30
11 GFI Tester 9.35 5
12 Pump 39.00 20
13 Gasket 1.50 29
14 Water Level Guage 12.99 30
15 Faucet Repair Kit 4.89 8
16 Teflon Thread Seal Tape (50 ft roll) 3.30 12
17 shutoff valve 6.50 10
18 Cable 5.00 18
19 Extension Cord (14/3, 25 ft) 27.95 11
20 Light switch (15 amp) 2.79 10
21 Ceiling Fan (52 inch) 79.95 0
22 Vinyl Electrical Tape (20 ft roll) 0.79 30
23 GFI Tester 9.35 5
24 Broom 9.99 12
25 Dust Pan 5.99 5
26 Gasoline Can 8.99 29
27 Turnbuckle 3.80 25
28 Siding nails (box of 100) 4.00 20
29 Flat washer (box of 100) 2.80 30
30 Machine screw (box of 100) 3.20 10
31 Hex bolt (box of 100) 6.50 23
32 Hex nut (box of 100) 3.80 15
33 Sheet Metal Screw (qty 100) 1.50 28
34 Door Hinges (3-pack) 6.30 10
35 Rubber work boots (1 pair) 28.00 5
36 Leather Work Gloves (1 pair) 12.00 8
37 Long Handle Grass Shear 30.00 5

38 records.
Command: o
Enter name of output file: testData03.txt
38 records written to file.
Command: q
Exit.

InventoryItem.h

// This class has overloaded constructors.
#ifndef INVENTORYITEM_H
#define INVENTORYITEM_H
#include < string >
using namespace std;

class InventoryItem
{
private:
string description; // The item description
double cost; // The item cost
int units; // Number of units on hand
public:
// Constructor #1
InventoryItem()
{ // Initialize description, cost, and units.
description = "";
cost = 0.0;
units = 0; }

// Constructor #2
InventoryItem(string desc)
{ // Assign the value to description.
description = desc;

// Initialize cost and units.
cost = 0.0;
units = 0; }

// Constructor #3
InventoryItem(string desc, double c, int u)
{ // Assign values to description, cost, and units.
description = desc;
cost = c;
units = u; }

// Mutator functions
void setDescription(string d)
{ description = d; }

void setCost(double c)
{ cost = c; }

void setUnits(int u)
{ units = u; }

// Accessor functions
string getDescription() const
{ return description; }

double getCost() const
{ return cost; }

int getUnits() const
{ return units; }
};
#endif
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.