Created
December 15, 2014 08:52
-
-
Save kevinrutherford/2d1f17a0be449c8fd1f0 to your computer and use it in GitHub Desktop.
Gilded rose solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Adjuster { | |
public abstract void adjust(Item item); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AdjustQualityBy implements Adjuster { | |
private int adjustment; | |
public AdjustQualityBy(int adjustment) { | |
this.adjustment = adjustment; | |
} | |
public void adjust(Item item) { | |
item.setQuality(item.getQuality() + adjustment); | |
if (item.getSellIn() < 0) | |
item.setQuality(item.getQuality() + adjustment); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BackstagePassAdjuster implements Adjuster { | |
public void adjust(Item item) { | |
item.setQuality(item.getQuality() + 1); | |
if (item.getSellIn() < 10) | |
item.setQuality(item.getQuality() + 1); | |
if (item.getSellIn() < 5) | |
item.setQuality(item.getQuality() + 1); | |
if (item.getSellIn() < 0) | |
item.setQuality(0); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class GildedRose { | |
private static final Map<String, Product> products; | |
static { | |
products = new HashMap<String, Product>(); | |
products.put("NORMAL ITEM", new Product(new AdjustQualityBy(-1))); | |
products.put("Aged Brie", new Product(new AdjustQualityBy(+1))); | |
products.put("Backstage passes to a TAFKAL80ETC concert", new Product(new BackstagePassAdjuster())); | |
products.put("Conjured Mana Cake", new Product(new AdjustQualityBy(-2))); | |
} | |
public static void updateQuality(List<Item> items) { | |
for (int i = 0; i < items.size(); i++) | |
update(items.get(i)); | |
} | |
private static void update(Item item) { | |
Product p = products.get(item.getName()); | |
if (p != null) | |
p.update(item); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Product { | |
private Adjuster qualityAdjuster; | |
public Product(Adjuster qualityAdjuster) { | |
this.qualityAdjuster = qualityAdjuster; | |
} | |
public void update(Item item) { | |
item.setSellIn(item.getSellIn() - 1); | |
qualityAdjuster.adjust(item); | |
if (item.getQuality() < 0) | |
item.setQuality(0); | |
if (item.getQuality() > 50) | |
item.setQuality(50); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment