refactorings

This commit is contained in:
Gabba 2017-12-15 13:06:22 +00:00
parent 56f0c5d5f8
commit a2b84d2659
14 changed files with 115 additions and 51 deletions

BIN
Java/.DS_Store vendored Normal file

Binary file not shown.

BIN
Java/src/.DS_Store vendored Normal file

Binary file not shown.

BIN
Java/src/main/.DS_Store vendored Normal file

Binary file not shown.

BIN
Java/src/main/java/.DS_Store vendored Normal file

Binary file not shown.

BIN
Java/src/main/java/com/.DS_Store vendored Normal file

Binary file not shown.

Binary file not shown.

View File

@ -1,62 +1,43 @@
package com.gildedrose; package com.gildedrose;
import com.gildedrose.item.Item;
import com.gildedrose.item.ItemFactory;
class GildedRose { class GildedRose {
private static final int LOWEST_QUALITY_LEVEL_POSSIBLE = 0;
private static final int HIGHEST_QUALITY_LEVEL_POSSIBLE = 50;
private final ItemFactory itemFactory;
Item[] items; Item[] items;
public GildedRose(Item[] items) { public GildedRose(Item[] items) {
this.itemFactory = new ItemFactory();
this.items = items; this.items = items;
} }
public void updateQuality() { public void updateQuality() {
for (int i = 0; i < items.length; i++) { customizeItems();
if (!items[i].name.equals("Aged Brie") for (Item item : items) {
&& !items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) { item.updateYourState();
if (items[i].quality > 0) { if (hasReachedLowestQualityLimit(item.quality)) {
if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) { item.quality = LOWEST_QUALITY_LEVEL_POSSIBLE;
items[i].quality = items[i].quality - 1; } else if (hasReachedHighestQualityLimit(item.quality)) {
} item.quality = HIGHEST_QUALITY_LEVEL_POSSIBLE;
}
} else {
if (items[i].quality < 50) {
items[i].quality = items[i].quality + 1;
if (items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
if (items[i].sellIn < 11) {
if (items[i].quality < 50) {
items[i].quality = items[i].quality + 1;
}
}
if (items[i].sellIn < 6) {
if (items[i].quality < 50) {
items[i].quality = items[i].quality + 1;
}
}
}
}
}
if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
items[i].sellIn = items[i].sellIn - 1;
}
if (items[i].sellIn < 0) {
if (!items[i].name.equals("Aged Brie")) {
if (!items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
if (items[i].quality > 0) {
if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
items[i].quality = items[i].quality - 1;
}
}
} else {
items[i].quality = items[i].quality - items[i].quality;
}
} else {
if (items[i].quality < 50) {
items[i].quality = items[i].quality + 1;
}
}
} }
} }
} }
private void customizeItems() {
for (Item item : items) {
items = new Item[]{itemFactory.createItem(item.name, item.sellIn, item.quality)};
}
}
private boolean hasReachedLowestQualityLimit(int itemQuality) {
return itemQuality < LOWEST_QUALITY_LEVEL_POSSIBLE;
}
private boolean hasReachedHighestQualityLimit(int itemQuality) {
return itemQuality > HIGHEST_QUALITY_LEVEL_POSSIBLE;
}
} }

View File

@ -0,0 +1,13 @@
package com.gildedrose.item;
public class AgedBrie extends Item {
public AgedBrie(String name, int sellIn, int quality) {
super(name, sellIn, quality);
}
public void updateYourState() {
sellIn -= 1;
quality += 1;
}
}

View File

@ -0,0 +1,21 @@
package com.gildedrose.item;
public class BackstagePassesItem extends Item {
public BackstagePassesItem(String name, int sellIn, int quality) {
super(name, sellIn, quality);
}
public void updateYourState() {
sellIn -= 1;
if (sellIn >= 11) {
quality += 1;
} else if (sellIn > 5) {
quality += 2;
} else if (sellIn > 0) {
quality += 3;
} else {
quality = 0;
}
}
}

View File

@ -1,4 +1,4 @@
package com.gildedrose; package com.gildedrose.item;
public class Item { public class Item {
@ -14,8 +14,11 @@ public class Item {
this.quality = quality; this.quality = quality;
} }
@Override @Override
public String toString() { public String toString() {
return this.name + ", " + this.sellIn + ", " + this.quality; return this.name + ", " + this.sellIn + ", " + this.quality;
} }
public void updateYourState() {
}
} }

View File

@ -0,0 +1,16 @@
package com.gildedrose.item;
public class ItemFactory {
public Item createItem(String itemName, int sellIn, int quality) {
if (itemName.equals("Sulfuras, Hand of Ragnaros")) {
return new Sulfuras(itemName, sellIn, quality);
} else if (itemName.equals("Aged Brie")) {
return new AgedBrie(itemName, sellIn, quality);
} else if (itemName.equals("Backstage passes to a TAFKAL80ETC concert")) {
return new BackstagePassesItem(itemName, sellIn, quality);
} else {
return new StandardItem(itemName, sellIn, quality);
}
}
}

View File

@ -0,0 +1,17 @@
package com.gildedrose.item;
public class StandardItem extends Item {
public StandardItem(String name, int sellIn, int quality) {
super(name, sellIn, quality);
}
public void updateYourState() {
sellIn -= 1;
if (sellIn > 0) {
quality -= 1;
} else {
quality -= 2;
}
}
}

View File

@ -0,0 +1,11 @@
package com.gildedrose.item;
public class Sulfuras extends Item {
public Sulfuras(String name, int sellIn, int quality) {
super(name, sellIn, quality);
}
public void updateYourState() {
}
}

View File

@ -1,5 +1,7 @@
package com.gildedrose; package com.gildedrose;
import com.gildedrose.item.Item;
public class TexttestFixture { public class TexttestFixture {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("OMGHAI!"); System.out.println("OMGHAI!");