Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lab 1 Wilson, Drayden T Th
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- using namespace std;
- #define INV_ARR_MAX 25
- struct productInfo
- {
- string ID, name;
- int quantity, price;
- };
- typedef productInfo product;
- int fillInventory(product *list[], ifstream& inFile);
- void fillSortedInventory(product *list[], product *listSorted[], int listSize);
- void printInventory(product *list[], int listSize)
- int compareItems(const void *item1, const void *item2)
- void searchInventory(product *list[], int listSize)
- void searchInventory(product *list[], int listSize)
- void deallocateMemory(product *list[], int listSize);
- void runtimeError(const string errorMessage);
- int main()
- {
- product *pInventoryUnsorted[INV_ARR_MAX] = {nullptr},
- *pInventorySorted[INV_ARR_MAX] = {nullptr};
- int productCount, userMenuSelection = 0;
- ifstream inFile;
- inFile.open("...");
- if (!inFile)
- {
- runtimeError("Failed to open input file.");
- }
- productCount = fillInventory(pInventoryUnsorted, inFile);
- inFile.close();
- fillSortedInventory(pInventoryUnsorted, pInventorySorted, productCount);
- cout << "[1] Display the inventory unsorted.\n"
- << "[2] Display the inventory in ascending order by a field.\n"
- << "[3] Search for an item by ID or name.\n"
- << "[4] Display the unique item count, total worth,"
- << "and total item count of the inventory.\n"
- << "[5] Exit.\n";
- do
- {
- int evenSum, oddSum, evenMedian, oddMedian, userMenuSelection;
- bool sumCalculated = 0, medianCalculated = 0;
- if (userMenuSelection)
- {
- cout << "Select another option.: ";
- }
- else
- {
- cout << "Select an option by entering its corresponding number: ";
- }
- cin >> userMenuSelection;
- switch(userMenuSelection)
- {
- case 1:
- printInventory(pInventoryUnsorted, productCount);
- break;
- case 2:
- qsort(pInventorySorted, productCount, sizeof(product*), compareStructs);
- printInventory(pInventorySorted, productCount);
- break;
- case 3:
- searchInventory(pInventoryUnsorted, productCount);
- break;
- case 4:
- printInventoryInfo(pInventoryUnsorted, productCount);
- break;
- case 5:
- cout << "Exiting program.\n";
- break;
- default:
- cout << "Invalid menu option selected.\n";
- userMenuSelection = 1; //avoids repeating initial instructions
- break;
- }
- } while (userMenuSelection != 5);
- deallocateMemory(pInventoryUnsorted, productCount);
- return 0;
- }
- int fillInventory(product *list[], ifstream& inFile)
- {
- product test;
- int i;
- for (i = 0; inFile >> test.ID && i < INV_ARR_MAX; ++i)
- {
- inFile >> test.name >> test.quantity >> test.price;
- transform((test.name).begin(), (test.name).end(), (test.name).begin(), ::toupper);
- if (test.quantity < 0 || test.price < 0.01)
- {
- runtimeError("Invalid entry detected."); //specific enough?
- }
- else
- {
- list[i] = (product *)malloc(sizeof(product)); //try catch
- *list[i] = test;
- }
- }
- if (i == 0)
- {
- runtimeError("Input file is empty.");
- }
- else if (!inFile.eof())
- {
- runtimeError("Excess entries detected.");
- }
- return i;
- }
- void fillSortedInventory(product *list[], product *listSorted[], int listSize)
- {
- for (int i = 0; i < listSize; ++i)
- {
- listSorted[i] = list[i];
- }
- }
- void printInventory(product *list[], int listSize)
- {
- for (int i = 0; i < listSize; ++i)
- {
- cout << list[i]->ID << ' '
- << list[i]->name << ' '
- << list[i]->quantity << ' '
- << list[i]->price << endl;
- }
- }
- int compareItems(const void *item1, const void *item2)
- {
- product *pItem1 = *(product**)item1;
- product *pItem2 = *(product**)item2;
- int sortChoice;
- cout << "Select an option to sort the list by "
- << "[1]ID, [2]name, [3]quantity, [4]price: ";
- cin >> sortChoice;
- switch (sortChoice)
- {
- case 1:
- return pItem1->ID.compare(pItem2->ID);
- case 2:
- return pItem1->name.compare(pItem2->name);
- case 3:
- return pItem1->quantity - pItem2->quantity;
- case 4:
- return pItem1->price - pItem2->price;
- }
- }
- void searchInventory(product *list[], int listSize)
- {
- string searchItem;
- cout << "Input an ID or name to search by: ";
- cin >> searchItem; //assuming input is all numbers or all letters
- for (int i = 0; i < listSize; ++i)
- {
- if (isdigit(searchItem[0]))
- {
- if (list[i]->ID == searchItem)
- {
- cout << list[i]->ID << ' '
- << list[i]->name << ' '
- << list[i]->quantity << ' '
- << list[i]->price << endl;
- }
- else
- {
- cout << "Item not found.\n";
- }
- }
- else
- {
- if (list[i]->name == searchItem)
- {
- cout << list[i]->ID << ' '
- << list[i]->name << ' '
- << list[i]->quantity << ' '
- << list[i]->price << endl;
- }
- else
- {
- cout << "Item not found.\n";
- }
- }
- }
- }
- void printInventoryInfo(product *list[], int listSize)
- {
- int inventoryValue, inventorySize;
- for (int i = 0; i < listSize; ++i)
- {
- inventoryValue += list[i]->price;
- inventorySize += list[i]->quantity;
- }
- cout << "Unique Items: " << listSize
- << "\nInventory Value: " << inventoryValue
- << "\nInventory Size: " << inventorySize << endl;
- }
- void deallocateMemory(product *list[], int listSize)
- {
- for (int i = 0; i < listSize; ++i)
- {
- free((void *)list[i]);
- }
- }
- void runtimeError(const string errorMessage)
- {
- cout << "Error: " << errorMessage << " Exiting program.\n";
- exit(1);
- }
- //error check input data type?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement