For C++. Write a program to keep track of a sports store inventory. The store se
ID: 3717785 • Letter: F
Question
For C++.
Write a program to keep track of a sports store inventory. The store sells various items. For each item in the store, the following information is kept, item ID, item name, number of pieces ordered, number of pieces currently in the store, number of pieces sold, manufacturer's price for the item and the store's selling price. At the end of each week, the store manage would like to see a report in the following format: Dakine Sports Store Report itemName pOrdered plnstore Basketba 100 Frisbee pSold 20 10 manufPrice sellingPrice 10.00 5.00 ItemID 100 75 30.00 15.00 75 Total Inventory: Total number of items in the Store: The total inventory is the total selling value of al of the items currently in the store. (for what is listed in the report above the value would be 3375.00(80 *30.0065 15.00) The total number of items is the sum of the number of pieces of all of the items in the store. for what is listed in the report above the value would be 145(80+65) Your program must be menu driven (see sample menu on page 2), giving the user various choices such as, Checking whether an item is in the store Selling an item in the store, Printing the report. After inputting the data, sort it according to the items' names. Also after an item is sold, update the appropriate counts. Initially, the number of pieces (of an item) in the store is the same as the number of pieces ordered, and the number of pieces of an item sold is zero. Input to the program is a file consisting of data in the following form (see example of text file on page 3) itemName pOrdered ma nuPrice sellingPrice Use seven parallel vectors to store the information. The program must contain at least the following functions: one to input the data into the vectors, one to display the menu, one to sell an item, and one to print the report for the managerExplanation / Answer
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string.h>
#include<stdlib.h>
#define MAX 100
using namespace std;
// Class Inventory definition
class Inventory
{
// Data member to store data
string ItemID;
string ItemName;
int PiecesOrdered;
int PicesStore;
int PicesSold;
double manufPrice;
double sellingPrice;
double totalInventory;
int totalItemInStore;
public:
// Member function prototype
Inventory();
int readFile(Inventory[]);
void displayReport(Inventory[], int);
void sortItem(Inventory[], int);
int checkItem(Inventory[], int);
void sellItem(Inventory[], int);
void calculate(Inventory[], int);
};// End of class
// Function to calculate total inventory and total items in stock
void Inventory::calculate(Inventory inv[], int len)
{
// Loops till number of records
for(int x = 0; x < len; x++)
{
// Calculates total inventory by multiplying pieces in store with selling price of each item
inv[0].totalInventory += (inv[x].PicesStore * inv[x].sellingPrice);
// Calculates total item by adding each items pieces in store
inv[0].totalItemInStore += inv[x].PicesStore;
}// End of for loop
}// End of function
// Function to sell an item
void Inventory::sellItem(Inventory inv[], int len)
{
// To store found index position
int index;
// To store the purchase quantity entered by the user
int num;
// Calls the function to check whether item is available or not
// and stores the return index position
index = checkItem(inv, len);
// Checks if index is not - 1 item found
if(index != -1)
{
// Accept number of item to buy
cout<<" Enter how many item of "<<inv[index].ItemName<<" you wants to purchase? ";
cin>>num;
// Checks if the found item quantity is less than the quantity entered by the user
// Displays error message
if(inv[index].PicesStore < num)
cout<<" Insufficient stock. Pieces in stock "<<inv[index].PicesStore;
// Otherwise valid quantity
else
{
// Adds the quantity to pieces sold for the found item
inv[index].PicesSold += num;
// Subtracts the quantity from pieces in store for the found item
inv[index].PicesStore -= num;
// Calls the calculate function to calculate total inventory and total items
calculate(inv, len);
}// End of else
}// End of if condition
// Otherwise item not found
else
cout<<" Item not found.";
}// End of function
// Function to search for an item and returns index position if found otherwise returns -1
int Inventory::checkItem(Inventory inv[], int len)
{
// Initial found status is -1 for not found
int found = -1;
// To store the name entered by the user to search
string name;
// Accepts the name
cout<<" Enter the Item Name: ";
cin>>name;
// Loops till number of records
for(int x = 0; x < len; x++)
{
// Checks if the current item name is equals to the item name entered by the user
if(inv[x].ItemName.compare(name) == 0)
{
// Set the found to x value as index position found
found = x;
break;
}// End of if condition
}// End of for loop
// Returns the found index position
return found;
}// End of function
// Function to sort items by name
void Inventory::sortItem(Inventory inv[], int len)
{
int x, y;
// Creates a temporary object for swapping
Inventory temp;
// Loops till length of the array
for(x = 0; x < len; x++)
{
//Loops till length of the array minus x minus one times
for(y = 0; y < len - x - 1; y++)
{
//Checks if current index position of the array is greater than the next index position of the array
if(inv[y].ItemName.compare(inv[y + 1].ItemName) > 0)
{
//Swapping process
temp = inv[y];
inv[y] = inv[y + 1];
inv[y + 1] = temp;
}//End of if condition
}//End of inner for loop
}//End of outer for loop
}// End of function
// Default constructor to initialize data members
Inventory::Inventory()
{
ItemID = ItemName = "";
PiecesOrdered = PicesStore = PicesSold = manufPrice = sellingPrice = totalInventory = totalItemInStore = 0;
}// End of constructor
// Read data from file, stores it in array of inv object and returns number of records
int Inventory::readFile(Inventory inv[])
{
// Creates an object of ifstream
ifstream readf;
// Opens the file for reading
readf.open ("ItemInfo.txt");
// Check that file can be opened or not
// is_open() function returns true if a file is open and associated with this stream object.
// Otherwise returns false.
if(!readf.is_open())
{
// Displays error message
cout<<" Error: Unable to open the file!";
// Exit the program
exit(0);
}// End of if condition
// To store number of records
int c = 0;
// Loops till not end of the file
// eof() function returns true if the stream's eofbit error state flag is set (which signals that the End-of-File has been reached
// by the last input operation). Otherwise returns false.
while(!readf.eof())
{
// Reads data from file and stores it in c index position's data members
readf>>inv[c].ItemID;
readf>>inv[c].ItemName;
readf>>inv[c].PiecesOrdered;
inv[c].PicesStore = inv[c].PiecesOrdered;
readf>>inv[c].manufPrice;
readf>>inv[c].sellingPrice;
//Increase the record counter
c++;
}//End of while
//Close file
readf.close();
//Returns record counter
return c;
}//End of function
// Function to display Inventory Report
void Inventory::displayReport(Inventory inv[], int len)
{
// Displays heading
cout<<" Dakine Sports Store Report"<<endl;
cout<<left<<setw(8)<<"Item ID"<<setw(15)<<"Item Name"<<setw(15)<<"P_Ordered";
cout<<setw(12)<<"In_Store"<<setw(15)<<"P_Sold"<<setw(20)<<"Manf_Price"<<setw(10)<<"Sell_Price"<<endl;
// Loops till number of records
for(int x = 0; x < len; x++)
{
// Displays data
cout<<left<<setw(8)<<inv[x].ItemID<<setw(15)<<inv[x].ItemName<<setw(15)<<inv[x].PiecesOrdered;
cout<<setw(12)<<inv[x].PicesStore<<setw(15)<<inv[x].PicesSold<<setw(20)<<inv[x].manufPrice<<setw(10)<<inv[x].sellingPrice<<endl;
}// End of fro loop
// Displays totals
cout<<" Total Inventory: $"<<inv[0].totalInventory;
cout<<" Total number of items in the store: "<<inv[0].totalItemInStore;
}// End of function
// Function to return user choice
int menu()
{
int ch;
// Displays menu
cout<<" 1 Check whether an item is in the store?";
cout<<" 2 Selling an item in the store.";
cout<<" 3 Printing the report.";
cout<<" 4 Exit";
// Accepts user choice
cout<<" Enter your choice: ";
cin>>ch;
// Returns user choice
return ch;
}// End of function
// main function definition
int main()
{
// Creates an array of inventory of size MAX
Inventory inventory[MAX];
// Calls the function to read data from file and returns number of records
int length = inventory[0].readFile(inventory);
// Calls the function to sort the items by name
inventory[0].sortItem(inventory, length);
// Calls the function to calculate
inventory[0].calculate(inventory, length);
// Loops till user choice is not 4
do
{
// Calls the function to accept user choice
// Based on the return value of the user choice calls the functions
switch(menu())
{
case 1:
if(inventory[0].checkItem(inventory, length) == -1)
cout<<" Item not found.";
else
cout<<" Item available.";
break;
case 2:
inventory[0].sellItem(inventory, length);
break;
case 3:
inventory[0].displayReport(inventory, length);
break;
case 4:
exit(0);
default:
cout<<" Invalid choice!";
}// End of switch case
}while(1); // End of do - while loop
}// End of main function
Sample Output:
1 Check whether an item is in the store?
2 Selling an item in the store.
3 Printing the report.
4 Exit
Enter your choice: 3
Dakine Sports Store Report
Item ID Item Name P_Ordered In_Store P_Sold Manf_Price Sell_Price
3333 Ball 92 92 0 25.22 3.8
4444 Bat 75 75 0 6.7 3.25
2222 Helmat 80 80 0 22.1 4.5
1111 Pad 100 100 0 12.2 2.5
Total Inventory: $1203.35
Total number of items in the store: 347
1 Check whether an item is in the store?
2 Selling an item in the store.
3 Printing the report.
4 Exit
Enter your choice: 1
Enter the Item Name: Bat
Item available.
1 Check whether an item is in the store?
2 Selling an item in the store.
3 Printing the report.
4 Exit
Enter your choice: 2
Enter the Item Name: Bat
Enter how many item of Bat you wants to purchase? 80
Insufficient stock.
Pieces in stock 75
1 Check whether an item is in the store?
2 Selling an item in the store.
3 Printing the report.
4 Exit
Enter your choice: 2
Enter the Item Name: Bat
Enter how many item of Bat you wants to purchase? 20
1 Check whether an item is in the store?
2 Selling an item in the store.
3 Printing the report.
4 Exit
Enter your choice: 3
Dakine Sports Store Report
Item ID Item Name P_Ordered In_Store P_Sold Manf_Price Sell_Price
3333 Ball 92 92 0 25.22 3.8
4444 Bat 75 55 20 6.7 3.25
2222 Helmat 80 80 0 22.1 4.5
1111 Pad 100 100 0 12.2 2.5
Total Inventory: $2341.7
Total number of items in the store: 674
1 Check whether an item is in the store?
2 Selling an item in the store.
3 Printing the report.
4 Exit
Enter your choice: 2
Enter the Item Name: Pad
Enter how many item of Pad you wants to purchase? 40
1 Check whether an item is in the store?
2 Selling an item in the store.
3 Printing the report.
4 Exit
Enter your choice: 3
Dakine Sports Store Report
Item ID Item Name P_Ordered In_Store P_Sold Manf_Price Sell_Price
3333 Ball 92 92 0 25.22 3.8
4444 Bat 75 55 20 6.7 3.25
2222 Helmat 80 80 0 22.1 4.5
1111 Pad 100 60 40 12.2 2.5
Total Inventory: $3380.05
Total number of items in the store: 961
1 Check whether an item is in the store?
2 Selling an item in the store.
3 Printing the report.
4 Exit
Enter your choice: 4
ItemInfo.txt file contents
1111
Pad
100 12.20 2.50
2222
Helmat
80 22.10 4.50
3333
Ball
92 25.22 3.80
4444
Bat
75 6.70 3.25
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.