Menu

(Solved) : Consider Class Hierarchy Last Page Assignment Class Hierarchy Food Products Including Fres Q34229371 . . .

Consider the class hierarchy on the last page of thisassignment. It is a class hierarchy of food products, includingFreshVegetable and CannedItem.

A Package is also a product and is a container of multipleproducts.

• We wish to use the visitor pattern to implement two operationson the product hierarchy.

• The first operation is to find the cheapest item in a product,which we implement using the CheapestVisitor.

• The second operation is to reduce the price of CannedItem andFreshVegetable items in a product by specified amounts.

We implement this using the ReducePriceVisitor.

You are provided with the main program main.cpp and thecompleted header file for the Product hierarchy.

Note that the Package is implemented using the vector class fromthe c++ stl library.

The ProductVisitor hierarchy is partially implemented inProductVisitor.h.

You need to complete the implementation of the CheapestVisitorand ReducePriceVisitor classes.

You are also required to implement the following methods in thefile called ProductVisitor.cpp:

1. void ProductVisitor::visit(Package *p)

2. void CheapestVisitor::visit(FreshVegetable *p)

3. void ReducePriceVisitor::visit(FreshVegetable *p)

4. void ReducePriceVisitor::visit(CannedItem *p)

5. double CheapestVisitor::getMinPrice() // return the price ofthe cheapest item

6. Item *CheapestVisitor::getMinItem() // return a pointer tothe cheapest item

7. void CheapestVisitor::reset() // reset the CheapestVisitorbefore finding another cheapest item

• All points where implementation is required are marked by thecomment TO BE COMPLETED

• Compile the code using cc -o product.exe main.cppProductVisitor.cpp

• There are two test functions in main.cpp – change main() totry each one.

Package accept(ProductVisitor ) Product Item FreshVegetable accept(ProductVisitor *) Cannedltem accept(ProductVisitor ) CheapestVisitor visit(Cannedltem *) visit(FreshVegetable *) visit(Package) ProductVisitor ReducePriceVisitor visit(Cannedltem *) visit(FreshVegetable *) visit(Package*)

———————————————————————————-main.cpp——————————————————————————————————————————

#include “Product.h”
#include “ProductVisitor.h”

#include <iostream>
using namespace std;

void test1(double freshVegReduction, doublecannedItemReduction)
{
// Declare a couple of fresh vegetables and
// a canned item, giving their name and their price.
FreshVegetable carrot(“carrot”, 50.0), peas(“peas”, 60.0),parsnips(“parsnips”, 55.0);
CannedItem mushyPeas(“mushyPeas”, 80.0), bakedbeans(“bakedBeans”,100.0);
  
// Declare a package to contain multiple products
Package pack1(“package1”);

// Add products to the package
pack1.addProduct(&carrot);
pack1.addProduct(&peas);
pack1.addProduct(&bakedbeans);

// The Cheapest Visitor calculates the price of thecheapest
// item in the package
CheapestVisitor cheap;
pack1.accept(&cheap);

cout << “The cheapest item is “
<< cheap.getMinItem()->getName() << ” at price”
<< cheap.getMinPrice() << ” rupies.” << endl;

// The ReducePriceVisitor takes two arguments – a percentage(0.80) by
// which to reduce the price of FreshVegetable products and
// a percentage (0.50) by which to reduce CannedItem products

ReducePriceVisitor priceModifier(freshVegReduction,cannedItemReduction);
pack1.accept(&priceModifier);

// Use CheapestVisitor to re-calculate price of cheapestitem

cheap.reset(); // re-set to compute a new minimum price
pack1.accept(&cheap);
cout << “The cheapest item is “
<< cheap.getMinItem()->getName() << ” at price”
<< cheap.getMinPrice() << ” rupies.” << endl;

}

void test2(double freshVegReduction, doublecannedItemReduction)
{
// Declare a couple of fresh vegetables and
// a canned item, giving their name and their price.
FreshVegetable carrot(“carrot”, 50.0), peas(“peas”, 60.0),parsnips(“parsnips”, 55.0);
CannedItem mushyPeas(“mushyPeas”, 80.0), bakedbeans(“bakedBeans”,100.0);
// Declare a package to contain multiple items

Package pack1(“package1”);

// Declare a second package that will contain pack1

Package pack2(“package2”);

// Add products to the packages – pack2 contains pack1
pack1.addProduct(&carrot);
pack1.addProduct(&peas);
pack1.addProduct(&mushyPeas);
pack2.addProduct(&pack1);
pack2.addProduct(&bakedbeans);
pack2.addProduct(&parsnips);
// The Cheapest Visitor calculates the price of the cheapest
// item in the package
CheapestVisitor cheap;
pack2.accept(&cheap);

cout << “The cheapest item is “
<< cheap.getMinItem()->getName() << ” at price”
<< cheap.getMinPrice() << ” rupies.” << endl;

// The ReducePriceVisitor takes two arguments – a percentage(0.80) by
// which to reduce the price of FreshVegetable products and
// a percentage (0.50) by which to reduce CannedItem products

ReducePriceVisitor priceModifier(freshVegReduction,cannedItemReduction);
pack2.accept(&priceModifier);

// Use CheapestVisitor to re-calculate price of cheapestitem

cheap.reset(); // re-set to compute a new minimum price
pack2.accept(&cheap);
cout << “The cheapest item is “
<< cheap.getMinItem()->getName() << ” at price”
<< cheap.getMinPrice() << ” rupies.” << endl;

}

int main()
{
test1(0.8,0.5);
test1(0.5,0.8);
test1(1.0,0.4);
test2(0.8,0.5);
test2(0.5,0.8);
test2(1.0,0.4);
return 0;
}

————————————————————————–Product.h——————————————————————————————————————————

#ifndef _PRODUCT_H
#define _PRODUCT_H
#include <iostream>
#include <vector>
using namespace std;

const int MAX_NAME_LEN = 200;

class ProductVisitor;

class Product
{
public:
Product() {};
virtual void accept(ProductVisitor *v) = 0;
virtual double getPrice() = 0;
char *getName() {return name;};
protected:
char name[MAX_NAME_LEN];
};

class Item : public Product
{
public:
Item(const char *n) : price(0.0) {strcpy(name, n);};
Item(const char *n, double p) : price(p) {strcpy(name, n);};
virtual void accept(ProductVisitor *v) = 0;
double getPrice() {return price;};
void setPrice(double p) { price = p;};

private:
double price;
};

class FreshVegetable : public Item
{
public:
FreshVegetable(const char *n) : Item(n) {};
FreshVegetable(const char *n, double p) : Item(n,p) {};
  
void accept(ProductVisitor *v);
};

class CannedItem : public Item
{
public:
CannedItem(const char *n) : Item(n) {};
CannedItem(const char *n, double p) : Item(n,p) {};
void accept(ProductVisitor *v);
};

class Package : public Product
{
public:
Package(const char pname[]) {strcpy(name, pname);};
Package& addProduct(Product *product) {contents.push_back(product); return *this; };
Product *getProduct(int i) { return contents[i];};
int size() {return contents.size();};
virtual void accept(ProductVisitor *v);
double getPrice() { double p=0.0; for (unsigned inti=0;i<contents.size();i++) { p+=contents[i]->getPrice();}return p;};
private:
vector<Product *> contents;
};

#endif
  

————————————————————————–ProductVisitor.cpp———————————————————————————————————————-

#include “ProductVisitor.h”
#include “Product.h”

// Accept() method for all products that accept a
// ProductVisitor

void FreshVegetable::accept(ProductVisitor *v)
{
v->visit(this);
};

void CannedItem::accept(ProductVisitor *v)
{
v->visit(this);
};

void Package::accept(ProductVisitor *v)
{
v->visit(this);
};

// Visit method for ProductVisitor class on Packageclass
void ProductVisitor::visit(Package *p)
{
// .. TO BE COMPLETED
}

// Visit Method for the CheapestVisitor class on CannedItemclass

void CheapestVisitor::visit(CannedItem *p)
{
// .. TO BE COMPLETED
}

// Visit Method for the CheapestVisitor class onFreshVegetable class
void CheapestVisitor::visit(FreshVegetable *p)
{
// .. TO BE COMPLETED
}

// Visit Method for ReducePriceVisitor class onFreshVegetable class

void ReducePriceVisitor::visit(FreshVegetable *p)
{
// .. TO BE COMPLETED
}

// Visit Method for ReducePriceVisitor class onCannedItem class

void ReducePriceVisitor::visit(CannedItem *p)
{
// .. TO BE COMPLETED
}

// CheapestVisitor Method to return the price of thecheapest item
double CheapestVisitor::getMinPrice()
{
// TO BE COMPLETED
}

// CheapestVisitor Method to return the cheapestItem
Item *CheapestVisitor::getMinItem()
{
// TO BE COMPLETED
}

// CheapestVisitor Method to reset before finding theminimum item
void CheapestVisitor::reset()
{
// TO BE COMPLETED
}

——————————————————————————-ProductVisitor.h—————————————————————————————————————–

#ifndef _PRODUCT_VISITOR_H
#define _PRODUCT_VISITOR_H

class Product;
class Item;
class CannedItem;
class FreshVegetable;
class Package;

class ProductVisitor
{
public:
ProductVisitor() {};
virtual void visit(FreshVegetable *p)= 0;
virtual void visit(CannedItem *p) = 0;
void visit(Package *p);
};

class CheapestVisitor : public ProductVisitor
{
public:
// … TO BE COMPLETED
double getMinPrice(); // Return the price of the cheapestitem
Item *getMinItem(); // Return the item with the cheapestprice
void reset(); // Reset before visiting a differentproduct

void visit(FreshVegetable *p);
void visit(CannedItem *p);
private:
// .. TO BE COMPLETED
};

class ReducePriceVisitor : public ProductVisitor
{
public:
// .. TO BE COMPLETED
void visit(FreshVegetable *p);
void visit(CannedItem *p);
private:
// .. TO BE COMPLETED
};
#endif

Package accept(ProductVisitor ) Product Item FreshVegetable accept(ProductVisitor *) Cannedltem accept(ProductVisitor “) CheapestVisitor visit(Cannedltem *) visit(FreshVegetable *) visit(Package) ProductVisitor ReducePriceVisitor visit(Cannedltem *) visit(FreshVegetable *) visit(Package*) Show transcribed image text

Expert Answer


Answer to Consider Class Hierarchy Last Page Assignment Class Hierarchy Food Products Including Fres Q34229371 . . .

OR