Question

Consider the class hierarchy on the last page of this assignment. It is a class hierarchy...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

main.cpp

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


#include <iostream>
using namespace std;


int main()
{
// 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 some packages that can contain multiple products

Package pack1, pack2;

// 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;


ReducePriceVisitor priceModifier(0.90,0.50);
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 << "The cheapest product is "
       << cheap.getMinItem()->getName() << " at price "
       << cheap.getMinPrice() << " rupies." << endl;

return 0;
}


Product.h

#ifndef _PRODUCT_H
#define _PRODUCT_H
#include <iostream>
#include <vector>
#include <cstring>
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;
private:
double price;

};

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;};
char *getName() {return name;};

private:
double price;
char name[MAX_NAME_LEN];
};

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() {};
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 (int i=0;i<contents.size();i++) { p+=contents[i]->getPrice();} return p;};
private:
vector<Product *> contents;
};

#endif
  

  
ProductVisitor.cpp

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


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)
{
for (int i = 0; i < p->size(); i++)
{
    p->getProduct(i)->accept(this);
}
}

// Sets the min item.
void CheapestVisitor::setMinItem(Item *p) {
if (currentMinPrice == -1) {
    this->currentMinPrice = p->getPrice();
    this->minItem = p;
} else if (p->getPrice() < currentMinPrice) {
    this->currentMinPrice = p->getPrice();
    this->minItem = p;
}
}

// Visit Method for the CheapestVisitor class on CannedItem class
void CheapestVisitor::visit(CannedItem *p)
{
this->setMinItem(p);
}

// Visit Method for the CheapestVisitor class on FreshVegetable class
void CheapestVisitor::visit(FreshVegetable *p)
{
   this->setMinItem(p);
}

ReducePriceVisitor::ReducePriceVisitor(double a, double b) {
   this->pFreshVeg = a;
   this->pCanned = b;
}

//reduce price
void ReducePriceVisitor::reducePrice(Item *p,double percentage) {
   p->setPrice((p->getPrice())*percentage);
}
// Visit Method for ReducePriceVisitor class on FreshVegetable class

void ReducePriceVisitor::visit(FreshVegetable *p)
{
   this->reducePrice(p,this->pFreshVeg);
}

// Visit Method for ReducePriceVisitor class on CannedItem class

void ReducePriceVisitor::visit(CannedItem *p)
{
   this->reducePrice(p,this->pCanned);
}

// CheapestVisitor Method to return the price of the cheapest item
double CheapestVisitor::getMinPrice()
{
return this->currentMinPrice;
}

// CheapestVisitor Method to return the cheapest Item
// If more than one item is equally cheap,
// this returns the first item which was cheap.
Item *CheapestVisitor::getMinItem()
{
return this->minItem;
}

// CheapestVisitor Method to reset before finding the minimum item

void CheapestVisitor::reset()
{
this->currentMinPrice = -1;
};

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:
double getMinPrice();
Item *getMinItem();
void reset();
void visit(FreshVegetable *p);
void visit(CannedItem *p);
void setMinItem(Item *p);
private:
   double currentMinPrice = -1;
   Item *minItem;

};

class ReducePriceVisitor : public ProductVisitor
{
public:
ReducePriceVisitor(double a, double b);
void visit(FreshVegetable *p);
void visit(CannedItem *p);
void reducePrice(Item *p,double percetage);
private:
   double pFreshVeg;
   double pCanned;
};
#endif

Add a comment
Know the answer?
Add Answer to:
Consider the class hierarchy on the last page of this assignment. It is a class hierarchy...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this assignment, you are given several classes in the cpp file “DList.cpp”. Your task is...

    In this assignment, you are given several classes in the cpp file “DList.cpp”. Your task is to complete the implementation of the classes specified as below. Y 1 Your Task You are given a class “Item” that contains one integer value, and two pointers. You are going to build a doubly linked list class DLinkedList. I describe the tasks below. Task 1: Implement the constructors (default and copy) of DLinkedList. You need to make sure that the copy constructor makes...

  • lab 11 Do not change main.cpp, i need c++ code for queue.h and queue.cpp Given the...

    lab 11 Do not change main.cpp, i need c++ code for queue.h and queue.cpp Given the complete main() function, partial queue class header queue.h, and queue.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor enqueue() dequeue() You may elect to create the following helper functions: isFull() isEmpty() A description of these ADT operations are available in this Zybook and in the textbook's chapter 17. Example: If the input is: 3 Led Zepplin...

  • Assignment4: Evaluate Arithmetic Expressions. Requirements: Implement a concrete ArrayStack class that extends the IStack interface as...

    Assignment4: Evaluate Arithmetic Expressions. Requirements: Implement a concrete ArrayStack class that extends the IStack interface as we discussed in the class (any other different Stack class implementation, even if it is implemented by yourself, will not receive any credit). Write a test class called Evaluate and a method that evaluates an arithmatic expression, which is given by a string. import java.util.Scanner; public class Evaluate { public static void main(String[] args) } // your implementation // obtain user's input from keyboard...

  • The objectives of this assignment are to: Further enhance your knowledge and skill in Java. Gain...

    The objectives of this assignment are to: Further enhance your knowledge and skill in Java. Gain an understanding and experience with stacks. Gain further experience using generics. Continue to practice good programming techniques. Create a stack class named MyStack that stores generics with the methods shown below. Write a test program that thoroughly tests your stack implementation. void push(E item) push item onto top of stack E peek() return item on top of stack E pop() return item on top...

  • c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked...

    c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked list: template<class ItemType> struct NodeType {                 ItemType item;                 NodeType* next; }; template<class ItemType> class UList { public:                 UList(); // default constrctor                 UList(const UList &x); // we implement copy constructor with deep copy                 UList& operator = (UList &x); // equal sign operator with deep copy                 bool IsThere(ItemType item) const; // return true of false to indicate if item is...

  • Name the program for this assignment "bst_driver.cpp." In this assignment you will make several modifications to...

    Name the program for this assignment "bst_driver.cpp." In this assignment you will make several modifications to the BST class in the file “bst.cpp” that I provided. Consider the following: 1. Change the declaration of treenode to class treenode //node in a BST { public: string county_name; double population_size; treenode *lchild, *rchild; //left and right children pointers }; 2. Make the following changes to the BST class: a) change the name from “BST” to “bst”; b) change default constructor from “BST”...

  • I need help with the Implementation of an Ordered List (Template Files) public interface Ordered...

    I need help with the Implementation of an Ordered List (Template Files) public interface OrderedStructure { public abstract int size(); public abstract boolean add( Comparable obj ) throws IllegalArgumentException; public abstract Object get( int pos ) throws IndexOutOfBoundsException; public abstract void remove( int pos ) throws IndexOutOfBoundsException; public abstract void merge( OrderedList other ); } import java.util.NoSuchElementException; public class OrderedList implements OrderedStructure { // Implementation of the doubly linked nodes (nested-class) private static class Node { private Comparable value; private...

  • OUTCOMES After you finish this assignment, you will be able to do the following: Define an...

    OUTCOMES After you finish this assignment, you will be able to do the following: Define an abstract class Create concrete classes from an abstract class Overload an operator Split classes into .h and .cpp files Open files for reading Write to files Use output manipulators such as setw, fixed, and setprecision DESCRIPTION A binary arithmetic operation takes two double operands (left and right) to perform addition, subtraction, multiplication, or division on. For example, 10 + 11 is an addition (+)...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • create a programn in C++ that creates a class that represents a manufactured part on a...

    create a programn in C++ that creates a class that represents a manufactured part on a bill of material (BOM). with the following attributes: identifier (The parts identifier as an alpha numeric string), drawing (The AutoCAD drawing file that represents the part), quantity (The number of parts that are required). implement the functions for the Part class in the file Part.cpp. The file main.cpp will only be used for testing purposes, no code should be written in main.cpp. -part.cpp file:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT