Advertisement
Guest User

Untitled

a guest
Jul 13th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. #include "store.hpp"
  5. using std::string;
  6.  
  7.  
  8.  
  9.  
  10. //Adds a product to our inventory
  11. void Store::addProduct(Product* p)
  12. {
  13.     inventory.push_back(p);
  14. }
  15.  
  16.  
  17. //Searches through inventory and returns product if we carry that item
  18. Product* Store::getProductFromID(string prod)
  19. {
  20.  
  21.     int invSize = inventory.size();
  22.  
  23.     //iterate through each product and compare ID to search term
  24.     for (int i = 0; i < invSize; i++)
  25.     {
  26.         if(prod == inventory[i]->getIdCode())
  27.         {
  28.             return inventory[i]; //If found return Product
  29.  
  30.         }
  31.     }
  32.  
  33.     //If nothing found
  34.     return NULL;
  35. }
  36.  
  37.  
  38.  
  39. //Searches our product titles and descriptions to see if customer search
  40. //term matches any items in our inventory
  41. void Store::productSearch(string searchTerm)
  42. {
  43.  
  44.     //Put search term in lower case
  45.     for(int i = 0; i < searchTerm.size(); i++)
  46.         searchTerm[i] = tolower(searchTerm[i]);
  47.  
  48.  
  49.     for (int i = 0; i < inventory.size(); i++)
  50.     {
  51.  
  52.         //Create copy of Title and description so we can later
  53.         //put in lower case to match against search term
  54.         string itemTitleLower = inventory[i]->getTitle();
  55.         string itemDescLower = inventory[i]->getDescription();
  56.  
  57.         //Make title copy lowercase
  58.         for (int j = 0; j < itemTitleLower.size(); j++)
  59.         {
  60.             itemTitleLower[j] = tolower(itemTitleLower[j]);
  61.         }
  62.  
  63.         //Make description copy lowercase
  64.         for (int j = 0; j < itemDescLower.size(); j++)
  65.         {
  66.             itemDescLower[j] = tolower(itemDescLower[j]);
  67.         }
  68.  
  69.  
  70.         //If find yields > -1 we know we have a match
  71.         int foundInTitle = itemTitleLower.find(searchTerm);
  72.         int foundInDescription = itemDescLower.find(searchTerm);
  73.  
  74.         //start by saying no match found
  75.         bool foundItem = false;
  76.  
  77.         //But change that to a match if .find() shows > -1
  78.         if ((foundInTitle >= 0) || (foundInDescription >= 0))
  79.             foundItem = true;
  80.  
  81.         //If we get a match, print to the screen the item details
  82.         if(foundItem)
  83.         {
  84.             std::cout << inventory[i]->getTitle() << std::endl;
  85.             std::cout << "ID code: " << inventory[i]->getIdCode() << std::endl;
  86.             std::cout << "price: $" << inventory[i]->getPrice() << std::endl;
  87.             std::cout << inventory[i]->getDescription() << "\n" << std::endl;
  88.         }
  89.  
  90.     }
  91.  
  92. }
  93.  
  94.  
  95.  
  96.  
  97. //Adds a product to our member's cart
  98. void Store::addProductToCart(string pID)
  99. {
  100.  
  101.     Product * item = getProductFromID(pID);
  102.  
  103.     //If we don't have that item in our inventory
  104.     if(item == NULL)
  105.     {
  106.         std::cout << "Product #" << pID << " not found." << std::endl;
  107.         return;
  108.     }
  109.  
  110.     //Add product to cart if inventory actually has quantity to be added
  111.     if((item != NULL) && item->getQuantityAvailable() > 0)
  112.     {
  113.         shoppingCart.push_back(item);
  114.     }
  115.     else //If we are sold out of a product
  116.     {
  117.         std::cout << "Sorry, product #" << pID << " is currently out of stock." << std::endl;
  118.     }
  119.  
  120.  
  121. }
  122.  
  123. //Totals everything in cart and displays cost of everything
  124.  
  125. void Store::checkOut()
  126. {
  127.  
  128.  
  129.     if(shoppingCart.empty())
  130.     {
  131.         std::cout << "There are no items in the cart." << std::endl;
  132.     }
  133.     else
  134.     {
  135.         double subtotal = 0;  //used to hold subtotal before shipping cost
  136.  
  137.         int cartSize = shoppingCart.size();
  138.  
  139.         for(int i = 0; i < cartSize; i++)
  140.         {
  141.             //For each item in cart with quantity of 1 or more, print to screen details
  142.             string prodID = shoppingCart[i]->getIdCode();
  143.             if(getProductFromID(prodID)->getQuantityAvailable() > 0)
  144.             {
  145.                 //print to screen details
  146.                 std::cout << getProductFromID(prodID)->getTitle() << " - $" << getProductFromID(prodID)->getPrice() << std::endl;
  147.  
  148.                 //decrease inventory quantity and add cost to subtotal
  149.                 getProductFromID(prodID)->decreaseQuantity();
  150.                 subtotal += getProductFromID(prodID)->getPrice();
  151.  
  152.             }
  153.             else
  154.             {
  155.                 //if quantity dips below 1
  156.                 std::cout << "Sorry, product #" << prodID << ", '" << getProductFromID(prodID)->getTitle() << "', is no longer available." << std::endl;
  157.             }
  158.         }
  159.  
  160.         //output subtotal before shipping
  161.         std::cout << "Subtotal: $" << subtotal << std::endl;
  162.  
  163.  
  164.         // Tax is 7% of subtotal
  165.  
  166.         double tax = subtotal * .07;
  167.         std::cout.precision(2); //So everything shows in 'dollars and cents'
  168.         std::cout << "Tax: $" << std::fixed << tax << std::endl;
  169.         std::cout << "Total: $" << subtotal + tax << std::endl;
  170.  
  171.  
  172.         //Clear member cart after they checkout
  173.         shoppingCart.clear();
  174.  
  175.     }
  176.  
  177.  
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement