Write a program that uses a structure to store the following inventory data in a
ID: 3693245 • Letter: W
Question
Write a program that uses a structure to store the following inventory data in a file: The data can be either read from a text file or the keyboard Item name (string) Quantity on hand(int) Wholesale cost(double) Retail Cost(double) The program should have a menu that allows the user to perform the following tasks: 1. Add new records to the file 2. Display any record in the file a. User will provide the name of the item or the first n letters of the name of the item 3. Change any record in the file (Note: if this option chosen, then the whole record must be reentered even if the user wants to change only one or more fields) 4. Display all records 5. Prepare a report containing: a. The total wholesale value of the inventory b. The total retail value of the inventory c. The total quantity of all values in the inventory. Input validation: The program should not accept quantities or wholesale or retail costs less than 0.
Explanation / Answer
Ans;
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Item {
public:
string name;
int quantity;
double wholesaleCost;
double retailCost;
};
void addRecord(vector<Item*>& store) {
string msg = "Enter record in format: Name Quantity Wholesale-cost Retail-cost";
bool right = false;
Item* item = new Item();
do {
cout << msg << endl;
cin >> item->name >> item->quantity >> item->wholesaleCost >> item->retailCost;
if (item->wholesaleCost < 0 || item->retailCost < 0) right = false;
else right = true;
} while (!right);
store.push_back(item);
}
void displaySingle(vector<Item*>& store) {
cout << "Enter item name to search: ";
string name;
cin >> name;
for (int i = 0; i < store.size(); i++) {
string itemname = store[i]->name;
if (itemname.compare(0, name.size(), name) == 0) {
cout << store[i]->name << " " << store[i]->quantity << " " << store[i]->wholesaleCost << " " << store[i]->retailCost << endl;
}
}
}
void displayAll(vector<Item*>& store) {
cout << "Displaying all records" << endl;
for (int i = 0; i < store.size(); i++) {
cout << store[i]->name << " " << store[i]->quantity << " " << store[i]->wholesaleCost << " " << store[i]->retailCost << endl;
}
}
void changeRecord(vector<Item*>& store) {
cout << "Enter item name to change: ";
string msg = "Enter record in format: Name Quantity Wholesale-cost Retail-cost";
string name;
cin >> name;
for (int i = 0; i < store.size(); i++) {
if (name.compare(store[i]->name) == 0) {
cout << msg << endl;
cin >> store[i]->name >> store[i]->quantity >> store[i]->wholesaleCost >> store[i]->retailCost;
}
}
}
void prepareReport(vector<Item*>& store) {
cout << "Preparing report" << endl;
int totQ = 0;
double totWCost = 0;
double totRCost = 0;
for (int i = 0; i < store.size(); i++) {
totQ += store[i]->quantity;
totWCost += store[i]->wholesaleCost;
totRCost += store[i]->retailCost;
}
cout << "Report: " << endl;
cout << " Total Quantity : " << totQ << endl;
cout << " Total WholesaleCost : " << totWCost << endl;
cout << " Total RetailCost : " << totRCost << endl;
}
int main() {
vector<Item*> store;
string menu = "Enter your choice: 0 = exit 1 = Add record 2 = Display single 3 = Display all 4 = Change record 5 = Prepare report";
int input = 1;
do {
cout << menu << endl;
cin >> input;
switch (input) {
case 1 :
addRecord(store);
break;
case 2 :
displaySingle(store);
break;
case 3 :
displayAll(store);
break;
case 4 :
changeRecord(store);
break;
case 5 :
prepareReport(store);
break;
}
} while(input != 0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.