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

Create an online store simulator. It will have three classes: Product, Customer

ID: 3778526 • Letter: C

Question

Create an online store simulator. It will have three classes: Product, Customer and Store.

Must implement header files provided (cannot alter headers):

Product.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

Customer.hpp

#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

Store.hpp

#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);
void productSearch(std::string str);
void addProductToMemberCart(std::string pID, std::string mID);
void 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 product with matching ID. Returns NULL if no matching ID is found.

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

productSearch - for every product whose title or description contains the search string, prints out that product's title, ID code, price and description. 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.

addProductToMemberCart -  If the product isn't found in the inventory, print "Product #[idCode goes here] not found." If the member isn't found in the members, print "Member #[accountID goes here] not found." If both are found and the product is still available, calls the member's addProductToCart method. Otherwise it prints "Sorry, product #[idCode goes here] is currently out of stock." The same product can be added multiple times if the customer wants more than one of something.

checkOut -  If the member isn't found in the members, print "Member #[accountID goes here] not found." Otherwise prints out the title and price for each product in the cart and decreases the available quantity of that product by 1. If any product has already sold out, then on that line it should print 'Sorry, product #[idCode goes here], "[product name goes here]", is no longer available.'At the bottom it should print out the subtotal for the cart, the shipping cost ($0 for premium members, 7% of the cart cost for normal members), and the final total cost for the cart (subtotal plus shipping). If the cart is empty, it should just print "There are no items in the cart." When the calculations are complete, the member's cart should be emptied.

Here is an example of how the output of the Store::productSearch method might look (searching for "red"):

Here is an example of how the output of the Store::checkOutMember method might look:

Class Files must be named: Product.cpp, Customer.cpp, and Store.cpp.

Main method used should be separate from the class files and should only include  (#include Store.hpp).

Thank you for any help on this.

Explanation / Answer

Product.cpp

#include <iostream>
#include <string>
#include "Product.hpp"
using namespace std;

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;
}

string Product::getIdCode(){
   return idCode;
}

string Product::getTitle(){
   return title;
}

string Product::getDescription(){
   return description;
}
double Product::getPrice(){
   return price;
}

int Product::getQuantityAvailable(){
   return quantityAvailable;
}

void Product::decreaseQuantity(){
   quantityAvailable--;
}

__________________________________________________________________

Customer.cpp

#include <iostream>
#include <string>
#include "Customer.hpp"
using namespace std;

Customer::Customer(std::string n, std::string a, bool pm){
   name = n;
    accountID = a;
    premiumMember = pm;
}

string Customer::getAccountID(){
   return accountID;
}

vector<std::string> Customer::getCart(){
   return cart;
}

void Customer::addProductToCart(std::string PID){
   cart.push_back(PID);
}

bool Customer::isPremiumMember(){
   return premiumMember;
}

void Customer::emptyCart(){
   cart.clear();
}

______________________________________________________________

Store.cpp

#include <iostream>
#include <string>
#include <algorithm>
#include "Store.hpp"
#include "Product.cpp"
#include "Customer.cpp"

using namespace std;

// convert a string into lower case
string toLowerCase(string s){
   transform(s.begin(), s.end(), s.begin(),(int (*)(int))tolower);
   return s;
}

void Store::addProduct(Product* p){
   inventory.push_back(p);
}

void Store::addMember(Customer* c){
   members.push_back(c);
}

Product* Store::getProductFromID(std::string productId){
   for(int i=0;i<inventory.size();i++){
       // check if the productId matches to ith product in inventory
       if(productId.compare(inventory[i]->getIdCode())==0){
           return inventory[i];
       }
   }
   return NULL;
}

Customer* Store::getMemberFromID(std::string memberId){
   for(int i=0;i<members.size();i++){
       // check if the memberId matches to any member of store
       if(memberId.compare(members[i]->getAccountID())==0){
           return members[i];
       }
   }
   return NULL;
}


//search for a product in a store with id str. first convert the search string to lowercase then
//converting the title or descrition to be search into lower case
void Store::productSearch(std::string str){
   str = toLowerCase(str);
   cout << "--- Search Result ---"<<endl;
   for(int i=0;i<inventory.size();i++){
       //lowercase version of title
       string tmpTitle = toLowerCase(inventory[i]->getTitle());
       // lower case version of description
       string tmpDes = toLowerCase(inventory[i]->getDescription());
       // chcek if search string is present in title or description
       if(tmpTitle.find(str)!= std::string::npos || tmpDes.find(str)!= std::string::npos){
           cout << "Title: "<< inventory[i]->getTitle() << ", IdCode: " << inventory[i]->getIdCode() << ", Price: $" << inventory[i]->getPrice() << ", Description:" << inventory[i]->getDescription() << endl;
       }
   }
}

// add prodcut to the cart of a member
void Store::addProductToMemberCart(std::string pID, std::string mID){
   Product *p = getProductFromID(pID);
   Customer *m = getMemberFromID(mID);
   // member and product both are present
   if(p!=NULL && m!=NULL){
       // whether product is in the stock or not
       if(p->getQuantityAvailable()>0){
           m->addProductToCart(pID);
       }else{
           cout << "Sorry, product "<<pID<<" is currently out of stock."<<endl;
       }
   }else{
       // store do not have the product with id pID
       if(p==NULL){
           cout << "Product "<< pID <<" not found."<<endl;
       }
       // member with id mID is not a member of the store
       if(p==NULL){
           cout << "Member " << mID << " not found." <<endl;
       }
   }
}

void Store::checkOutMember(std::string mID){
   Customer *m = getMemberFromID(mID);
   if(m==NULL){
       cout << "Member "<<mID <<" not found."<<endl;
   }else{
       cout << "--- Cart for CheckOut --- " << endl;
       // shopping cart of member
       vector<std::string> cart = m->getCart();
       double subTotal = 0;
       int totalItems = 0;
       for(int i=0;i<cart.size();i++){
           Product *p = getProductFromID(cart[i]);
           // whether product is in the stock or not
           if(p->getQuantityAvailable()>0){
               cout << "Title: "<<p->getTitle()<<", Price: $"<<p->getPrice()<<endl;
               subTotal += p->getPrice();
               totalItems++;
               p->decreaseQuantity();
           }else{
               cout << "Sorry, product "<< cart[i] <<", "<< p->getTitle()<<" is no longer available."<<endl;
           }
       }
       // whether number of items to be shipped is zero
       if(totalItems==0){
           cout << "There are no items in the cart."<<endl;
           return;
       }
       double shippingCost = (m->isPremiumMember()) ? 0 : 0.07*subTotal;
       double final = subTotal + shippingCost;
       cout << "Sub-Total: $"<< subTotal <<", Shipping Cost: $"<<shippingCost<<", Final Total Cost: $"<<final<<endl;
   }
}

___________________________________________________________

Main.cpp

// only for testing purpose

#include <iostream>
#include "Store.cpp"
using namespace std;

int main(){
   Customer *m = new Customer("Rahul Patidar","rahul6818",false);
   Product *p1 = new Product("mac123","Macbook","Macbook Air 15.5' ",1122.5,1);
   Product *p2 = new Product("air12","Air Purifier","Aqua Air Purifier Alpha' ",11.5,3);
   Product *p3 = new Product("aqua12","Water Purifier","Aqua Water Purifier' ",10,2);
   Store *s = new Store();
   s->addMember(m);
   s->addProduct(p1);
   s->addProduct(p3);
   s->addProduct(p2);
   s->productSearch("air");
   s->addProductToMemberCart(p1->getIdCode(),m->getAccountID());
   s->addProductToMemberCart(p2->getIdCode(),m->getAccountID());
   s->addProductToMemberCart(p1->getIdCode(),m->getAccountID());
   s->checkOutMember(m->getAccountID());
}

_______________________________________________________

Output

--- Search Result ---

Title: Macbook, IdCode: mac123, Price: $1122.5, Description:Macbook Air 15.5'

Title: Air Purifier, IdCode: air12, Price: $11.5, Description:Aqua Air Purifier Alpha'

--- Cart for CheckOut ---

Title: Macbook, Price: $1122.5

Title: Air Purifier, Price: $11.5

Sorry, product mac123, Macbook is no longer available.

Sub-Total: $1134, Shipping Cost: $79.38, Final Total Cost: $1213.38

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