mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-21 01:11:08 +00:00
Restructured and fixed application logic
This commit is contained in:
parent
7049333680
commit
93d87f6bac
@ -1,62 +1,23 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import com.gildedrose.SellInUpdater;
|
||||||
|
import com.gildedrose.QualityUpdater;
|
||||||
|
|
||||||
class GildedRose {
|
class GildedRose {
|
||||||
Item[] items;
|
Item[] items;
|
||||||
|
SellInUpdater sellInUpdater = new SellInUpdater();
|
||||||
|
QualityUpdater qualityUpdater = new QualityUpdater();
|
||||||
|
|
||||||
public GildedRose(Item[] items) {
|
public GildedRose(Item[] items) {
|
||||||
this.items = items;
|
this.items = items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateQuality() {
|
//For each item update the SellIn value and adjust quality accordingly
|
||||||
for (int i = 0; i < items.length; i++) {
|
public void updateQuality() throws Exception {
|
||||||
if (!items[i].name.equals("Aged Brie")
|
for (Item item : items) {
|
||||||
&& !items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
sellInUpdater.updateSellInValue(item);
|
||||||
if (items[i].quality > 0) {
|
qualityUpdater.updateQualityForItem(item);
|
||||||
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;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
106
Java/src/main/java/com/gildedrose/QualityUpdater.java
Normal file
106
Java/src/main/java/com/gildedrose/QualityUpdater.java
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
public class QualityUpdater {
|
||||||
|
|
||||||
|
//Find items with exceptional quality rules
|
||||||
|
public void updateQualityForItem(Item item) throws Exception {
|
||||||
|
switch(item.name.substring(0,9)) {
|
||||||
|
case "Aged Brie":
|
||||||
|
updateNonDegradingItemQuality(item);
|
||||||
|
break;
|
||||||
|
case "Sulfuras,":
|
||||||
|
updateLegendaryItemQuality(item);
|
||||||
|
break;
|
||||||
|
case "Backstage":
|
||||||
|
updateBackstagePassQuality(item);
|
||||||
|
break;
|
||||||
|
case "Conjured ":
|
||||||
|
updateConjuredItemQuality(item);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
updateRegularItemQuality(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Increase quality of items which cannot degrade
|
||||||
|
private void updateNonDegradingItemQuality(Item item) throws Exception {
|
||||||
|
if (item.sellIn >= 0) {
|
||||||
|
adjustQuality(item, "increase",1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
adjustQuality(item, "increase",2);
|
||||||
|
}
|
||||||
|
makeSureQualityIsInRange(item, 50,0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Set legendary item quality to 80 as it can never be different
|
||||||
|
private void updateLegendaryItemQuality(Item item) {
|
||||||
|
makeSureQualityIsInRange(item, 80,80);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Increase quality of backstage passes as the concert date nears, after the concert quality drops to 0
|
||||||
|
private void updateBackstagePassQuality(Item item) throws Exception {
|
||||||
|
if (item.sellIn > 10) {
|
||||||
|
adjustQuality(item, "increase",1);
|
||||||
|
}
|
||||||
|
if (item.sellIn <= 10 && item.sellIn > 5) {
|
||||||
|
adjustQuality(item, "increase",2);
|
||||||
|
}
|
||||||
|
if (item.sellIn <= 5 && item.sellIn >= 0) {
|
||||||
|
adjustQuality(item, "increase",3);
|
||||||
|
}
|
||||||
|
if (item.sellIn < 0) {
|
||||||
|
adjustQuality(item, "set",0);
|
||||||
|
}
|
||||||
|
makeSureQualityIsInRange(item, 50,0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Decrease quality of conjured items fast
|
||||||
|
private void updateConjuredItemQuality(Item item) throws Exception {
|
||||||
|
if (item.sellIn >= 0) {
|
||||||
|
adjustQuality(item, "decrease",2);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
adjustQuality(item, "decrease",4);
|
||||||
|
}
|
||||||
|
makeSureQualityIsInRange(item, 50,0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Decrease quality of item types not specified above
|
||||||
|
private void updateRegularItemQuality(Item item) throws Exception {
|
||||||
|
if (item.sellIn >= 0) {
|
||||||
|
adjustQuality(item, "decrease",1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
adjustQuality(item, "decrease",2);
|
||||||
|
}
|
||||||
|
makeSureQualityIsInRange(item, 50,0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Adjust the item quality with previously specified value
|
||||||
|
private void adjustQuality(Item item, String adjustment , int value) throws Exception {
|
||||||
|
switch (adjustment) {
|
||||||
|
case "increase":
|
||||||
|
item.quality = item.quality + value;
|
||||||
|
break;
|
||||||
|
case "decrease":
|
||||||
|
item.quality = item.quality - value;
|
||||||
|
break;
|
||||||
|
case "set":
|
||||||
|
item.quality = value;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Exception("Unknown quality adjustment: "+adjustment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Make sure the quality never goes beyond the set boundaries
|
||||||
|
private void makeSureQualityIsInRange(Item item, int qualityIsNeverAbove, int qualityIsNeverBelow) {
|
||||||
|
if (item.quality > qualityIsNeverAbove) {
|
||||||
|
item.quality = qualityIsNeverAbove;
|
||||||
|
}
|
||||||
|
if (item.quality < qualityIsNeverBelow) {
|
||||||
|
item.quality = qualityIsNeverBelow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
20
Java/src/main/java/com/gildedrose/SellInUpdater.java
Normal file
20
Java/src/main/java/com/gildedrose/SellInUpdater.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
public class SellInUpdater {
|
||||||
|
|
||||||
|
//Decrease the sellIn date of non-legendary items with every passing day
|
||||||
|
public void updateSellInValue(Item item) {
|
||||||
|
if (isNoLegendaryItem(item)) {
|
||||||
|
item.sellIn = item.sellIn - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Check if the item isn't legendary
|
||||||
|
private boolean isNoLegendaryItem(Item item) {
|
||||||
|
if (item.name.startsWith("Sulfuras,")) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
public class TexttestFixture {
|
public class TexttestFixture {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) throws Exception {
|
||||||
System.out.println("OMGHAI!");
|
System.out.println("OMGHAI!");
|
||||||
|
|
||||||
Item[] items = new Item[] {
|
Item[] items = new Item[] {
|
||||||
@ -18,7 +18,7 @@ public class TexttestFixture {
|
|||||||
|
|
||||||
GildedRose app = new GildedRose(items);
|
GildedRose app = new GildedRose(items);
|
||||||
|
|
||||||
int days = 2;
|
int days = 100;
|
||||||
if (args.length > 0) {
|
if (args.length > 0) {
|
||||||
days = Integer.parseInt(args[0]) + 1;
|
days = Integer.parseInt(args[0]) + 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user