refactor: move all the mutations on Item to its own class

This commit is contained in:
Kadir Sirimsi 2025-02-10 13:11:52 +01:00
parent 58205b5740
commit d66943f0f4
No known key found for this signature in database
GPG Key ID: A21C0144C2D2A134
2 changed files with 24 additions and 24 deletions

View File

@ -15,55 +15,39 @@ class GildedRose {
if (!isAgedBrie && !passesToTafkalConcert) { if (!isAgedBrie && !passesToTafkalConcert) {
if (item.quality > 0 && !isSulfuras) { if (item.quality > 0 && !isSulfuras) {
deductOneFromQuality(item); item.deductOneFromQuality();
} }
} else if(item.quality < 50) { } else if(item.quality < 50) {
addOneToQuality(item); item.addOneToQuality();
if (passesToTafkalConcert) { if (passesToTafkalConcert) {
if (item.sellIn < 11 && item.quality < 50) { if (item.sellIn < 11 && item.quality < 50) {
addOneToQuality(item); item.addOneToQuality();
} }
if (item.sellIn < 6 && item.quality < 50) { if (item.sellIn < 6 && item.quality < 50) {
addOneToQuality(item); item.addOneToQuality();
} }
} }
} }
if (!isSulfuras) { if (!isSulfuras) {
deductSellIn(item); item.deductSellIn();
} }
if (item.sellIn < 0) { if (item.sellIn < 0) {
if (!isAgedBrie) { if (!isAgedBrie) {
if (!passesToTafkalConcert) { if (!passesToTafkalConcert) {
if (item.quality > 0 && !isSulfuras) { if (item.quality > 0 && !isSulfuras) {
deductOneFromQuality(item); item.deductOneFromQuality();
} }
} else { } else {
setQualityToZero(item); item.setQualityToZero();
} }
} else if ((item.quality < 50)) { } else if ((item.quality < 50)) {
addOneToQuality(item); item.addOneToQuality();
} }
} }
} }
} }
public void setQualityToZero(Item item) {
item.quality = 0;
}
public void deductSellIn(Item item) {
item.sellIn--;
}
public void addOneToQuality(Item item) {
item.quality++;
}
public void deductOneFromQuality(Item item) {
item.quality--;
}
} }

View File

@ -18,6 +18,22 @@ public class Item {
return name; return name;
} }
public void setQualityToZero() {
quality = 0;
}
public void deductSellIn() {
sellIn--;
}
public void addOneToQuality() {
quality++;
}
public void deductOneFromQuality() {
quality--;
}
@Override @Override
public String toString() { public String toString() {
return this.name + ", " + this.sellIn + ", " + this.quality; return this.name + ", " + this.sellIn + ", " + this.quality;