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: 3729706 • Letter: Y

Question

You will be writing a (rather primitive) online store simulator. It will have three classes: Product, Customer and Store. 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 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.

#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 <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

I was able to complete the implementation files for Product.cpp and Customer.cpp. I got stuck on Store.cpp, especially the member function std::vector<std::string> productSearch(std::string str). Any guidance or explanation greatly appreciated.

Explanation / Answer

Code for "Store.cpp":

#include<string.h>
#include<stdio.h>
#include<bits/stdc++.h>
#include"Store.hpp"
using namespace std;
//add the product in inventory vector
Store::addProduct(Product *p)
{
inventory.push_back(p);
}
//add the member into members vector
Store::void addMember(Customer* c)
{
members.push_back(c);
}
//return product which matches the id and Null if not present
Store::Product* getProductFromID(std::string idd)
{
Product *p;
for(i=0;i<inventory.size();i++)
{
p=inventory[i];
string id=p.getIdCode();
if(strcmp(id,idd)==0)
{
return p;//if both the id here and id in the product is same then return product
}
}
return NULL;
}
//return customer object pointer which matches the customer id and null if no such member is present
Store::Customer* getMemberFromID(std::string cusid)
{
Customer * c;
for(i=0;i<members.size();i++)
{
c=members[i];
string id=c.getAccountID();
if(strcmp(id,cusid)==0)
{
return c;
}
}
return NULL;

}
//search for product
Store::std::vector<std::string> productSearch(std::string str)
{
/*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.*/
string str1="";
//i have made one new string which contain the string(str1) having first letter small if it is capital in str and have first letter capital if the first letter
//in the str is small --> (if str="wood" then str1="Wood" ) and if(str=="Wood" then str1="wood") so we need to make sure that any of the string is present in
//either title or description
if(str[0]<='Z' && str[0]>='A')
{
str1=str1+(str[0]-'A'+'a');//make first letter small
}
else if(str[0]<='z' && str[0]>='a'){
str1=str1+(str[0]-'a'+'A');//make first letter capital
}
else{
str1=str1+str[0];//if first letter is not alphabet then leave it as it is
}
str1=str1+str.substring(1);//add the substring from index 1 to last as it is into str1
vector<string> productId;//to return
Product *p;
for(i=0;i<inventory.size();i++)
{
p=inventory[i];
string title=p.getTitle();
string desc=p.getDescription();
std::size_t found = title.find(str);
std::size_t found1 = title.find(str1);
std::size_t found2 = desc.find(str);
std::size_t found3 = desc.find(str1);
//find if any is present or not
if(found!=std::string::npos || found1!=std::string::npos || found2!=std::string::npos || found3!=std::string::npos)
{
productId.push_back(p.getIdCode());
}
}
return productId;
}
Store::std::string addProductToMemberCart(std::string pID, std::string mID)
{
/* 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.*/
Product *p=getProductFromID(pID);
if(p==NULL)
{
return "product ID not found";
}
Customer *c=getMemberFromID(mID);
if(c==NULL)
{
return "member ID not found";
}
if(p.getQuantityAvailable()<=0)
{
return "product out of stock";
}
c.addProductToCart(p.getIdCode());
return "product added to cart";
}
Store::double checkOutMember(std::string mID)
{
/*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.*/
Customer *c=getMemberFromID(mID);
double cost=0;
int i;
//if customer not present then return -1
if(c==NULL)
{
return -1;
}
//product in the cart of customer
vector<string> products=c.getCart();
//process each product
for(i=0;i<products.size();i++)
{
if(products[i].getQuantityAvailable()>0)//if it is available then only add its cost
{
cost=cost+products[i].getPrice();
products[i].decreaseQuantity();//decrease quantity
}
}
//if the member is premium then shipping cost is 0 (no need to add into cost)
if(!c.isPremiumMember())
{
cost=cost+(cost*7.0/100.0);//add 7% into cost
}
c.emptyCart();
return cost;
}

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