UML diagram Store - products : list of Product - customers : list of Customer -
ID: 3914759 • Letter: U
Question
UML diagram
Store
- products : list of Product
- customers : list of Customer
- name
- getCustomer(customerID) : Customer
- getProduct(productID) : Product
+ takeShipment(productID, quantity, cost)
+ sellProduct(customerID, productID, quantity)
+ takePayment(customerID, amount)
+ listProducts() : String
+ listCustomers() : String
+ Store(name)
+ Store()
+ addProduct(productID, productName);
+ addCustomer(customerID, customerName, credit)
+ getName()
+ setName(newName)
Customer
-id
- name
- credit : true/false
- balance : dollars
+ Customer (customerID, customerName, credit)
+ getID()
+ getName()
+ setName(newName)
+ getCredit() : true/false
+ setCredit(credit : true/false)
+ getBalance()
+ processPayment(amount)
+ processPurchase(amount, product)
Product
- id
- name
- totalPaid
- inventory
- description : String
- productsPurchased : list of Product
- numSold
+ Product(productID, productName)
+ getDescription() : String
+ getPrice() : Dollars
+ addShipment(quantity, cost)
+ getInventoryCount() : quantity in inventory
+ setDescription(description)
+ reduceInventory(quantity)
+ getNumberSold() : quantity
+ getTotalPaid() : dollars
+ getID()
+ getName()
+ setName(newName)
____________________________________________________________
There are three diamonds pointing from
1: -products : list of Product to the class Product
2: - customers : list of Customer to the class Product
3:- productsPurchased : list of Product to Product
Write 3 classes .cpp files with header files .h: Product, Class, and Custome
Explanation / Answer
main.cpp
#include "Product.h"
#include "Customer.h"
#include "Store.h"
using namespace std;
int main ()
{
Product p (11, "Alex");
Product q;
p.setName("me");
p.setDescription("Good eggs");
p.addShipment(20000, 300);
//p.reduceInventory(200);
Customer d (15, "Sam", true);
d.setName("Sam");
d.setCredit(true);
d.processPayment(500);
d.processPurchase(600, p);
Store h ("Daniel");
h.addProduct(200,"Man");
h.addProduct(122,"food");
h.addCustomer(300,"Me", true);
h.getProduct(200).addShipment(20000,300);
cout<< "got the first"<<endl;
h.getProduct(122).addShipment(200,3);
cout<< "got the second"<<endl;
h.sellProduct(300,200,5);
h.sellProduct(300,122,15);
h.listProducts();
h.listCustomers();
}
Customer.cpp
#include "Customer.h"
using namespace std;
//helper function that overloads operator << to help print information
std::ostream& operator<<(std::ostream &os, const Customer& rhs)
{
string cree = "false";
if (rhs.getCredit() == 1){
cree = "true";
}
os << "Customer Name: " << rhs.getName() << ' '
<< "Customer ID: " << rhs.getID() << ' '
<< "Has Credit: " << cree << ' '
<< "Balance: " << rhs.getBalance() << ' '
<< "Products purchased -- " << ' '
<< ' '
<< rhs.getProductsPurchased() << ' ';
return os;
}
//default consturctor
Customer::Customer(){};
//constructor
Customer::Customer(int id, std::string name, bool credit):
name(name), id(id), credit(credit), balance (0) {
if (name.length()==0)
throw runtime_error("Customer name cannot be empty."); //runtime error for empty strings
}
int Customer::getID() const //function that gets Customer ID and returns it
{
return id;
}
string Customer::getName() const //function that gets Customer name and returns it
{
return name;
}
void Customer::setName(string name) //function sets the Customer name
{
if (name.length()==0)
throw runtime_error("Customer name cannot be empty."); //runtime error for empty strings
Customer::name = name;
}
bool Customer::getCredit() const // function that gets Customer credit and returns it
{
return credit;
}
void Customer::setCredit(bool hascredit) //function sets the customer credit
{
credit = hascredit;
}
double Customer::getBalance() const //function gets the Customer baalance and returns it
{
return balance;
}
void Customer::processPayment (double amount) //function processes payment using ammount and adds to balance
{
if (amount < 0)
{
throw runtime_error("Amount must be positive."); //Throws runtime error for negative amount
}
balance += amount;
}
void Customer::processPurchase(double amount, Product product) //processing the payment using vector of products
{
bool num = true;
int num2 = product.getID();
for (int i=0; i < productsPurchased.size(); i++) //loops through products
{
if (num2 == productsPurchased[i].getID())
{
num = false;
}
}
if (amount < 0)
{
throw runtime_error("Not enough amount."); //throws runtime error for negative amount
}
else if (credit == true){ //allows customer purchase given that they hae credit but not balance to purchase
balance -= amount;
if (num)
productsPurchased.push_back(product);
}
else {
if (balance >= amount) { // if customer has no credit
balance -= amount;
if (num)
productsPurchased.push_back(product);
}
else {
throw runtime_error("Not enough credit."); //throw runtime error when cutomer doesn't have enough balance and credit
}
}
}
string Customer::getProductsPurchased() //gets the products that haven been purchased using previous vector
const{
string pur;
stringstream pout;
for (int i=0; i < productsPurchased.size(); i++)
{
pout << productsPurchased.at(i) << endl;
}
pur = pout.str();
return pur;
}
Customer.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include "Product.h"
//datatypes
class Customer {
int id;
std::string name;
double balance;
std::vector<Product> productsPurchased;
bool credit;
//accessors
public:
Customer();
Customer(int customerID, std::string name, bool credit);
int getID() const;
std::string getName() const;
void setName(std::string name);
bool getCredit() const;
void setCredit(bool hasCredit);
double getBalance() const;
void processPayment(double amount );
void processPurchase (double amount, Product product);
std::string getProductsPurchased () const;
};
//helper function
std::ostream& operator <<(std::ostream &os, const Customer& rhs);
#endif
Product.cpp
#include "Product.h"
using namespace std;
//helper function that overloads operator << to help print information
std::ostream& operator<<(std::ostream &os, const Product& hs)
{
os << "Product Name: " << hs.getName() << ' '
<< "Product ID: " << hs.getID() << ' '
<< "Description: " << hs.getDescription() << ' '
<< "Inventory: " << hs.getInventoryCount() << ' '
<< "Total Paid: " << hs.getTotalPaid()<< ' ';
return os;
}
//default constructor
Product::Product(){};
//constructor
Product::Product(int id, string name) : id(id), name(name),
inventory(0), numSold(0), totalPaid(0.0), description("")
{
if (name.length()==0)
throw runtime_error("Product name cannot be empty."); //runtime error for empty strings
}
int Product::getID() const //function that gets Product ID and returns it
{
return id;
}
string Product::getName() const //function that gets Product name and returns it
{
return name;
}
void Product::setName(string productName)
{
if (productName.length()==0)
throw runtime_error("Product name cannot be empty."); //runtime error for empty strings
Product::name = productName;
//return an empty string
}
string Product::getDescription() const { //function that gets Product ddescription and returns it
return description;
}
void Product::setDescription(string description) //function that sets Product description
{
this->description = description;
}
int Product::getNumberSold() const //function gets the numher of products sold and returns it
{
return numSold;
}
double Product::getTotalPaid() const //function gets the number total paid and returns it
{
return totalPaid;
}
int Product::getInventoryCount() const //function gets the inventory count and returns it
{
return inventory;
}
double Product::getPrice()const //function calculates the price using a given formula and returns the price
{
double price = (totalPaid / (inventory + numSold)) * 1.25;
return price;
}
void Product::addShipment(int quantity, double shipmentCost) //function calculates shipment cost
{
if (quantity < 0 || (shipmentCost < 0) )
throw runtime_error("Shipment quantity and cost must be positive."); //throw runtime error for negative cost or quantity
inventory += quantity;
totalPaid += shipmentCost;
}
void Product::reduceInventory(int quantity) // fucntion reduces inventory when purchased is made
{
if (inventory < quantity)
{
throw runtime_error("Not enough inventory."); //throw runtime error if inventory is less than quantity
}
else if (quantity < 0)
{
throw runtime_error("Not enough quantity."); //throw runtime error if quantity is equal to zero
}
else
{
inventory -= quantity;
numSold += quantity;
}
}
Product.h
//header guards
#ifndef PRODUCT_H
#define PRODUCT_H
#include <iostream>
#include <string>
#include <stdexcept>
#include <sstream>
#include <vector>
//datatypes
class Product {
int id;
std::string name;
std::string description;
int inventory;
int numSold;
double totalPaid;
// accessors
public:
Product();
Product(int id, std::string);
int getID() const;
std::string getName() const;
void setName(std::string);
std::string getDescription() const;
void setDescription(std::string);
int getNumberSold() const;
double getTotalPaid() const;
int getInventoryCount() const;
double getPrice() const;
void addShipment(int, double);
void reduceInventory(int);
};
//helper function
std::ostream& operator <<(std::ostream &os, const Product& hs);
#endif
Store.cpp
#include "Store.h"
using namespace std;
#include <sstream>
//default consturctor
Store::Store(){};
//consturctor
Store::Store(string name): name (name){};
string Store::getName() const //function that gets Store name and returns it
{
return name;
}
void Store::setName(string newName) //function sets the Store name
{
this->name = newName;
}
//function adds a product, check if it already exists and if it doesn't pushses it into the product vector
void Store::addProduct(int productID, string productName)
{
for (int i = 0; i < products.size(); i++)
{
if (products.at(i).getID() == productID)
{
throw runtime_error("Product exists");
}
}
products.push_back(Product(productID, productName));
}
//function gets a product given its ID and checks if that ID exists by looping through product vector
Product& Store::getProduct(int productID)
{
bool find = false;
for (int i = 0; i < products.size(); i++)
{
if (products.at(i).getID()==productID)
{
find = true;
return products.at(i);
}
}
if (!find)
throw runtime_error("Product Not Found");
}
//function adds a customer, check if the customer already exists and if they doesn't pushses them into the customer vector
void Store::addCustomer(int customerID, string customerName, bool credit)
{
for (int i = 0; i < customers.size(); i++)
{
if (customers.at(i).getID() == customerID)
{
throw runtime_error("Customer exists");
}
}
customers.push_back(Customer(customerID, customerName, credit));
}
//function gets a customer given their ID and checks if that ID exists by looping through customer vector; overloaded function that sets customer credit to false by default
void Store::addCustomer(int customerID, string customerName)
{
for (int i = 0; i < customers.size(); i++)
{
if (customers.at(i).getID() == customerID)
{
throw runtime_error("Customer exists");
}
}
bool credit = false;
customers.push_back(Customer(customerID, customerName, credit));
}
//function gets a customer given their ID and checks if that ID exists by looping through customer vector
Customer& Store::getCustomer(int customerID)
{
bool find = false;
for (int i = 0; i < customers.size(); i++)
{
if (customers.at(i).getID()==customerID)
{
find = true;
return customers.at(i);
}
}
if (!find)
throw runtime_error ("Customer not found");
}
//fucntion allows a sale by using processPurchase from customer class and reduce inventory from product class; changes quanitity into amount so function parameters are valid
void Store::sellProduct(int customerID, int productID, int quantity)
{
getProduct(productID).reduceInventory(quantity);
amount = getProduct(productID).getPrice() * quantity;
getCustomer(customerID).processPurchase(amount, getProduct(productID));
}
//function take product ID and finds it quantity and shipment cost; uses getProduct to catch its error
void Store::takeShipment(int productID, int quantity, double cost)
{
getProduct(productID).addShipment(quantity, cost);
}
//finds the matching customer and processes their payment
void Store::takePayment(int customerID, double amount)
{
getCustomer(customerID).processPayment(amount);
}
//outputs information about products; uses the overloaded opertaor
void Store::listProducts() const
{
for (int i = 0; i < products.size(); i++)
{
cout << products.at(i)<< endl;
}
}
//outputs information about customers; uses overloaded opertaor
void Store::listCustomers() const
{
for (int i = 0; i < customers.size(); i++)
{
cout << customers.at(i)<< endl;
}
}
//allows the listProducts to be returned as string
string Store::listProducts()
{
ostringstream os;
if(products.size() == 0)
{
os << "No Products" << endl;
}
else
{
for(int i = 0; i < products.size(); i++)
{
Product product = products.at(i);
os << products.at(i) << endl;
cout << products.at(i) << endl;
}
}
return os.str();
}
//allows the listCustomer to be returned as string
string Store::listCustomers()
{
ostringstream os;
if(customers.size() == 0)
{
os << "No Customer" << endl;
}
else
{
for(int i = 0; i < customers.size(); i++)
{
Customer customer = customers.at(i);
os << customers.at(i) << endl;
cout << customers.at(i) << endl;
}
}
return os.str();
}
Store.h
//header guards
#ifndef STORE_H
#define STORE_H
#include "Product.h"
#include "Customer.h"
class Store {
//datatypes
std::string name;
std::vector<Product> products;
std::vector<Customer> customers;
double cost;
double amount;
//functions
public:
Store();
Store(std::string name);
std::string getName() const;
void setName(std::string name);
void addProduct(int productID, std::string productName);
Product& getProduct(int productID);
void addCustomer(int customerID, std::string customerName, bool credit);
void addCustomer(int customerID, std::string customerName);
Customer& getCustomer(int customerID);
void takeShipment(int productID, int quantity, double cost);
void sellProduct(int customerID, int productID, int quantity);
void takePayment(int customerID, double amount);
std::string listProducts();
std::string listCustomers();
void listProducts() const;
void listCustomers() const;
};
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.