Question

In: Computer Science

Online Store Simulator cpp files needed hpp files provided You will be writing a (rather primitive)...

Online Store Simulator cpp files needed hpp files provided

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 three files are below the questions;

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

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"):

red blender
ID code: 123
price: $350
sturdy blender perfect for making smoothies and sauces

hot air balloon
ID code: 345
price: $700
fly into the sky in your own balloon - comes in red, blue or chartreuse


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

giant robot - $7000
Sorry, product #345, "live goat", is no longer available.
oak and glass coffee table - $250
Subtotal: $7250
Shipping Cost: $0
Total: $7250

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.

customer.hpp

#ifndef CUSTOMER_HPP
#define CUSTOMER_HPP

#include
#include "Product.hpp"

class Customer
{
private:
std::vector cart;
std::string name;
std::string accountID;
bool premiumMember;
public:
Customer(std::string n, std::string a, bool pm);
std::string getAccountID();
std::vector getCart();
void addProductToCart(std::string);
bool isPremiumMember();
void emptyCart();
};

#endif

product.hpp

#ifndef PRODUCT_HPP
#define PRODUCT_HPP

#include

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

store.hpp

#ifndef STORE_HPP
#define STORE_HPP

#include
#include "Customer.hpp"

class Store
{
private:
std::vector inventory;
std::vector 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

Solutions

Expert Solution

Solution:

1. Product.cpp:

------------------------------------------

#include "Product.hpp"

//Constructor
Product::Product(std::string id, std::string t, std::string d, double p,
       int qa) {
   this->idCode=id;
   this->title=t;
   this->description=d;
   this->price=p;
   this->quantityAvailable=qa;
}

//returns idCode
std::string Product::getIdCode() {
   return this->idCode;
}

//return title
std::string Product::getTitle() {
   return this->title;
}

//returns description
std::string Product::getDescription() {
   return this->description;
}

//return price
double Product::getPrice() {
   return this->price;
}

//returns quantity available
int Product::getQuantityAvailable() {
   return this->quantityAvailable;
}

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

------------------------------------------------

2. Customer.cpp:

------------------------------------------------------

#include "Customer.hpp"

//Constructor
Customer::Customer(std::string n, std::string a, bool pm) {
   this->name=n;
   this->accountID=a;
   this->premiumMember=pm;
}

//return account id
std::string Customer::getAccountID() {
   return this->accountID;
}

//returns cart
std::vector<std::string> Customer::getCart() {
   return this->cart;
}

//adds a product to cart
void Customer::addProductToCart(std::string prod) {
   this->cart.push_back(prod);
}

//returns whether a member is a premium member or not
bool Customer::isPremiumMember() {
   return this->premiumMember;
}

//empties cart
void Customer::emptyCart() {
   this->cart.clear();
}

---------------------------------------

3. Store.cpp:

---------------------------------------

#include "Store.hpp"

//adds a product to the inventory
void Store::addProduct(Product* p) {
   this->inventory.push_back(p);
}

//adds a customer to the members
void Store::addMember(Customer* c) {
   this->members.push_back(c);
}

//returns a product with matching ID or NULL;
Product* Store::getProductFromID(std::string prod) {
   Product *p=NULL;
   std::vector<Product*>::iterator it;
   for(it=inventory.begin();it!=inventory.end();++it)
   {
       p=*it;
       if(p->getIdCode().compare(prod)==0)
           break;
   }
   return p;
}

//returns a member with matching id
Customer* Store::getMemberFromID(std::string mem) {
   Customer *c=NULL;
       std::vector<Customer*>::iterator it;
       for(it=members.begin();it!=members.end();++it)
       {
           c=*it;
           if(c->getAccountID().compare(mem)==0)
               break;
       }
       return c;
}

//lists products with matching title
void Store::productSearch(std::string str) {
   Product *p=NULL;
   std::vector<Product*>::iterator it;
   for(it=inventory.begin();it!=inventory.end();++it)
   {
       p=*it;
       if(p->getTitle().find(str)!=std::string::npos)
           std::cout<<std::endl<<"Title:"<<p->getTitle()<<std::endl
                   <<"ID code:"<<p->getIdCode()<<std::endl
                   <<"Price:"<<p->getPrice()<<std::endl
                   <<"Description:"<<p->getDescription()<<std::endl;
   }

}

//adds a product to member's cart
void Store::addProductToMemberCart(std::string pID, std::string mID) {
   Product *p;
   Customer *c;
   p=getProductFromID(pID);
   if(p==NULL)
   {
       std::cout<<"Product #["<<pID<<"] not found."<<std::endl;
   }
   else
   {
       c=getMemberFromID(mID);
       if(c==NULL)
       {
           std::cout<<"Member #["<<mID<<"] not found."<<std::endl;
       }
       else
       {
           if(p->getQuantityAvailable()>0)
           {
               c->addProductToCart(p->getIdCode());
           }
           else
           {
               std::cout<<"Sorry, product #["<<pID<<"] is currently out of stock."<<std::endl;
           }
       }
   }
}

//checks out a member
void Store::checkOutMember(std::string mID) {
   Customer *c=getMemberFromID(mID);
   double subTotal=0.0;
   if(c==NULL)
   {
       std::cout<<"Member #["<<mID<<"] not found."<<std::endl;
   }
   else
   {
       std::vector<std::string> cart=c->getCart();
       if(cart.size()==0)
           std::cout<<"There are no items in the cart."<<std::endl;
       else
       {
           std::vector<Product*>::iterator it;
           Product *p;
           for(it=inventory.begin();it!=inventory.end();++it)
           {
               p=*it;
               std::cout<<std::endl<<"Title:"<<p->getTitle()<<" "
                           <<"Price:"<<p->getPrice()<<std::endl;
               if(p->getQuantityAvailable()>0)
               {
                   p->decreaseQuantity();
                   subTotal+=p->getPrice();
               }
               else
                   std::cout<<"Sorry, product #["<<p->getIdCode()<<"], ["<<p->getTitle()<<"], is no longer available"<<std::endl;
           }
           std::cout<<"Sub Total:"<<subTotal<<std::endl;
           double shippingCost=0.0;
           if(c->isPremiumMember())
               shippingCost=0.0;
           else
           {
               shippingCost=(7.0*subTotal)/100.0;
           }
           double totalCost=0.0;
           totalCost=subTotal+shippingCost;
           std::cout<<"Total cost:"<<totalCost<<std::endl;
           c->emptyCart();
       }
   }
}

----------------------------------------------------

4. Updated Store.hpp:

-------------------------------------------------------

#ifndef STORE_HPP_
#define STORE_HPP_

#include <iostream>
#include <vector>
#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 prod);
   Customer* getMemberFromID(std::string mem);
   void productSearch(std::string str);
   void addProductToMemberCart(std::string pID, std::string mID);
   void checkOutMember(std::string mID);
};

#endif /* STORE_HPP_ */

-------------------------------------------------------

5. Updated Customer.hpp:

---------------------------------------------------

#ifndef CUSTOMER_HPP_
#define CUSTOMER_HPP_

#include <iostream>
#include <cstring>
#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 prod);
   bool isPremiumMember();
   void emptyCart();
};

#endif /* CUSTOMER_HPP_ */

----------------------------------------------------

6. main() function:

-----------------------------------------------

#include <iostream>
#include "Store.hpp"

using namespace std;

int main() {
   Store store;
   Product *p1 = new Product("123","red blender","sturdy blender perfect for making smoothies and sauces",350,20);
   store.addProduct(p1);
   store.productSearch("red");
   return 0;
}

--------------------------

Output:

-----------------------------

Title:red blender
ID code:123
Price:350
Description:sturdy blender perfect for making smoothies and sauces

--------------------------------------


Related Solutions

Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428 Create : DieselElectric.cpp DieselElectric.h DieselElectric dieselElectric -fuelSupply:int --------------------------- +getSupply():int +setSupply(s:int):void +dieselElectric(f:int) +~dieselElectric() +generateID():string +calculateRange():double The class variables are as follows: fuelSuppply: The fuel supply of the train in terms of a number of kilolitres...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main file. You will practice writing class constants, using data files. You will add methods public and private to your BankAccount class, as well as helper methods in your main class. You will create an arrayy of objects and practice passing objects to and return objects from functions. String functions practice has also been included. You have been given a template in Zylabs to help...
Code needed in C++, make changes to the file provided (18-1, has 3 files) Chapter 18...
Code needed in C++, make changes to the file provided (18-1, has 3 files) Chapter 18 Stacks and Queues ----------------------------------------------------------------------------------------------------- capacity is just 5 1. push 6 numbers on the stack 2. catch the overflow error in a catch block 3. pop one element, which means your capacity is now down to 4 4. push the element that was rejected earlier 5. verify your entire stack by popping to show the new numbers. IntStack.h #include <memory> using namespace std; class...
C++ question. Need all cpp and header files. Part 1 - Polymorphism problem 3-1 You are...
C++ question. Need all cpp and header files. Part 1 - Polymorphism problem 3-1 You are going to build a C++ program which runs a single game of Rock, Paper, Scissors. Two players (a human player and a computer player) will compete and individually choose Rock, Paper, or Scissors. They will then simultaneously declare their choices and the winner is determined by comparing the players’ choices. Rock beats Scissors. Scissors beats Paper. Paper beats Rock. The learning objectives of this...
You will be writing a program to calculate the cost of an online movie order. Write...
You will be writing a program to calculate the cost of an online movie order. Write a program that calculates and prints the cost of viewing something through the CNR Cable Company’s on demand. The company offers three levels of viewing: TV, Movies that are New Releases and Movies (not considered new releases). Its rates vary depending on what is to be viewed. The rates are computed as follows: TV: Free New Releases: 6.99 Movies (not new releases): The cost...
Situation - You are operating an online jewellery store. Problems that you are facing - 1....
Situation - You are operating an online jewellery store. Problems that you are facing - 1. Competition from other online jewellery stores. 2. Cybersecurity (protecting yourself and your customers) 3. Completing orders since there are shipping delays (ensure customers receive orders on time) For each problem listed above, what would be the symptoms and what would be the underlying problem?
This Homework would have two python files for a cheap online ticket seller. (You are free...
This Homework would have two python files for a cheap online ticket seller. (You are free to imagine a new scenario and change any part of the question.) The main purpose of the homework is to use basic concepts. You should try to use basic python elements like variables, if-else, loops, lists, dictionary, tuple, functions in this homework. *** Price list to calculate the price for various destinations--- New York : Price For Delta $ 200.00 (Economy), 300.00(Business), 400(First class)...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain account numbers (10 digits) along with the account type (L:Loan and S:Savings) and their current balance. The second file (transactions.txt) will contain transactions on the accounts. It will specifically include the account number, an indication if it is a withdrawal/deposit and an amount. Both files will be pipe delimited (|) and their format will be the following: accounts.txt : File with bank account info...
Consider that you visit an online store you have visited before. You quickly notice a “welcome...
Consider that you visit an online store you have visited before. You quickly notice a “welcome back” message at the top of your page. You make a purchase: a fine set of grilling utensils. The next time you open your browser, ads shown in the sidebar included gas grills and accessories as well as deals from local butcher shops. Similar ads begin to appear on your social media pages and other websites you often visit. In addition, emails offering subscriptions...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT