Question
Store Backend - Part 2 Objectives Use Return by Reference. Use reuse by creating and using private helper functions. Work with a collection of a
Store Backend - Part 2
Objectives
Use Return by Reference.
Use reuse by creating and using private helper functions.
Work with a collection of a user-defined datatype.
Deal with keeping data consistent if exceptions are encountered.
Work with multiple classes.
Submission
Design: Submit the PDF to eCampus.
Source Code: Submit the source code (Customer.cpp , Customer.h , Product.cpp, Product.h, Store.cpp and Store.h files) to Vocareum Note: you do not have to upload the file with the main() function. However, if you do, it will not affect grading. We will use one with our test cases when we compile the classes you provide.
Program
You will create a backend for organizing data for a store. The program will span two homeworks. This weeks homework will be based on this UML Diagram. You will create the classes. Rather than testing a program, the autograder will use your classes in a program used for grading. However, to develop code you will need to create a "driver program(s) that you can run to develop and test your code. In the driver program, you can create sequences of statements to test your classes. The driver program will be similar to what you did in part 1.
Specifications
Based on the UML Diagram, update the Store class.
Include all attributes / data members as indicated in the UML Diagram.
Implement the constructors and the methods / member functions listed below.
Design
A lot of the design has been done for you in the UML Class Diagram provided.
Outline your steps for creating a driver program for testing the classes.
Create test cases to ensure the class behaves correctly when created and or updated. Think about how you can use and set up the different classes to test the methods in your store.
Separate Files
Each class should be in a separate file with its own headerfile. By convention, the class name matches the name of the source (.cpp) and header (.h) files. Do not forget to use header guards.
const
Think about which member functions should not change the objects. Those member function declarations should be modified and marked as const for all classes.
Store Class
If invalid customerIDs or productIDs are passed into any function, it should throw an exception. Note this could be by calling getProduct() or getCustomer() with the invalid identifiers.
Store();
Store(string name);
string getName();
void setName(string name);
void addProduct(int productID, string productName); Create a new Product and add it to products. If this productID already belongs to another product, throw an exception.
Product& getProduct(int productID);
Find the matching product and return it. Be sure that the product can be modified so that the changes persist in the store's vector. If the product does not exist throw an exception.
void addCustomer(int customerID, string customerName, bool credit=false); Create a new Customer and add it to customers. If this customerID already belongs to another customer, throw an exception. If an argument is not provided for the credit parameter, set it false by default.
Customer& getCustomer(int customerID);
Find the matching customer and return it. Be sure that the customer can be modified so that the changes persist in the store's vector. If the customer does not exist throw an exception.
void takeShipment(int productID, int quantity, double cost);
Find matching Product. If product is not in list of products throw an exception. Otherwise, update product with the shipment quantity and cost.
void sellProduct(int customerID, int productID, int quantity);
Make the sale if it is allowed, otherwise throw an exception or let an exception propagate. In this context allowed means that the product's reduceInventory() and the customer's processPurchase() functions would not throw an exception and the product and customer must exist. Warning: Do not change the product or customer if both cannot be done successfully.
void takePayment(int customerID, double amount);
Find matching customer and process the payment. If the customer does not exist, you should throw an exception or let an exception propagate that is thrown when you call another function to get the customer.
void listProducts();
Output information about each product. (Use overloaded output operator for Product.)
void listCustomers();
Output information about each customer. (Use overloaded output operator for Customer.)
Here is the code its based off of. I cant figure out this part. If you can edit my code to complete this assignment that would be much appreciated.
Customer.cpp
#include "Customer.h"
#include
#include
#include
#include
#include
#include "Product.h"
#include
#include
using namespace std;
Customer::Customer(int ID, std::string name, bool credit){
if(name != ""){
this->name = name;
id = ID;
this->credit = credit;
this->balance = 0;
}
else{
throw runtime_error("Error");
}
}
int Customer::getID() const{
return id;
}
std::string Customer::getName() const{
return name;
}
void Customer::setName(std::string name){
if(name != ""){
this->name = name;
}
else{
throw runtime_error("Error");
}
}
bool Customer::getCredit() const{
return credit;
}
void Customer::setCredit(bool val){
credit = val;
}
double Customer::getBalance() const{
return balance;
}
void Customer::processPayment(double amount){
if(amount < 0){
throw runtime_error("Error");
}else
{
this->balance += amount;
}
}
void Customer::processPurchase(double amount, Product product){
if(amount <0){
throw runtime_error("Error");
}
else if(credit){
balance -= amount;
}
else if(!credit && (balance >= amount)){
balance -= amount;
}
else{
throw runtime_error("Error");
}
int repeat = 0;
for(int i = 0; i
if(productsPurchased.at(i).getID() == product.getID()){
repeat++;
}
}
if(repeat == 0){
productsPurchased.push_back(product);
}
}
std::string Customer::getProductsPurchased() const{
ostringstream os;
for(int i = 0; i
os << productsPurchased.at(i) << std::endl;
}
std::string info = os.str();
return info;
}
ostream& operator<< (std::ostream& o, const Customer& c){
o << "Customer Name: " << c.getName() << endl;
o << "Customer ID: " << c.getID() << endl;
if (c.getCredit() == true){
o << "Has Credit: true" << endl;
}
else {
o << "Has Credit: false" << endl;
}
o << "Balance: " << c.getBalance() << endl;
o << "Products purchased -- " << endl << endl << c.getProductsPurchased() << endl;
return o;
}
Customer.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include
#include
#include
#include
#include "Product.h"
#include
#include
//////////////////////////////////////////////////////////////////////////////
// Color Class Definition //
//////////////////////////////////////////////////////////////////////////////
class Customer {
/* Public */
public:
Customer();
Customer(int ID, std::string customerName, bool credit);
std::string to_str() const;
int getID(void) const;
std::string getName(void) const;
void setName(std::string);
bool getCredit() const;
void setCredit(bool);
double getBalance() const;
void processPayment(double);
void processPurchase(double, Product);
std::string getProductsPurchased() const;
/* Private */
private:
int id;
std::string name;
bool credit;
double balance;
std::vector productsPurchased;
};
//////////////////////////////////////////////////////////////////////////////
// Helper Function Declarations //
//////////////////////////////////////////////////////////////////////////////
std::ostream& operator<<(std::ostream&, const Customer&);
#endif
Product.cpp
#include "Product.h"
#include
#include
#include
#include
#include
#include
using namespace std;
ostream& operator<< (ostream& o, const Product& p){
o << "Product Name: " << p.getName() << endl;
o << "Product ID: " << p.getID() << endl;
o << "Description: " << p.getDescription() << endl;
o << "Inventory: " << p.getInventoryCount() << endl;
o << "Total Paid: " << p.getTotalPaid() << endl;
return o;
}
Product::Product(){
}
Product::Product(int productID, std::string name)
{
id = productID;
if(name != ""){
this->name = name;
this->inventory = 0;
this->numSold = 0;
this->totalPaid = 0.0;
this->description = "";
}
else{
throw runtime_error("Error");
}
}
int Product::getID() const{
return id;
}
std::string Product::getName() const{
return name;
}
std::string Product::getDescription() const{
return description;
}
std::string Product::setName(std::string productName){
if(productName == ""){
throw runtime_error("Error");
}else{
this->name = productName;}
return "";
}
void Product::setDescription(std::string description){
this->description = description;
}
int Product::getNumberSold() const{
return numSold;
}
double Product::getTotalPaid() const{
return totalPaid;
}
int Product::getInventoryCount() const{
return inventory;
}
void Product::addShipment(int shipmentQuantity, double shipmentCost){
if(shipmentQuantity < 0 || shipmentCost < 0){
throw runtime_error("Error");
}
else{
inventory += shipmentQuantity;
totalPaid += shipmentCost;
}
}
void Product::reduceInventory(int purchaseQuantity){
if(purchaseQuantity <0){
throw runtime_error("Error");
}
if(inventory < purchaseQuantity)
{
throw runtime_error("Error");
}
else{
inventory -= purchaseQuantity;
numSold += purchaseQuantity;
}
}
double Product::getPrice() const{
double price = (totalPaid / (inventory + numSold)) * 1.25;
return price;
}
Product.h
#ifndef PRODUCT_H
#define PRODUCT_H
#include
#include
#include
#include
#include
//////////////////////////////////////////////////////////////////////////////
// Color Class Definition //
//////////////////////////////////////////////////////////////////////////////
class Product {
/* Public */
public:
Product();
Product(int productId, std::string productName);
std::string to_str() const;
int getID() const;
std::string getName() const;
std::string setName(std::string productName);
std::string getDescription() const;
void setDescription(std::string description);
int getNumberSold() const;
double getTotalPaid() const;
int getInventoryCount() const;
void addShipment(int shipmentQuantity, double shipmentCost);
void reduceInventory(int purchaseQuantity);
double getPrice() const;
/* Private */
private:
int id;
std::string name;
std::string description;
int inventory;
int numSold;
double totalPaid;
};
//////////////////////////////////////////////////////////////////////////////
// Helper Function Declarations //
//////////////////////////////////////////////////////////////////////////////
std::ostream& operator<<(std::ostream&, const Product&);
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started