Write a complete C++ program as described below. Documentation is required; see section.
Starter code and input files are given to you. Please go through them on Titanium before beginning to program.
Each item that is scanned at a cash register reduces the grocery store’s inventory – i.e., the number of items decreases by one. Your program should code this logic.
A text file contains the initial inventory (i.e., number of items) of every type of item carried in the store in the following format:
23043 Apple 6
74001 Coke 12
10078 Eggs_6ct 10
10079 Eggs_12ct 6
34302 Milk_1qt 5
59004 Bread_wheat 5
Column 1 is the item code, column 2 is the item name, and column 3 is the number of items.
The list of scanned items is represented by an array of item codes (integers). For example, if the array is: [34302, 10078, 34302], the inventory after scanning should be printed as:
Apple 6
Coke 12
Eggs_6ct 9
Eggs_12ct 6
Milk_1qt 3
Bread_wheat 5
Implement your logic within a class GroceryInventory. The class should have the following public member functions:
These public member functions will be called from the provided main program and the answers printed there (see file part2.cpp)
You are free to add other (public/private) member variables and functions to class GroceryInventory as needed.
Note: part2.cpp is not provided in the question
Code for the above program is provided below. If any double, please comment below.
GroceryInventory.h
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
// Declaration of class
class GroceryInventory
{
int itemCode[10];
string itemName[10];
int numOfItems[10];
int count;
public:
// Read data from file
GroceryInventory(string filename)
{
// Open the file
ifstream in;
in.open(filename);
int i=0;
// Read values to array
while(in>>itemCode[i])
{
in>>itemName[i];
in>>numOfItems[i];
i++;
}
// Total items
count=i;
}
// Print the values
int printInventory(int itemcodes[], int nItems)
{
// Parse through the list of
scanned items
for(int j=0;j<nItems;j++)
{
// Check the
itemcode
for(int
i=0;i<count;i++)
{
// Decrement scanned item count
if(itemcodes[j]==itemCode[i])
numOfItems[i]--;
}
}
int tot=0;
// Print each item
for(int i=0;i<count;i++)
{
cout<<itemName[i]<<"
"<<numOfItems[i]<<endl;
// Find total
items
tot=tot+numOfItems[i];
}
return tot;
}
};
part2.cpp
#include<iostream>
#include"GroceryInventory.h"
using namespace std;
int main()
{
GroceryInventory gr("inp.txt");
// Array of scanned items
int arr[3]={34302,10078,34302};
int count=gr.printInventory(arr,3);
// Print the total
cout<<"Total items =
"<<count<<endl;
system("pause");
}
Output:
Get Answers For Free
Most questions answered within 1 hours.