PLEASE DO NOT USE #include , #include , #include , or .reserve() in any .cpp or
ID: 3730467 • Letter: P
Question
PLEASE DO NOT USE #include , #include , #include , or .reserve() in any .cpp or .hpp file
PLEASE exactly copy the .hpp files member variables and functions to create the .cpp files
PLEASE write in basic c++ code
PLEASE include comments (especially for Store.cpp)
PLEASE include a main function file for testing purposes
*************************************************************************************************************************************
PLEASE PLEASE read the comments above before answering.
Assignment:
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.
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 <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
//main.cpp
#include "Store.hpp"
#include <iostream>
using namespace std;
int main()
{
Store market;
market.addMember(new Customer("Bob", "555", true));
market.addMember(new Customer("Mary", "777", false));
market.addMember(new Customer("Jill", "000", true));
market.addProduct(new Product("123", "Peaches", "a fuzzy fruit", 1.50, 3));
market.addProduct(new Product("321", "Snickers", "chocolate bar", 1.00, 1));
market.addProduct(new Product("111", "Coke", "A nice refreshing soda", 3.00, 0));
market.productSearch("Coke");
market.productSearch("bar");
market.productSearch("FRUIT");
market.addProductToMemberCart("321", "000");
market.addProductToMemberCart("123", "777");
market.addProductToMemberCart("123", "777");
market.addProductToMemberCart("123", "777");
market.addProductToMemberCart("111", "777");
market.addProductToMemberCart("123", "222");
market.addProductToMemberCart("456", "000");
market.checkOutMember("000");
market.checkOutMember("333");
market.checkOutMember("777");
market.checkOutMember("777");
}
-------------------------------------------------------------------
//Customer.cpp
#include "Customer.hpp"
#include <iostream>
using std::string;
Customer::Customer(string n, string a, bool pm)
{
name = n;
accountID = a;
premiumMember = pm;
}
string Customer::getAccountID()
{
return accountID;
}
std::vector<string> Customer::getCart()
{
return cart;
}
//adds the product to the customer's cart
void Customer::addProductToCart(string product)
{
cart.push_back(product);
}
bool Customer::isPremiumMember()
{
return premiumMember;
}
//empties the customer's cart
void Customer::emptyCart()
{
cart.clear();
}
------------------------------------------------------------------
//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
-----------------------------------------------------------
//Product.cpp
#include "Product.hpp"
#include <iostream>
using std::string;
Product::Product(string id, string t, 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;
}
//decreasing the quantity available of the this product in the inventory
void Product::decreaseQuantity()
{
quantityAvailable--;
}
-----------------------------------------------------------
//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();
};
---------------------------------------------
//Store.cpp
#include "Store.hpp"
#include <iostream>
#include <vector>
using std::string;
using std::cout;
using std::endl;
string toUpper(string);
//adds a product to the store's inventory
void Store::addProduct(Product* p)
{
inventory.push_back(p);
}
// adds a customer to the store's member list
void Store::addMember(Customer* c)
{
members.push_back(c);
}
Product* Store::getProductFromID(string pId)
{
for (int i = 0; i < inventory.size(); i++) //seaches for product in inventory
{
if (inventory.at(i)->getIdCode() == pId)
return inventory.at(i);
}
return NULL;
}
Customer* Store::getMemberFromID(string mId)
{
for (int i = 0; i < members.size(); i++) //searches for member in member list
{
if (members.at(i)->getAccountID() == mId)
return members.at(i);
}
return NULL;
}
// uses a given string to seach for a product containing the given string in its title or descpriton and prints out its attributes
void Store::productSearch(string str)
{
for (int i = 0; i < inventory.size(); i++) //scans through entire inventory
{
Product* p = inventory.at(i); //gets the product in inventory at the current location of loop
//converts all strings(product title and description, and string to search)
string titleCopy = toUpper(p->getTitle());
string descriptionCopy = toUpper(p->getDescription());
str = toUpper(str);
size_t foundInTitle = titleCopy.find(str);
size_t foundInDesc = descriptionCopy.find(str);
if (foundInTitle != std::string::npos || foundInDesc != std::string::npos) //if the string to search has been found
{
cout << p->getTitle()
<< " ID code: " << p->getIdCode()
<< " Price: " << p->getPrice()
<< " " << p->getDescription() << endl;
cout << " ";
}
}
}
//adds a prodcut to a member's cart
void Store::addProductToMemberCart(string pID, string mID)
{
Product* pro = getProductFromID(pID);
Customer* cust = getMemberFromID(mID);
if (pro == NULL)
{
cout << "Product #" << pID << " not found" << endl;
return;
}
else if (cust == NULL)
{
cout << "Member #" << mID << " no found" << endl;
return;
}
else if (pro->getQuantityAvailable() == 0)
{
cout << "Sorry, product #" << pID << " is currently out of stock" << endl;
return;
}
else
cust->addProductToCart(pro->getIdCode());
}
//checkout a member's cart
void Store::checkOutMember(string mID)
{
Customer* cust = getMemberFromID(mID);
if (cust == NULL)
{
cout << "Member #" << mID << " not found" << endl;
return;
}
else
{
std::vector<string> crt = cust->getCart();
if (crt.size() == 0) return; //makes customer's cart is not empty
double totalPrice = 0.0;
cout << " ";
for (int i = 0; i < crt.size(); i++) //scans through entire customers cart
{
Product* pro = getProductFromID(crt.at(i)); //gets the product in customer's cart at the current location of loop
if (pro->getQuantityAvailable() == 0)
cout << "Sorry product #" << pro->getIdCode() << ", " << pro->getTitle() << " is no longer available" << endl;
else //product available
{
pro->decreaseQuantity(); //decreases the amount of products left
cout << pro->getTitle() << " - $" << pro->getPrice() << endl;
totalPrice += pro->getPrice(); //increases the total price of the customer's cart
}
}
cout << "Subtotal: $" << totalPrice << endl;
double shippingCost;
if (cust->isPremiumMember()) //finds out of customer is a premium member
shippingCost = 0.0;
else
shippingCost = totalPrice * 0.07;
cout << "Shipping Cost: $" << shippingCost << endl;
cout << "Total: $" << totalPrice + shippingCost << endl;
cust->emptyCart();
}
}
// Converts string into all uppercase
string toUpper(string arrayString)
{
string convertedString = "";
char letter;
for (int stringIndex = 0; stringIndex < arrayString.length(); stringIndex++)
{
letter = arrayString[stringIndex];
letter = toupper(letter);
convertedString += letter;
}
return convertedString;
}
-----------------------------------------------------
//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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.