mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-18 07:51:29 +00:00
Update GildedRose.java
refactored code into small and reusable chunks focusing on items
This commit is contained in:
parent
f80413c2fd
commit
63e79e073e
@ -1,62 +1,114 @@
|
||||
package com.gildedrose;
|
||||
|
||||
class GildedRose {
|
||||
Item[] items;
|
||||
Item[] items;
|
||||
|
||||
public GildedRose(Item[] items) {
|
||||
this.items = items;
|
||||
}
|
||||
public static final String AGED_BRIE = "Aged Brie";
|
||||
public static final String BACKSTAGE_PASSES = "Backstage passes to a TAFKAL80ETC concert";
|
||||
public static final String SULFURAS = "Sulfuras, Hand of Ragnaros";
|
||||
public static final String CONJURED = "Conjured";
|
||||
|
||||
public void updateQuality() {
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
if (!items[i].name.equals("Aged Brie")
|
||||
&& !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 {
|
||||
if (items[i].quality < 50) {
|
||||
items[i].quality = items[i].quality + 1;
|
||||
public GildedRose(Item[] items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
public void updateQuality() {
|
||||
for (Item item : items) {
|
||||
updateQuality(item);
|
||||
updateSellIn(item);
|
||||
}
|
||||
}
|
||||
|
||||
if (items[i].sellIn < 6) {
|
||||
if (items[i].quality < 50) {
|
||||
items[i].quality = items[i].quality + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private void updateQuality(Item item) {
|
||||
|
||||
if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
|
||||
items[i].sellIn = items[i].sellIn - 1;
|
||||
}
|
||||
switch (item.name) {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
case AGED_BRIE:
|
||||
updateAgedBrie(item);
|
||||
break;
|
||||
case BACKSTAGE_PASSES:
|
||||
updateBackStagePasses(item);
|
||||
break;
|
||||
case SULFURAS:
|
||||
updateSulfras(item);
|
||||
break;
|
||||
case CONJURED:
|
||||
updateConjured(item);
|
||||
break;
|
||||
default:
|
||||
updateNormalItem(item);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void updateAgedBrie(Item item) {
|
||||
increaseQualityByOne(item);
|
||||
if (isItemExpired(item)) {
|
||||
increaseQualityByOne(item);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateBackStagePasses(Item item) {
|
||||
|
||||
if (!isItemExpired(item)) {
|
||||
increaseQualityByOne(item);
|
||||
|
||||
if (item.sellIn < 11) {
|
||||
increaseQualityByOne(item);
|
||||
}
|
||||
if (item.sellIn < 6) {
|
||||
increaseQualityByOne(item);
|
||||
}
|
||||
} else {
|
||||
item.quality = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void updateSulfras(Item item) {
|
||||
// todo...
|
||||
}
|
||||
|
||||
private void updateConjured(Item item) {
|
||||
updateNormalItem(item);
|
||||
updateNormalItem(item);
|
||||
}
|
||||
|
||||
private void updateNormalItem(Item item) {
|
||||
|
||||
decreaseQualityByOne(item);
|
||||
if (isItemExpired(item)) {
|
||||
decreaseQualityByOne(item);
|
||||
}
|
||||
|
||||
}
|
||||
private void updateSellIn(Item item) {
|
||||
|
||||
if (!item.name.equals(SULFURAS)) {
|
||||
item.sellIn = item.sellIn - 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private boolean isItemExpired(Item item) {
|
||||
return item.sellIn < 0;
|
||||
|
||||
}
|
||||
|
||||
public void decreaseQualityByOne(Item item) {
|
||||
if (item.quality > 0) {
|
||||
item.quality = item.quality - 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void increaseQualityByOne(Item item) {
|
||||
if (item.quality < 50) {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user