(PLEASE WRITE IN BASIC C++ CODE, DO NOT USE #include <algorithm>) You will be wr
ID: 3730268 • Letter: #
Question
(PLEASE WRITE IN BASIC C++ CODE, DO NOT USE #include <algorithm>)
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 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
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
here is your file without algorithm included : ------------>>>>>>>>>>
#ifndef STORE_HPP
#define STORE_HPP
#include <vector>
#include <string>
#include "cutomer.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
void Store::addProduct(Product *p){
inventory.push_back(p);
}
void Store::addMember(Customer *c){
members.push_back(c);
}
Product* Store::getProductFromID(std::string id){
for(int i = 0;i<inventory.size();i++){
if(inventory[i]->getID() == id){
return inventory[i];
}
}
return NULL;
}
Customer* Store::getMemberFromID(std::string id){
for(int i = 0;i<members.size();i++){
if(members[i]->getAccountID() == id){
return members[i];
}
}
return NULL;
}
std::vector<std::string> Store::productSearch(std::string str){
//for changing to lower case
for(int i = 0;i<str.length();i++){
if((int)(str[i]) < 97){
str[i] = (char)((int)str[i] + 32);
}
}
std::vector<std::string> result;
std::string t_d;
for(int i = 0;i<inventory.size();i++){
t_d = "";
t_d += inventory[i]->getTitle();
t_d += inventory[i]->getDescription();
//for changing to lower case
for(int i = 0;i<t_d.length();i++){
if((int)(t_d[i]) < 97){
t_d[i] = (char)((int)t_d[i] + 32);
}
}
if(t_d.find(str) != std::string::npos){
result.push_back(inventory[i]->getID());
}
}
//for sorting
string temp;
temp.reserve(20);
for(int i = 2;i<result.size();i++){
for(int j = i;j>0;j--){
if(result[j] < result[j-1]){
temp = result[j];
result[j] = result[j-1];
result[j-1] = temp;
}
}
}
return result;
}
std::string Store::addProductToMemberCart(std::string pId,std::string mId){
Product *pt = getProductFromID(pId);
Customer *ct = getMemberFromID(mId);
string status = "";
if(pt == NULL){
status += "Product Id Not Found";
return status;
}
if(ct == NULL){
status += "Customer Id Not Found";
return status;
}
if(pt->getQuantity() <= 0){
status += "Product Out Of Stock";
return status;
}
status += pt->getID();
ct->addProductToCart(status);
pt->decreaseQuantity();
status.reserve(40);
status = "product added to cart";
return status;
}
double Store::checkOutMember(std::string mId){
Customer *ct = getMemberFromID(mId);
if(ct == NULL){
return -1;
}
double cost = 0;
std::vector<std::string> str = ct->getCart();
for(int i = 0;i<str.size();i++){
Product *pt = getProductFromID(str[i]);
if(pt == NULL){
continue;
}else{
if(pt->getQuantity() <= 0){
continue;
}else{
cost = cost + pt->getPrice();
}
}
}
cost = cost + 40.0;//40 shipping cost you can change
return cost;
}
/////////////////////////////////////
#ifndef CUSTOMER_HPP
#define CUSTOMER_HPP
#include <vector>
#include<iostream>
#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
Customer::Customer(std::string n,std::string a,bool pm){
name = n;
accountID = a;
premiumMember = pm;
}
std::string Customer::getAccountID(){
return accountID;
}
std::vector<std::string> Customer::getCart(){
return cart;
}
bool Customer::isPremiumMember(){
return premiumMember;
}
void Customer::addProductToCart(std::string itemid){
cart.push_back(itemid);
}
void Customer::emptyCart(){
cart.clear();
}
///////////////////////////////////////////////
#ifndef _PRODUCT_H
#define _PRODUCT_H
#include<iostream>
using namespace std;
class Product{
string ID;
string title;
string desc;
double price;
int quant;
public:
Product(string,string,string,double,int);
string getID();
string getTitle();
string getDescription();
double getPrice();
int getQuantity();
void decreaseQuantity();
};
#endif
Product::Product(string id,string t,string d,double p,int q){
ID = id;
title = t;
desc = d;
price = p;
quant = q;
}
string Product::getDescription(){
return desc;
}
string Product::getID(){
return ID;
}
string Product::getTitle(){
return title;
}
double Product::getPrice(){
return price;
}
int Product::getQuantity(){
return quant;
}
void Product::decreaseQuantity(){
if(quant <= 0){
return;
}
quant--;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.