Add Class inheritance and refactoring of the code

This commit is contained in:
brianblessou 2019-05-12 16:44:36 +02:00
parent 29f0e257aa
commit e97cf12d15
5 changed files with 50 additions and 53 deletions

View File

@ -1,32 +1,16 @@
package com.gildedrose;
public class AgedBrie {
private Item item;
public class AgedBrie extends RegularItem {
public AgedBrie(Item item) {
this.item=item;
}
public void updateQuaility() {
increaseQuality();
if (itemHasExpired()) {
increaseQuality();
}
}
private void increaseQuality() {
if (item.quality < 50) {
item.quality += 1;
}
}
private boolean itemHasExpired() {
boolean condition;
if (item.sellIn < 0) {
condition = true;
increaseQualityBy(2);
} else {
condition = false;
increaseQualityBy(1);
}
return condition;
}
}

View File

@ -1,38 +1,20 @@
package com.gildedrose;
public class BackStageItem {
private Item item;
public class BackStageItem extends RegularItem{
public BackStageItem(Item item) {
this.item=item;
}
public void update() {
increaseQuality();
increaseQualityBy(1);
if (item.sellIn < 10) {
increaseQuality();
increaseQualityBy(1);
}
if (item.sellIn < 5) {
increaseQuality();
increaseQualityBy(1);
}
if (itemHasExpired()) {
item.quality -= item.quality;
}
}
private void increaseQuality() {
if (item.quality < 50) {
item.quality += 1;
}
}
private boolean itemHasExpired() {
boolean condition;
if (item.sellIn < 0) {
condition = true;
} else {
condition = false;
}
return condition;
}
}

View File

@ -0,0 +1,14 @@
package com.gildedrose;
public class ConjuredItem extends RegularItem {
public ConjuredItem(Item item) {
this.item=item;
}
public void updateQuality() {
decreaseQualityBy(1);
if (itemHasExpired()) {
decreaseQualityBy(1);
}
}
}

View File

@ -5,6 +5,7 @@ class GildedRoseItem {
public static final String SULFURA = "Sulfuras, Hand of Ragnaros";
public static final String AGED_BRIE = "Aged Brie";
public static final String BACKSTAGE = "Backstage passes to a TAFKAL80ETC concert";
public static final String CONJURED = "Conjured Mana Cake";
public GildedRoseItem(Item[] items) {
@ -22,6 +23,9 @@ class GildedRoseItem {
} else if (item.name.equals(BACKSTAGE)) {
BackStageItem backStageItem = new BackStageItem(item);
backStageItem.update();
} else if (item.name.equals(CONJURED)) {
ConjuredItem conjuredItem = new ConjuredItem(item);
conjuredItem.updateQuality();
} else {
RegularItem regularItem = new RegularItem(item);
regularItem.updateQuality();

View File

@ -1,21 +1,25 @@
package com.gildedrose;
public class RegularItem {
private Item item;
public Item item;
public RegularItem() {
}
public RegularItem(Item item) {
this.item = item;
}
public void updateQuality() {
if (itemHasExpired(item)) {
decreaseQualityTwice();
if (itemHasExpired()) {
decreaseQualityBy(2);
} else {
decreaseQuality();
decreaseQualityBy(1);
}
}
private boolean itemHasExpired(Item item) {
protected boolean itemHasExpired() {
boolean condition;
if (item.sellIn < 0) {
condition = true;
@ -25,16 +29,25 @@ public class RegularItem {
return condition;
}
private void decreaseQualityTwice() {
decreaseQuality();
decreaseQuality();
protected void increaseQualityBy(int factor) {
item.quality += factor;
qualityOfAnItemIsNotMoreThan(50);
}
private void decreaseQuality() {
if (item.quality > 0) {
item.quality -= 1;
protected void decreaseQualityBy(int factor) {
item.quality -= factor;
qualityOfAnItemIsNeverNegative();
}
private void qualityOfAnItemIsNotMoreThan(int limit) {
if (item.quality > limit) {
item.quality = limit;
}
}
private void qualityOfAnItemIsNeverNegative() {
if (item.quality < 0) {
item.quality = 0;
}
}
}