instance method

This commit is contained in:
Karim Fadel 2026-01-14 08:03:21 +02:00
parent a58edfb942
commit b2e26bd812
2 changed files with 53 additions and 53 deletions

View File

@ -1,9 +1,6 @@
package com.gildedrose;
class GildedRose {
private final static String AGED_BRIE = "Aged Brie";
private final static String BACKSTAGE_PASSES = "Backstage passes to a TAFKAL80ETC concert";
private final static String SULFURAS = "Sulfuras, Hand of Ragnaros";
Item[] items;
@ -13,66 +10,26 @@ class GildedRose {
public void updateQuality() {
for (Item item : items) {
if (item.name.equals(AGED_BRIE)) {
increaseQualityByOne(item);
} else if (item.name.equals(BACKSTAGE_PASSES)) {
increaseQualityBackstage(item);
if (item.name.equals(Item.AGED_BRIE)) {
item.increaseQualityByOne();
} else if (item.name.equals(Item.BACKSTAGE_PASSES)) {
item.increaseQualityBackstage();
} else {
decreaseQualityByOne(item);
item.decreaseQualityByOne();
}
decreaseSellInEachDay(item);
item.decreaseSellInEachDay();
if (item.sellIn < 0) {
if (item.name.equals(AGED_BRIE)) {
increaseQualityByOne(item);
} else if (item.name.equals(BACKSTAGE_PASSES)) {
if (item.name.equals(Item.AGED_BRIE)) {
item.increaseQualityByOne();
} else if (item.name.equals(Item.BACKSTAGE_PASSES)) {
item.quality = 0;
} else {
decreaseQualityByOne(item);
item.decreaseQualityByOne();
}
}
}
}
private void decreaseSellInEachDay(Item item) {
if (!item.name.equals(SULFURAS)) {
item.sellIn--;
}
}
private void increaseQualityBackstage(Item item) {
if (item.sellIn < 6) {
increaseQualityByThree(item);
} else if (item.sellIn < 11) {
increaseQualityByTwo(item);
} else {
increaseQualityByOne(item);
}
}
private void decreaseQualityByOne(Item item) {
if (item.quality > 0) {
if (!item.name.equals(SULFURAS)) {
item.quality--;
}
}
}
private void increaseQualityByOne(Item item) {
if (item.quality < 50) {
item.quality++;
}
}
private void increaseQualityByTwo(Item item) {
increaseQualityByOne(item);
increaseQualityByOne(item);
}
private void increaseQualityByThree(Item item) {
increaseQualityByTwo(item);
increaseQualityByOne(item);
}
}

View File

@ -2,6 +2,9 @@ package com.gildedrose;
public class Item {
public final static String AGED_BRIE = "Aged Brie";
public final static String BACKSTAGE_PASSES = "Backstage passes to a TAFKAL80ETC concert";
public final static String SULFURAS = "Sulfuras, Hand of Ragnaros";
public String name;
public int sellIn;
@ -18,4 +21,44 @@ public class Item {
public String toString() {
return this.name + ", " + this.sellIn + ", " + this.quality;
}
void decreaseSellInEachDay() {
if (!name.equals(SULFURAS)) {
sellIn--;
}
}
void increaseQualityByOne() {
if (quality < 50) {
quality++;
}
}
void increaseQualityByTwo() {
increaseQualityByOne();
increaseQualityByOne();
}
void increaseQualityByThree() {
increaseQualityByTwo();
increaseQualityByOne();
}
void decreaseQualityByOne() {
if (quality > 0) {
if (!name.equals(SULFURAS)) {
quality--;
}
}
}
void increaseQualityBackstage() {
if (sellIn < 6) {
increaseQualityByThree();
} else if (sellIn < 11) {
increaseQualityByTwo();
} else {
increaseQualityByOne();
}
}
}