Skip to content
Permalink
Browse files

Step 6: Extract the behaviour handling into an Updater class

  • Loading branch information
sandordargo committed Jul 23, 2018
1 parent f37bbcd commit 32b9b235d242fee994cac6bf4f394fbc73a52dad
Showing with 93 additions and 17 deletions.
  1. +4 −8 cpp/GildedRose.cc
  2. +89 −9 cpp/GildedRose.h
@@ -10,15 +10,11 @@ void GildedRose::processItem(Item & item)
item.updateQuality();

} else if (item.name == "Sulfuras, Hand of Ragnaros") {

item.updateSellIn();
item.updateQuality();
} else if (item.name == "Aged Brie") {
item.sellIn--;
if (item.quality < 50) {
item.quality++;
if (item.quality < 50 && item.sellIn < 0) {
item.quality++;
}
}
item.updateSellIn();
item.updateQuality();
} else {
if(item.name != "Aged Brie" && item.name != "Backstage passes to a TAFKAL80ETC concert"){
if(item.quality > 0){
@@ -1,31 +1,111 @@
#include <string>
#include <vector>
#include <iostream>

using namespace std;

class Updater {
public:
Updater(int& sellIn, int& quality) : _quality(quality), _sellIn(sellIn) {}
virtual ~Updater() {};

virtual void updateQuality() = 0;
virtual void updateSellIn() = 0;

protected:
int& _quality;
int& _sellIn;
};

class DefaultUpdater : public Updater {
public:
DefaultUpdater(int& sellIn, int& quality) : Updater(sellIn, quality) {}

virtual ~DefaultUpdater() {};

virtual void updateQuality() {
if (_quality > 0) {
_quality--;
if (_quality > 0 && _sellIn < 0) {
_quality--;
}
}
}

virtual void updateSellIn() {
_sellIn--;
}
};

class SulfurasUpdater : public Updater {
public:
SulfurasUpdater(int& sellIn, int& quality) : Updater(sellIn, quality) {}

virtual ~SulfurasUpdater() {};

virtual void updateQuality() {}

virtual void updateSellIn() {}
};

class AgedBrieUpdater : public Updater {
public:
AgedBrieUpdater(int& sellIn, int& quality) : Updater(sellIn, quality) {}

virtual ~AgedBrieUpdater() {};

virtual void updateQuality() {
if (_quality < 50) {
_quality++;
if (_quality < 50 && _sellIn < 0) {
_quality++;
}
}
}

virtual void updateSellIn() {
_sellIn--;
}
};

class Item
{
public:
string name;
int sellIn;
int quality;
Item(string name, int sellIn, int quality) : name(name), sellIn(sellIn), quality(quality)
{}
Updater* updater;

Item(const Item& i):Item(i.name, i.sellIn, i.quality) {}
Item& operator=(const Item& i){
this->name = i.name;
this->quality = i.quality;
this->sellIn = i.sellIn;
}

Item(string name, int sellIn, int quality) : name(name), sellIn(sellIn), quality(quality)//, updater()
{
if (name == "Sulfuras, Hand of Ragnaros") {
updater = new SulfurasUpdater(this->sellIn, this->quality);
} else if(name == "Aged Brie") {
updater = new AgedBrieUpdater(this->sellIn, this->quality);
} else {
updater = new DefaultUpdater(this->sellIn, this->quality);
}

}

void updateSellIn() {
sellIn--;
updater->updateSellIn();
}

void updateQuality() {
if (quality > 0) {
quality--;
if (quality > 0 && sellIn < 0) {
quality--;
}
}
updater->updateQuality();
}
};



class GildedRose
{
public:

0 comments on commit 32b9b23

Please sign in to comment.
You can’t perform that action at this time.