Write a program to create a list of products by storing their information in the
ID: 3770337 • Letter: W
Question
Write a program to create a list of products by storing their information in the following arrays:
itemNum: an array of long integers to hold the product numbers.
itemName: an array of string to hold the product names.
quantity: an array of integers to hold each product's quantity.
unitPrice: an array of double to hold each product's unit price.
totalPrice: an array of double to hold each product's total price (unit price x quantity).
The program should relate the data in each array through the subscripts. For example, the number in element 0 of the itemName array should be the name of the product whose number is stored in element 0 of the itemNum array. Similarly with the other arrays. Create a constant for the array size to be used by all the arrays.
When the program is run, it first calls a function to display a menu as follows:
There are 6 functions:
showMenu: display the menu above.
addItem: when 1 is selected, this function asks the user to enter the product number, name, quantity, and unit price. They are stored in their respective arrays. Keep track of the total number of products entered (how many elements have values) to be used by the listItem function. After each input, ask the user if he/she wants to add more. If yes, make sure that the total number of inputs does not exceed the array size before proceeding to ask the user to enter again. If no, return to display the menu.
retrieveItem: when 2 is selected, this function asks the user to enter the product number. It searches for the number in the itemNum array. If found, display the number, name, quantity, unit price, and total price.
listItem: when 3 is selected, this function traverses the arrays and displays each product information (number, name, quantity, unit price, and total price). Make sure to display only the elements that have values. Do not use the whole array size if the arrays are not filled up. Make sure to format the output as shown below.
searchList: this function performs a linear search on an integer array. It is called by updateItem and retrieveItem functions.
Input Validation: Accept only value between 1 and 4 for the menu choice. The quantity must be positive. The unit price must be at least 0.10.
Do not use global variables. You can use a global constant if you want. That's all. Learn to pass by reference if necessary. Do not pass reference everything because you like it. Pass by reference only when the variale is modified. Do not use break, continue, exit and return from an if (the only return is from a function. So only one return and not twice or several in a function). Do not use pointers as we did not learn them. You can use vector if you want.
Explanation / Answer
Complete Program:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void showMenu();
void addItem(long int itemNum[], string itemName[], int quantity[], double unitPrice[], double totalPrice[], int n, int &count);
void retrieveItem(long int itemNum[], string itemName[], int quantity[], double unitPrice[], double totalPrice[], int count);
void listItem(long int itemNum[], string itemName[], int quantity[], double unitPrice[], double totalPrice[], int count);
int searchList(long int itemNum[], int count, long int prodNum);
void updateItem(long int itemNum[], string itemName[], int quantity[], double unitPrice[], double totalPrice[], int count);
int main()
{
const int SIZE = 10;
long int itemNum[SIZE];
string itemName[SIZE];
int quantity[SIZE];
double unitPrice[SIZE];
double totalPrice[SIZE];
int choice;
int count = 0;
do
{
showMenu();
cin >> choice;
while(choice < 1 || choice > 4)
{
showMenu();
cin >> choice;
}
if(choice == 1)
addItem(itemNum, itemName, quantity, unitPrice, totalPrice, SIZE, count);
else if(choice == 2)
retrieveItem(itemNum, itemName, quantity, unitPrice, totalPrice, count);
else if(choice == 3)
listItem(itemNum, itemName, quantity, unitPrice, totalPrice, count);
else
cout << "Thank you" << endl << endl;
}while(choice != 4);
system("pause");
return 0;
}
void showMenu()
{
cout << " List of Items" << endl;
cout << "1. Add Item" << endl;
cout << "2. Retrieve Item" << endl;
cout << "3. List all" << endl;
cout << "4. Quit the Program" << endl;
cout << "Enter your choice: ";
}
void addItem(long int itemNum[], string itemName[], int quantity[], double unitPrice[], double totalPrice[], int n, int &count)
{
if(count == n)
cout << "List is full" << endl;
else
{
char more = 'Y';
while(toupper(more) == 'Y' && count < n)
{
cout << " Enter the product number: ";
cin >> itemNum[count];
cout << "Enter the product name: ";
cin >> itemName[count];
cout << "Enter the product's quantity: ";
cin >> quantity[count];
cout << "Enter the product unit price: $ ";
cin >> unitPrice[count];
while(unitPrice[count] < 0.10)
{
cout << "Enter the product unit price: $ ";
cin >> unitPrice[count];
}
totalPrice[count] = quantity[count] * unitPrice[count];
count++;
cout << "Do you want to add more? (Y/N): ";
cin >> more;
if(count == n)
cout << "List is full" << endl;
}
}
}
void retrieveItem(long int itemNum[], string itemName[], int quantity[], double unitPrice[], double totalPrice[], int count)
{
if(count == 0)
cout << "List is empty" << endl;
else
{
long int prodNum;
cout << " Enter the product number: ";
cin >> prodNum;
int index = searchList(itemNum, count, prodNum);
if(index >= 0)
{
cout << "Product number: " << itemNum[index] << endl;
cout << "Product name: " << itemName[index] << endl;
cout << "Product's quantity: " << quantity[index] << endl;
cout << "Product unit price: $ " << setprecision(2) << fixed << unitPrice[index] << endl;
cout << "Product total price: $ " << setprecision(2) << fixed << totalPrice[index] << endl;
}
else
cout << prodNum << " is not found in the list" << endl;
}
}
void listItem(long int itemNum[], string itemName[], int quantity[], double unitPrice[], double totalPrice[], int count)
{
if(count == 0)
cout << "List is empty" << endl;
else
{
cout << " List of all items:" << endl;
for(int i = 0; i < count; i++)
{
cout << " Product number: " << itemNum[i] << endl;
cout << "Product name: " << itemName[i] << endl;
cout << "Product's quantity: " << quantity[i] << endl;
cout << "Product unit price: $ " << setprecision(2) << fixed << unitPrice[i] << endl;
cout << "Product total price: $ " << setprecision(2) << fixed << totalPrice[i] << endl;
}
}
}
int searchList(long int itemNum[], int count, long int prodNum)
{
for(int i = 0; i < count; i++)
{
if(itemNum[i] == prodNum)
return i;
}
return -1;
}
void updateItem(long int itemNum[], string itemName[], int quantity[], double unitPrice[], double totalPrice[], int count)
{
if(count == 0)
cout << "List is empty" << endl;
else
{
long int prodNum;
cout << " Enter the product number: ";
cin >> prodNum;
int index = searchList(itemNum, count, prodNum);
if(index >= 0)
{
cout << "Enter the product name: ";
cin >> itemName[index];
cout << "Enter the product's quantity: ";
cin >> quantity[index];
cout << "Enter the product unit price: $ ";
cin >> unitPrice[index];
while(unitPrice[count] < 0.10)
{
cout << "Enter the product unit price: $ ";
cin >> unitPrice[count];
}
totalPrice[index] = quantity[index] * unitPrice[index];
}
else
cout << prodNum << " is not found in the list" << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.