Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will be writing a (rather primitive) online store simulator. It will have th

ID: 3729356 • Letter: Y

Question

You will be writing a (rather primitive) online store simulator. It will have three classes: Product, Customer and Store. To make things a little simpler for you, I am supplying you with the three .hpp files. You will write the three implementation files. You should not alter the provided .hpp files.

Here are the .hpp files: Product.hpp, Customer.hpp and Store.hpp.

#ifndef PRODUCT_HPP
#define PRODUCT_HPP
#include <string>
class Product
{
private:
std::string idCode;
std::string title;
std::string description;
double price;
int quantityAvailable;
public:
Product(std::string id, std::string t, std::string d, double p, int
qa);
std::string getIdCode();
std::string getTitle();
std::string getDescription();
double getPrice();
int getQuantityAvailable();
void decreaseQuantity();
};
#endif


#ifndef CUSTOMER_HPP
#define CUSTOMER_HPP
#include <vector>
#include "Product.hpp"
class Customer
{
private:
std::vector<std::string> cart;
std::string name;
std::string accountID;
bool premiumMember;
public:
Customer(std::string n, std::string a, bool pm);
std::string getAccountID();
std::vector<std::string> getCart();
void addProductToCart(std::string);
bool isPremiumMember();
void emptyCart();
};
#endif


#ifndef STORE_HPP
#define STORE_HPP
#include <vector>
#include <string>
#include "Customer.hpp"
class Store
{
private:
std::vector<Product*> inventory;
std::vector<Customer*> members;
public:
void addProduct(Product* p);
void addMember(Customer* c);
Product* getProductFromID(std::string);
Customer* getMemberFromID(std::string);
std::vector<std::string> productSearch(std::string str);
std::string addProductToMemberCart(std::string pID, std::string mID);
double checkOutMember(std::string mID);
};
#endif

Here are descriptions of methods for the three classes:

Product:

A Product object represents a product with an ID code, title, description, price and quantity available.

constructor - takes as parameters five values with which to initialize the Product's idCode, title, description, price, and quantity available

get methods - return the value of the corresponding data member

decreaseQuantity - decreases the quantity available by one

Customer:

A Customer object represents a customer with a name and account ID. Customers must be members of the Store to make a purchase. Premium members get free shipping.

constructor - takes as parameters three values with which to initialize the Customer's name, account ID, and whether the customer is a premium member

get methods - return the value of the corresponding data member

isPremiumMember - returns whether the customer is a premium member

addProductToCart - adds the product ID code to the Customer's cart

emptyCart - empties the Customer's cart

Store:

A Store object represents a store, which has some number of products in its inventory and some number of customers as members.

addProduct - adds a product to the inventory

addMember - adds a customer to the members

getProductFromID - returns pointer to product with matching ID. Returns NULL if no matching ID is found.

getMemberFromID - returns pointer to customer with matching ID. Returns NULL if no matching ID is found.

productSearch - return a sorted vector of ID codes for every product whose title or description contains the search string. The first letter of the search string should be case-insensitive, i.e. a search for "wood" should match Products that have "Wood" in their title or description, and a search for "Wood" should match Products that have "wood" in their title or description. You may use string::find() and string::npos (Links to an external site.)Links to an external site.. You may assume that the search string will consist of a single word.

addProductToMemberCart -  If the product isn't found in the inventory, return "product ID not found". If the member isn't found in the members, return "member ID not found". If both are found and the product is still available, call the member's addProductToCart method to add the product and then return "product added to cart". If the product was not still available, return "product out of stock".  This function does not need to check how many of that product are available - just that there is at least one. It should also not change how many are available - that happens during checkout. The same product can be added multiple times if the customer wants more than one of something.

checkOutMember -  If the member ID isn't found, return -1. Otherwise return the charge for the member's cart. This will be the total cost of all the items in the cart, not including any items that are not in the inventory or are out of stock, plus the shipping cost. If a product is not out of stock, you should add its cost to the total and decrease the available quantity of that product by 1. Note that it is possible for an item to go out of stock during checkout. For example, if the customer has two of the same product in their cart, but the store only has one of that product left, the customer will be able to buy the one that's available, but won't be able to buy a second one, because it's now out of stock. For premium members, the shipping cost is $0. For normal members, the shipping cost is 7% of the total cost of the items in the cart. When the charge for the member's cart has been tabulated, the member's cart should be emptied, and the charge amount returned.

You must submit these files: Product.cpp, Customer.cpp, and Store.cpp. You do not need to submit the .hpp files.

In the main method you use for testing, you should only need to #include Store.hpp. Remember that your compile command needs to list all of the .cpp files.

Explanation / Answer

//Customer.cpp

//include the needed files

#include "Product.hpp"

#include "Customer.hpp"

//overloaded-constructor

Customer::Customer(std::string n, std::string a, bool pm)

{

     name=n;

     accountID=a;

     premiumMember=pm;

}

//get accountID

std::string Customer::getAccountID()

{

     return accountID;

}

//get cart

std::vector<std::string> Customer::getCart()

{

     return cart;

}

//add sid to cart

void Customer::addProductToCart(std::string sid)

{

     cart.push_back(sid);

}

//return true if customer is premiumMember

bool Customer::isPremiumMember()

{

     if(premiumMember==true)

          return true;

     return false;

}

//function empty the cart

void Customer::emptyCart()

{

     cart.empty();

}

//Product.cpp

//include the needed files

#include "Product.hpp"

//copy-constructor

Product:: Product(std::string id, std::string t, std::string d, double p, int qa)

{

     idCode=id;

     title=t;

     description=d;

     price=p;

     quantityAvailable=qa;

}

//get product idCode

std::string Product::getIdCode()

{

     return idCode;

}

//get product title

std::string Product::getTitle()

{

     return title;

}

//get product description

std::string Product::getDescription()

{

     return description;

}

//get price

double Product::getPrice()

{

     return price;

}

//return the quantityAvailable

int Product::getQuantityAvailable()

{

     return quantityAvailable;

}

//decrease the quantityAvailable by 1

void Product::decreaseQuantity()

{

     quantityAvailable -= 1;

}

//Store.cpp

//include the needed files

#include "Store.hpp"

#include<iostream>

#include <string>

#include <vector>

//add product p tp inventory

void Store::addProduct(Product* p)

{

     inventory.push_back(p);

}

//add customer c to members

void Store::addMember(Customer* c)

{

     members.push_back(c);

}

//get Product from inventory using ID

Product* Store::getProductFromID(std::string ID)

{

     int kk=0;

     for(kk=0;kk<inventory.size();kk++)

     {

          if(ID.compare(inventory[kk]->getIdCode())==0)

              return inventory[kk];

     }

     return NULL;

}

//get Customer from members using ID

Customer* Store::getMemberFromID(std::string ID)

{

     int kk=0;

     for(kk=0;kk<members.size();kk++)

     {

          if(ID.compare(members.at(kk)->getAccountID())==0)

              return members[kk];

     }

     return NULL;

}

//return product by str

void Store::productSearch(std::string str)

{

     int kk=0;

     for(kk=0;kk<inventory.size();kk++)

     {

if((inventory[kk]-> getTitle().find(str)!=std::string::npos) ||(inventory[kk]-> getDescription().find(str)!=std::string::npos))

          {

              std::cout<<inventory[kk]->getTitle()<<std::endl;

std::cout<<"ID code:"<<inventory[kk]-> getIdCode()<<std::endl;

std::cout<<"Price: $"<<inventory[kk]-> getPrice()<<std::endl;

std::cout<<inventory[kk]-> getDescription()<<std::endl;

          }

     }

}

//add product to customer cart

void Store::addProductToMemberCart(std::string pID, std::string mID)

{

     int kk=0;

     int fg1=0, fg2=0;

     for(kk=0;kk<inventory.size();kk++)

     {

          if(pID.compare(inventory[kk]->getIdCode())==0)

          {

              fg1=1;

              for(int aa=0;aa<members.size();aa++)

              {

if(mID.compare(members.at(aa)-> getAccountID())==0)

                   {

                        fg2=1;

if(inventory[kk]-> getQuantityAvailable()>1)

                        {

members.at(aa)-> addProductToCart(pID);

                        }

                        else

std::cout<<" Sorry , product #"<<inventory[kk]->getIdCode()<<" is currently out of stock."<<std::endl;

                   }

              }

              if(fg2==0)

              {

std::cout<<" Sorry , Member #"<<mID<<" not found"<<std::endl;

              }

          }

     }

     if(fg1==0)

          std::cout<<" Product #"<<pID<<" not found"<<std::endl;

}

//process the cutomer cart

void Store::checkOutMember(std::string mID)

{

     int kk=0,fg=0;

     double ss=0;

     for(kk=0;kk<members.size();kk++)

     {

          if(mID.compare(members.at(kk)->getAccountID())==0)

          {

              fg=1;

std::vector<std::string> custCat=members.at(kk)-> getCart();

              int ff=0;

              for(int aa=0;aa<custCat.size();aa++)

              {

                   ff=0;

                   for(int bb=0;bb<inventory.size();bb++)

                   {

if(inventory[bb]-> getIdCode().compare(custCat[aa])==0)

                        {

                             ff=1;

if(inventory[bb]-> getQuantityAvailable()>=1)

                             {

ss += inventory[bb]-> getPrice();

inventory[bb]-> decreaseQuantity();

std::cout<<inventory[bb]-> getTitle()<<" - $"<<inventory[bb]-> getPrice()<<std::endl;

                             }

                             else

std::cout<<" Sorry product #"<<inventory[bb]-> getIdCode()<<","<<inventory[bb]-> getTitle()<<", is no longer available"<<std::endl;

                        }

                   }

              }

              std::cout<<"Subtotal: $"<<ss<<std::endl;

              int shipCst=0;

              if(members[kk]->isPremiumMember())

                   shipCst=0;

              else

                   shipCst= 0.07* ss;

std::cout<<"Shipping cost: $"<<shipCst<<std::endl;

              std::cout<<"Total: $"<<(ss+shipCst)<<std::endl;

          }

     }

     if(fg==0)

std::cout<<" Sorry , Member #"<<mID<<" not found"<<std::endl;

}

//main.cpp

//include the needed files

#include "Store.hpp"

#include "Customer.hpp"

#include "Product.hpp"

#include <iostream>

#include "string"

using namespace std;

//main

int main()

{

     //customer c1 & c2

     Customer c1("Arlen","1",true);

     Customer c2("Tom","2", false);

     //Product P1 & P2

     Product P1("123","red blender","Sturdy blender",350,4);

Product P2("345","hot air ballon","Fly into the sky in your own ballon",750,4);

     //Store s

     Store s;

     //Add product

     s.addProduct(&P1);

     s.addProduct(&P2);

     //Add customer

     s.addMember(&c1);

     s.addMember(&c2);

     //Add product to member cart

     s.addProductToMemberCart("123","1");

     s.addProductToMemberCart("345","1");

     s.productSearch("red");

     //check out c1

     s.checkOutMember("1");

     system("pause");

     return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote