Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that will allow a user to manage the inventory of a small st

ID: 3586003 • Letter: W

Question

Write a C++ program that will allow a user to manage the inventory of a small store that sells various products of any type. The inventory for the store will contain the following information for each product: product name (i.e. “AppleiPhone3GS”, it will NOT contain spaces in it) sku (stock keeping unit code, an integer) quantity (how many of this product in stock) price (in dollars and cents) Note: Your program should be able to store up to 100 different products. You may assume that the skus will be unique (you do not need to check for this). The program should first read the inventory from a text file named “inventory.dat”. This file will contain data for each product in the inventory in this order: product name, sku, quantity, price. It will contain up to 100 products. See the sample file (inventory.dat) on the Tracs website. Then, it should offer the user a menu with the following options: 1. Display the inventory sorted by sku. 2. Lookup a product by sku. 3. Lookup a product by name. 4. Quit The program should perform the selected operation and then re-display the menu. Do not change the menu numbers associated with the operations. For the Display operation, display the information for each product on a separate line. The values should line up in columns (use setw). Headers for the table are optional. For the Lookup operations, label the output values (i.e. Name: AppleiPhone3GS, etc.). If the product is not found, display an appropriate message. 2 Additional Requirements: This program must be done in a Linux or Unix environment, using a command line compiler like g++. Do not use codeblocks, eclipse, or Xcode to compile. Your program must compile and run, otherwise you will receive a score of 0. The program must be modular (use top-down design), with significant work done by functions. Each function should perform a single, well-defined task. Use a partially filled array of structures to store the inventory: Use a counter variable to count the number of products that are read in from the file, and use this value as the size of the array for the search and sort functions. Your program should work for an input file with any number of products up to 100, but if you are struggling to make this work, you may assume it will have exactly 7 (for partial credit). You MUST use binary search for Lookup by sku. You may use (and modify) the code from the book. See the “Homework Resources” under Resources tool in TRACS. Please look at this code before you start implementing your program. I will put a sample input file on the Tracs website (inventory.dat) and the console output from running my solution on that file (output2.txt).

Explanation / Answer

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string.h>
#include<stdlib.h>
using namespace std;

//Read data from file and returns number of records
int readFile(string name[], int code[], int qty[], double price[])
{
//Creates an object of ifstream
ifstream readf;
//Opens the file for reading
readf.open ("inventory.txt");
//To store number of records
int c = 0;
//Loops till end of file
while(!readf.eof())
{
//Reads data and stores in respective array
readf>>name[c];
readf>>code[c];
readf>>qty[c];
readf>>price[c];
//Increase the record counter
c++;
}//End of while
//Close file
readf.close();
//Returns record counter
return c;
}//End of function

//Function to display complete inventory information
void displayInventory(string name[], int code[], int qty[], double price[], int len)
{
//Displays the length
for(int x = 0; x < len; x++)
{
//Displays the product information
cout<<setw(10)<<"Product name: "<<setw(20)<<std::left<<name[x]<<setw(6)<<"SKU: "<<setw(8)<<code[x]<<setw(12)<<"Quantity: "<<setw(6)<<qty[x]<<setw(10)<<"Price: "<<setw(10)<<price[x]<<endl;
}//End of for loop
}//End of function

//Function to display menu and return user choice
int menu()
{
int choice;
//Display menu
cout<<" 1. Display the inventory sorted by sku.";
cout<<" 2. Lookup a product by sku.";
cout<<" 3. Lookup a product by name.";
cout<<" 4. Quit ";
//Accept user choice
cout<<" Enter your choice: ";
cin>>choice;
//Return user choice
return choice;
}//End of function

//Function to sort SKU (product code) in ascending order
void sortedBySKU(string name[], int code[], int qty[], double price[], int len)
{
//Temporary variable to store sku
int tC;
//Counter for row and column
int r, c;
//Temporary variable to store name
string tN;
//Temporary variable to store price
double tP;
//Loops till length minus one position
for(r = 0; r < len; r++)
{
//Loops till length minus upper counter r value minus one
for(c = 0; c < len - r - 1; c++)
{
//Compares current position code value with the next position code value if it is greater
if(code[c] > code[c + 1])
{
//Swap sku
tC = code[c];
code[c] = code[c + 1];
code[c + 1] = tC;

//Swap name
tN = name[c];
name[c] = name[c + 1];
name[c + 1] = tN;

//Swap quantity
tC = qty[c];
qty[c] = qty[c + 1];
qty[c + 1] = tC;

//Swap price
tP = price[c];
price[c] = price[c + 1];
price[c + 1] = tP;

}//End of if
}//End of inner for loop
}//End of outer for loop
}//End of function

//Function to sort name (product name) in ascending order
void sortedByName(string name[], int code[], int qty[], double price[], int len)
{

//Temporary variable to store sku
int tC;
//Counter for row and column
int r, c;
//Temporary variable to store name
string tN;
//Temporary variable to store price
double tP;
//Loops till length minus one position
for(r = 0; r < len; r++)
{
//Loops till length minus upper counter r value minus one
for(c = 0; c < len - r - 1; c++)
{
//Compares current position name with the next position name if it is greater
if(name[c].compare(name[c+1]) > 0)
{
//Swap sku
tC = code[c];
code[c] = code[c + 1];
code[c + 1] = tC;

//Swap name
tN = name[c];
name[c] = name[c + 1];
name[c + 1] = tN;

//Swap quantity
tC = qty[c];
qty[c] = qty[c + 1];
qty[c + 1] = tC;

//Swap price
tP = price[c];
price[c] = price[c + 1];
price[c + 1] = tP;

}//End of if
}//End of inner for loop
}//End of outer for loop
}//End of function

//Function to search a sku using binary search
void searchBySKU(string name[], int code[], int qty[], double price[], int len, int sku)
{
//Variable to store beginning, end, mid position
int Beg, End, Mid;
int Flag;
//Beginning and flag is set to zero
Beg = Flag = 0;
//End position is set to length minus one
End = len - 1;
//Calls the function to sort by sku in ascending order
sortedBySKU(name, code, qty, price, len);
//Loops till beginning is less than or equal to end position
while(Beg <= End)
{
//Calculates the mid position
Mid = (Beg + End) / 2;
//Checks if the sku value is equal to code mid position value
if(sku == code[Mid])
{
//Display the product information
cout<<setw(10)<<"Product name: "<<setw(20)<<std::left<<name[Mid]<<setw(6)<<"SKU: "<<setw(8)<<code[Mid];
cout<<setw(12)<<"Quantity: "<<setw(6)<<qty[Mid]<<setw(10)<<"Price: "<<setw(10)<<price[Mid]<<endl;
//Set the flag to one
Flag = 1;
//Come out of the loop
break;
}//End of if
//Checks if sku value is less than the code mid position value
else if(sku < code[Mid])
//Set the end position to mix minus one position
End = Mid - 1;
//Otherwise
else
//Set the beginning position to mid plus one position
Beg = Mid + 1;
}//End of while loop
//Checks if the flag value is zero item not found
if(Flag == 0)
cout<<" Item code "<<sku<<" not available ";
}//End of function

//Function to search product name using binary search
void searchByName(string name[], int code[], int qty[], double price[], int len, string nameS)
{
//Variable to store beginning, end, mid position
int Beg, End, Mid;
int Flag;
//Beginning and flag is set to zero
Beg = Flag = 0;
//End position is set to length minus one
End = len - 1;
//Calls the function to sort by product name in ascending order
sortedByName(name, code, qty, price, len);
//Loops till beginning is less than or equal to end position
while(Beg <= End)
{
//Calculates the mid position
Mid = (Beg + End) / 2;
//Checks if the entered name is equal to code mid position name
if(nameS == name[Mid])
{
//Display the product information
cout<<setw(10)<<"Product name: "<<setw(20)<<std::left<<name[Mid]<<setw(6)<<"SKU: "<<setw(8)<<code[Mid];
cout<<setw(12)<<"Quantity: "<<setw(6)<<qty[Mid]<<setw(10)<<"Price: "<<setw(10)<<price[Mid]<<endl;
//Set the flag to one
Flag = 1;
//Come out of the loop
break;
}//End of if
//Checks if entered product name is less than the code mid position value
else if(nameS < name[Mid])
//Set the end position to mix minus one position
End = Mid - 1;
//Otherwise
else
//Set the beginning position to mid plus one position
Beg = Mid + 1;
}//End of while loop
//Checks if the flag value is zero item not found
if(Flag == 0)
cout<<" Product "<<nameS<<" not available ";
}//End of function

//Main function
int main()
{
//To store product name
string name[100];
//To store sku and quanrity
int code[100], quantity[100];
//To store price
double price[100];
//To store number of record in file
int len;
//To store user choice
int choice;
//To store sku entered by the user to search
int sku;
//To store product name entered by the user to search
string nameS;
//Call function to read file
len = readFile(name, code, quantity, price);
//Loops till user choice is not 4
do
{
//Calls the function menu to display menu and store the return choice
choice = menu();
//Switch begins
switch(choice)
{
case 1:
sortedBySKU(name, code, quantity, price, len);
displayInventory(name, code, quantity, price, len);
break;
case 2:
cout<<" Enter stock keeping unit code: ";
cin>>sku;
searchBySKU(name, code, quantity, price, len, sku);
break;
case 3:
cout<<" Enter Product Name: ";
cin>>nameS;
searchByName(name, code, quantity, price, len, nameS);
break;
case 4:
exit(0);
default:
cout<<" Invalid Choice!";
}//End of switch - case
}while(1); //End of do - while
}//End of main

Sample Run:


1. Display the inventory sorted by sku.
2. Lookup a product by sku.
3. Lookup a product by name.
4. Quit
Enter your choice: 1
Product name: MotoG SKU: 1002 Quantity: 20 Price: 5000
Product name: AppleiPhone SKU: 1003 Quantity: 15 Price: 8000
Product name: Linovo SKU: 1005 Quantity: 40 Price: 2500
Product name: Motorola SKU: 1009 Quantity: 15 Price: 1000

1. Display the inventory sorted by sku.
2. Lookup a product by sku.
3. Lookup a product by name.
4. Quit
Enter your choice: 2

Enter stock keeping unit code: 1005
Product name: Linovo SKU: 1005 Quantity: 40 Price: 2500

1. Display the inventory sorted by sku.
2. Lookup a product by sku.
3. Lookup a product by name.
4. Quit
Enter your choice: 2

Enter stock keeping unit code: 1008

Item code 1008 not available
1. Display the inventory sorted by sku.
2. Lookup a product by sku.
3. Lookup a product by name.
4. Quit
Enter your choice: 9

Invalid Choice!
1. Display the inventory sorted by sku.
2. Lookup a product by sku.
3. Lookup a product by name.
4. Quit
Enter your choice: 3

Enter Product Name: Linovo
Product name: Linovo SKU: 1005 Quantity: 40 Price: 2500

1. Display the inventory sorted by sku.
2. Lookup a product by sku.
3. Lookup a product by name.
4. Quit
Enter your choice: 3

Enter Product Name: Samsung

Product Samsung not available
1. Display the inventory sorted by sku.
2. Lookup a product by sku.
3. Lookup a product by name.
4. Quit
Enter your choice: 4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote