Advertisement
COSCI539

CS136 Lab1 [3]

Feb 25th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.58 KB | None | 0 0
  1. //Lab 1 Wilson, Drayden T Th
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. #define INV_ARR_MAX 25
  10.  
  11. struct productInfo
  12. {
  13.     string ID, name;
  14.     int quantity, price;
  15. };
  16.  
  17. typedef productInfo product;
  18.  
  19. int fillInventory(product *list[], ifstream& inFile);
  20. void fillSortedInventory(product *list[], product *listSorted[], int listSize);
  21. void printInventory(product *list[], int listSize)
  22. int compareItems(const void *item1, const void *item2)
  23. void searchInventory(product *list[], int listSize)
  24. void searchInventory(product *list[], int listSize)
  25. void deallocateMemory(product *list[], int listSize);
  26. void runtimeError(const string errorMessage);
  27.  
  28. int main()
  29. {
  30.     product *pInventoryUnsorted[INV_ARR_MAX] = {nullptr},
  31.             *pInventorySorted[INV_ARR_MAX] = {nullptr};
  32.     int productCount, userMenuSelection = 0;
  33.     ifstream inFile;
  34.    
  35.     inFile.open("...");
  36.    
  37.     if (!inFile)
  38.     {
  39.         runtimeError("Failed to open input file.");
  40.     }
  41.    
  42.     productCount = fillInventory(pInventoryUnsorted, inFile);
  43.    
  44.     inFile.close();
  45.    
  46.     fillSortedInventory(pInventoryUnsorted, pInventorySorted, productCount);
  47.    
  48.     cout << "[1] Display the inventory unsorted.\n"
  49.          << "[2] Display the inventory in ascending order by a field.\n"
  50.          << "[3] Search for an item by ID or name.\n"
  51.          << "[4] Display the unique item count, total worth,"
  52.          << "and total item count of the inventory.\n"
  53.          << "[5] Exit.\n";
  54.          
  55.     do
  56.     {
  57.         int evenSum, oddSum, evenMedian, oddMedian, userMenuSelection;
  58.         bool sumCalculated = 0, medianCalculated = 0;
  59.        
  60.         if (userMenuSelection)
  61.         {
  62.             cout << "Select another option.: ";
  63.         }
  64.         else
  65.         {
  66.             cout << "Select an option by entering its corresponding number: ";
  67.         }
  68.        
  69.         cin >> userMenuSelection;
  70.    
  71.         switch(userMenuSelection)
  72.         {
  73.             case 1:
  74.                 printInventory(pInventoryUnsorted, productCount);
  75.                 break;
  76.             case 2:
  77.                 qsort(pInventorySorted, productCount, sizeof(product*), compareStructs);
  78.                 printInventory(pInventorySorted, productCount);
  79.                 break;
  80.             case 3:
  81.                 searchInventory(pInventoryUnsorted, productCount);
  82.                 break;
  83.             case 4:
  84.                 printInventoryInfo(pInventoryUnsorted, productCount);
  85.                 break;
  86.             case 5:
  87.                 cout << "Exiting program.\n";
  88.                 break;
  89.             default:
  90.                 cout << "Invalid menu option selected.\n";
  91.                
  92.                 userMenuSelection = 1; //avoids repeating initial instructions
  93.                 break;
  94.         }
  95.     } while (userMenuSelection != 5);
  96.    
  97.     deallocateMemory(pInventoryUnsorted, productCount);
  98.  
  99.     return 0;
  100. }
  101.  
  102. int fillInventory(product *list[], ifstream& inFile)
  103. {
  104.     product test;
  105.     int i;
  106.    
  107.     for (i = 0; inFile >> test.ID && i < INV_ARR_MAX; ++i)
  108.     {
  109.        
  110.         inFile >> test.name >> test.quantity >> test.price;
  111.        
  112.         transform((test.name).begin(), (test.name).end(), (test.name).begin(), ::toupper);
  113.        
  114.         if (test.quantity < 0 || test.price < 0.01)
  115.         {
  116.             runtimeError("Invalid entry detected."); //specific enough?
  117.         }
  118.         else
  119.         {
  120.            list[i] = (product *)malloc(sizeof(product)); //try catch
  121.            
  122.            *list[i] = test;
  123.         }
  124.     }
  125.    
  126.     if (i == 0)
  127.     {
  128.         runtimeError("Input file is empty.");
  129.     }
  130.     else if (!inFile.eof())
  131.     {
  132.         runtimeError("Excess entries detected.");
  133.     }
  134.  
  135.     return i;
  136. }
  137.  
  138. void fillSortedInventory(product *list[], product *listSorted[], int listSize)
  139. {
  140.     for (int i = 0; i < listSize; ++i)
  141.     {
  142.         listSorted[i] = list[i];
  143.     }
  144. }
  145.  
  146. void printInventory(product *list[], int listSize)
  147. {
  148.     for (int i = 0; i < listSize; ++i)
  149.     {
  150.         cout << list[i]->ID << ' '
  151.              << list[i]->name << ' '
  152.              << list[i]->quantity << ' '
  153.              << list[i]->price << endl;
  154.     }
  155. }
  156.  
  157. int compareItems(const void *item1, const void *item2)
  158. {
  159.     product *pItem1 = *(product**)item1;
  160.     product *pItem2 = *(product**)item2;
  161.     int sortChoice;
  162.    
  163.     cout << "Select an option to sort the list by "
  164.          << "[1]ID, [2]name, [3]quantity, [4]price: ";
  165.     cin  >> sortChoice;
  166.    
  167.     switch (sortChoice)
  168.     {
  169.         case 1:
  170.             return pItem1->ID.compare(pItem2->ID);
  171.         case 2:
  172.             return pItem1->name.compare(pItem2->name);
  173.         case 3:
  174.             return pItem1->quantity - pItem2->quantity;
  175.         case 4:
  176.             return pItem1->price - pItem2->price;
  177.     }
  178. }
  179.  
  180. void searchInventory(product *list[], int listSize)
  181. {
  182.     string searchItem;
  183.    
  184.     cout << "Input an ID or name to search by: ";
  185.     cin  >> searchItem; //assuming input is all numbers or all letters
  186.    
  187.     for (int i = 0; i < listSize; ++i)
  188.     {
  189.         if (isdigit(searchItem[0]))
  190.         {
  191.             if (list[i]->ID == searchItem)
  192.             {
  193.                 cout << list[i]->ID << ' '
  194.                      << list[i]->name << ' '
  195.                      << list[i]->quantity << ' '
  196.                      << list[i]->price << endl;
  197.             }
  198.             else
  199.             {
  200.                 cout << "Item not found.\n";
  201.             }
  202.         }
  203.         else
  204.         {
  205.             if (list[i]->name == searchItem)
  206.             {
  207.                 cout << list[i]->ID << ' '
  208.                      << list[i]->name << ' '
  209.                      << list[i]->quantity << ' '
  210.                      << list[i]->price << endl;
  211.             }
  212.             else
  213.             {
  214.                 cout << "Item not found.\n";
  215.             }
  216.         }
  217.        
  218.     }
  219. }
  220.  
  221. void printInventoryInfo(product *list[], int listSize)
  222. {
  223.     int inventoryValue, inventorySize;
  224.    
  225.     for (int i = 0; i < listSize; ++i)
  226.     {
  227.         inventoryValue += list[i]->price;
  228.         inventorySize += list[i]->quantity;
  229.     }
  230.    
  231.     cout << "Unique Items: " << listSize
  232.          << "\nInventory Value: " << inventoryValue
  233.          << "\nInventory Size: " << inventorySize << endl;
  234. }
  235.  
  236. void deallocateMemory(product *list[], int listSize)
  237. {
  238.     for (int i = 0; i < listSize; ++i)
  239.     {
  240.         free((void *)list[i]);
  241.     }
  242. }
  243.  
  244. void runtimeError(const string errorMessage)
  245. {
  246.     cout << "Error: " << errorMessage << " Exiting program.\n";
  247.     exit(1);
  248. }
  249.  
  250. //error check input data type?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement