Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the class hierarchy on the last page of this assignment. It is a class hierarchy of food products, including FreshVegetable and CannedItem. A Package

Consider the class hierarchy on the last page of this assignment. It is a class hierarchy of food products, including FreshVegetable and CannedItem.

A Package is also a product and is a container of multiple products.

We wish to use the visitor pattern to implement two operations on 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 and FreshVegetable items in a product by specified amounts.

We implement this using the ReducePriceVisitor.

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

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

The ProductVisitor hierarchy is partially implemented in ProductVisitor.h.

You need to complete the implementation of the CheapestVisitor and ReducePriceVisitor classes.

You are also required to implement the following methods in the file 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 of the cheapest item

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

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

All points where implementation is required are marked by the comment TO BE COMPLETED

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

There are two test functions in main.cpp - change main() to try each one.

image text in transcribed

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

#include "Product.h" #include "ProductVisitor.h"

#include using namespace std;

void test1(double freshVegReduction, double cannedItemReduction) { // 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 the cheapest // item in the package CheapestVisitor cheap; pack1.accept(&cheap);

cout getName()

// 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 cheapest item

cheap.reset(); // re-set to compute a new minimum price pack1.accept(&cheap); cout getName()

}

void test2(double freshVegReduction, double cannedItemReduction) { // 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 getName()

// 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 cheapest item

cheap.reset(); // re-set to compute a new minimum price pack2.accept(&cheap); cout getName()

}

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 #include 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 int i=0;igetPrice();} return p;}; private: vector 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 Package class void ProductVisitor::visit(Package *p) { // .. TO BE COMPLETED }

// Visit Method for the CheapestVisitor class on CannedItem class

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

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

// Visit Method for ReducePriceVisitor class on FreshVegetable class

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

// Visit Method for ReducePriceVisitor class on CannedItem class

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

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

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

// CheapestVisitor Method to reset before finding the minimum 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 cheapest item Item *getMinItem(); // Return the item with the cheapest price void reset(); // Reset before visiting a different product

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*)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

More Books

Students also viewed these Databases questions