In: Computer Science
A college must track equipment items purchased using special funds.
Create an Inventory class that represents an equipment item. An equipment item consists of an 8-alphanumeric inventory number, a short description of the item, the purchase price of the item, and its current location (e.g.: room/building location). If an item is surplussed (e.g., gotten rid of), then the current location should say surplus, but the item should remain on the list.
Write a program that reads inventory items from a file into a
vector. Implement a menu system that allows the user to add, edit,
and delete records from the list as well as search the list based
on inventory number and print a report of all records.
The list should always be maintained in order of inventory number.
When the program closes, the data file should be overwritten with
the most recent data from the list.
Implement one of the sorting and searching algorithms from the chapter. Do not use the built-in sort function.
Be sure to use the same menu options as shown in the example so the auto-grader can accurately grade your assignment.
Sample
MAIN MENU 1 - Add 2 - Edit 3 - Delete 4 - Search 5 - Print All 6 - Exit Choice: 2 Enter inventory number to edit: 12-2322-12 Curent Values 12-2322-12 Printer 800.00 PW-590 EDIT MENU 1 - Inventory Number 2 - Description 3 - Price 4 - Location 5 - Done Choice: 3 Enter new price: 820.00 EDIT MENU 1 - Inventory Number 2 - Description 3 - Price 4 - Location 5 - Done Choice: 1 Enter new Inventory Number: 12-AB-3333 Invalid inventory number. EDIT MENU 1 - Inventory Number 2 - Description 3 - Price 4 - Location 5 - Done Choice: 5 MAIN MENU 1 - Add 2 - Edit 3 - Delete 4 - Search 5 - Print All 6 - Exit Choice: 5 12-1194-94 Switch 417.00 TR-123 12-1232-35 Monitor 300.00 Surplus 12-1384-91 MicroPlus 1200.00 Surplus 12-2322-12 Printer 820.00 PW-590 12-3245-21 Test 120.00 Surplus 14-4343-41 Cabinet 175.00 AW-212 14-4992-22 Bookshelf 375.00 BN-100 14-8383-12 Chair 70.00 BN-100 14-9842-85 Desk 283.00 BN-100 14-9923-95 Typewriter 120.00 Surplus MAIN MENU 1 - Add 2 - Edit 3 - Delete 4 - Search 5 - Print All 6 - Exit Choice: 6
Need the answer in C++ and always need a .cpp file, .h file, and driver file
Working code implemented in C++ and appropriate comments provided for better understanding.
Here I am attaching code for these files:
main.cpp:
#include <iostream>
#include <fstream>
#include <vector>
#include "Inventory.h"
#include <iomanip>
using namespace std;
int main() {
string partNumber, name, location;
double price;
bool stop = false, recordFound;
int selection, recordNumber;
vector<Inventory> partList;
ifstream inputFile;
inputFile.open("items.txt");
if (!inputFile) {
cerr << "Unable to open file
items.txt";
exit(1); // call system to
stop
}
while(inputFile) {
inputFile >>
partNumber;
inputFile >> name;
inputFile >> price;
inputFile >> location;
partList.push_back(Inventory(partNumber, name, price,
location));
}
partList.pop_back();
cout << "\n\t***********************\n\t**
Current Inventory **\n\t***********************\n";
for (int i = 0; i < partList.size(); i++) {
cout << fixed <<
setprecision(2) << "Record Number: " << i + 1 <<
setw(8) << left << " " << setw(12) << left
<< partList[i].getPartNumber() << " " << setw(12)
<< left <<
partList[i].getName() << " " << setw(12) << left
<< partList[i].getPrice() << " " <<
partList[i].getLocation() << endl;
}
while (!stop) {
cout << "\n\nWhat action
would you like to perform?\n 1. Add\n 2. Edit\n 3. Delete\n 4.
Search\n 5. Print all\n 6. Exit\n";
cin >> selection;
switch (selection) {
case 1: //add a
record
cout << "You've selected to add a
record..\nPlease enter the inventory number: ";
cin >> partNumber;
cout << "Please enter the name of the item
(as one word): ";
cin >> name;
cout << "Please enter the cost of the
item: ";
cin >> price;
cout << "Please enter the location of the
item: ";
cin >> location;
cout << "\n*Confirm addition of record: "
<< partNumber << " " << name << " "
<< price << " " << location << endl
<< "Press 1 to add\nPress 0 to cancel";
cin >> selection;
if (selection == 1) {
partList.push_back(Inventory(partNumber, name, price,
location));
}
else {
cout << "\nRecord was
not added.";
}
break;
case 2: //edit a
record
cout << "\nYou've selected to edit a
record..\nSelect the number of Inventory Number record you would
like to edit: ";
cin >> recordNumber;
cout << "You've selected record number: "
<< recordNumber;
cout << "\n1. Inventory Number \n2.
Description \n3. Price \n4. Location\n5. Done \n";
cin >> selection;
if (selection == 1) {
cout << "Please enter
the new inventory number: ";
cin >>
partNumber;
partList[recordNumber -
1].setPartNumber(partNumber);
}
else if (selection == 2) {
cout << "Please enter
the new item name: ";
cin >> name;
partList[recordNumber -
1].setName(name);
}
else if (selection == 3) {
cout << "Please enter
the new item price: ";
cin >> price;
partList[recordNumber -
1].setPrice(price);
}
else if (selection == 4) {
cout << "Please enter
the new item location: ";
cin >> location;
partList[recordNumber -
1].setLocation(location);
}
else
if(selection == 5)
{
break;
}
case 3: //remove
a record
cout << "\nYou've selected to delete a
record..\nSelect the number of the record you would like to delete:
";
cin >> recordNumber;
cout << "\nYou've selected: ";
cout << fixed << setprecision(2)
<< "Record Number: " << recordNumber << " "
<< partList[recordNumber - 1].getPartNumber() << " "
<<
partList[recordNumber -
1].getName() << " " << partList[recordNumber -
1].getPrice() << " " << partList[recordNumber -
1].getLocation() << endl;
cout << "\nConfirm Deletion\nPress 1 for
yes\nPress 0 for no\n";
cin >> selection;
if (selection == 1) {
partList.erase(partList.begin() + recordNumber - 1);
}
else {
cout << "Deletion
canceled\n";
}
break;
case 4: //search
a record
cout << "You've selected to search for a
record..\nPlease enter the inventory of the record using the format
(XX-XXXX-XX): ";
cin >> partNumber;
recordFound = false;
for (int i = 0; i < partList.size(); i++)
{
if
(partList[i].getPartNumber() == partNumber) {
cout
<< "This record exists: \n" << fixed <<
setprecision(2) << "Record Number: " << i + 1 <<
" " << partList[i].getPartNumber() << " "
<<
partList[i].getName() << " " <<
partList[i].getPrice() << " " <<
partList[i].getLocation() << endl;
recordFound = true;
}
if (recordFound == false
&& (i + 1) == partList.size()) {
cout
<< "\nThere was no record for this inventory number";
}
}
break;
case 5: //print
all records
cout << "\n\t***********************\n\t**
Current Inventory **\n\t***********************\n";
for (int i = 0; i < partList.size(); i++)
{
cout << fixed <<
setprecision(2) << "Record Number: " << i + 1 <<
setw(8) << left << " " << setw(12) << left
<< partList[i].getPartNumber() << " " << setw(12)
<< left <<
partList[i].getName() << " " << setw(12) << left
<< partList[i].getPrice() << " " <<
partList[i].getLocation() << endl;
}
break;
case 6: //save
and quit
ofstream outFile("items.txt");
for (int i = 0; i < partList.size(); i++)
{
outFile <<
partList[i].getPartNumber() << " ";
outFile <<
partList[i].getName() << " ";
outFile << fixed
<< setprecision(2) << partList[i].getPrice() << "
";
outFile <<
partList[i].getLocation() << endl;
}
stop = true;
break;
}
}
return 0;
}
Inventory.cpp:
#include "Inventory.h"
Inventory::Inventory() {
setPartNumber("00 - 0000 - 00");
setName("Default");
setLocation("None");
setPrice(0);
}
Inventory::Inventory(string partNum, string name, double price,
string location) {
setPartNumber(partNum);
setName(name);
setLocation(location);
setPrice(price);
}
void Inventory::setPartNumber(string partNum) {
partNumber = partNum;
}
void Inventory::setName(string nameOfItem) {
name = nameOfItem;
}
void Inventory::setLocation(string loc) {
location = loc;
}
void Inventory::setPrice(double priceOfItem) {
price = priceOfItem;
}
string Inventory::getPartNumber() {
return partNumber;
}
string Inventory::getName() {
return name;
}
string Inventory::getLocation() {
return location;
}
double Inventory::getPrice() {
return price;
}
Inventory.h:
#ifndef Inventory_H
#define Inventory_H
#include <string>
using namespace std;
class Inventory {
private:
string partNumber;
string name;
string location;
double price;
public:
Inventory();
Inventory(string partNumber, string name, double
price, string location);
void setPartNumber(string);
void setName(string);
void setLocation(string);
void setPrice(double);
string getPartNumber();
string getName();
string getLocation();
double getPrice();
};
#endif
Sample Output Screenshots: