I have a trouble with c++ visitor pattern. I have tried to solve this. but I didn't. can anyone help me out?
Below is the code I have got.
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;
// 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(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>
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;
};
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
p->getProduct(this);
p->getPrice(this);
}
// Visit Method for the CheapestVisitor class on CannedItem class
void CheapestVisitor::visit(CannedItem *p)
{
// .. TO BE COMPLETED
p->accept(this);
}
// Visit Method for the CheapestVisitor class on FreshVegetable class
void CheapestVisitor::visit(FreshVegetable *p)
{
// .. TO BE COMPLETED
p->accept();
}
// Visit Method for ReducePriceVisitor class on FreshVegetable class
void ReducePriceVisitor::visit(FreshVegetable *p)
{
// .. TO BE COMPLETED
p->visit(FreshVegetable *p);
}
// Visit Method for ReducePriceVisitor class on CannedItem class
void ReducePriceVisitor::visit(CannedItem *p)
{
// .. TO BE COMPLETED
p-> visit(CannedItem *p);
}
// 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();
Item *getMinItem();
void reset();
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
};
above is the code I have got. I know it can be simple. but I have not much knowledge to complete this code. anyone can help to complete this code? as the output I need to get The cheapest item is carrot at price 50 rupies. The cheapest item is mushyPeas at price 40 rupies.
did u find answer for this ?
RépondreSupprimerHere you go
RépondreSupprimer//ProductVisitor.h
#ifndef _PRODUCT_VISITOR_H
#define _PRODUCT_VISITOR_H
#include
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 - Done
CheapestVisitor(): cheapest_item(NULL){}
virtual ~CheapestVisitor() {}
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
void findChepestItem(Item* item);
Item* cheapest_item;
};
class ReducePriceVisitor : public ProductVisitor
{
public:
// .. TO BE COMPLETED - Done
ReducePriceVisitor(double freshVegReduction, double cannedItemReduction):
freshVegReductionAmount(freshVegReduction),
cannedItemReductionAmount(cannedItemReduction)
{}
virtual ~ReducePriceVisitor(){};
void visit(FreshVegetable *p);
void visit(CannedItem *p);
private:
// .. TO BE COMPLETED - Done
double freshVegReductionAmount;
double cannedItemReductionAmount;
};
#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 : Done
for ( int i =0; i < p->size() ; ++i){
p->getProduct(i)->accept(this);
}
}
// Visit Method for the CheapestVisitor class on CannedItem class
void CheapestVisitor::findChepestItem(Item* item)
{
if ( (cheapest_item == NULL) || (item->getPrice() < cheapest_item->getPrice()))
{
cheapest_item = item;
}
}
void CheapestVisitor::visit(CannedItem *p)
{
// .. TO BE COMPLETED - Done
findChepestItem(p);
}
// Visit Method for the CheapestVisitor class on FreshVegetable class
void CheapestVisitor::visit(FreshVegetable *p)
{
// .. TO BE COMPLETED - Done
findChepestItem(p);
}
// Visit Method for ReducePriceVisitor class on FreshVegetable class
void ReducePriceVisitor::visit(FreshVegetable *p)
{
// .. TO BE COMPLETED - Done
p->setPrice(p->getPrice() - freshVegReductionAmount);
}
// Visit Method for ReducePriceVisitor class on CannedItem class
void ReducePriceVisitor::visit(CannedItem *p)
{
// .. TO BE COMPLETED - Done
p->setPrice(p->getPrice() - cannedItemReductionAmount);
}
// CheapestVisitor Method to return the price of the cheapest item
double CheapestVisitor::getMinPrice()
{
// TO BE COMPLETED - Done
return cheapest_item->getPrice();
}
// CheapestVisitor Method to return the cheapest Item
Item *CheapestVisitor::getMinItem()
{
// TO BE COMPLETED - Done
return cheapest_item;
}
// CheapestVisitor Method to reset before finding the minimum item
void CheapestVisitor::reset()
{
// TO BE COMPLETED - Done
cheapest_item = NULL;
}
Thanks bro
RépondreSupprimer