Medium
This code works and the client needs are met, it can however be largely improved.
I which way could it be improved, regarding the maintenability ?
class GildedRose {
private static final String SULFURAS_NAME = "Sulfuras, Hand of Ragnaros";
private static final String BACKSTAGE_PASSES_NAME = "Backstage passes to a TAFKAL80ETC concert";
private static final String AGED_BRIE_NAME = "Aged Brie";
Item[] items;
public GildedRose(Item[] items) {
this.items = items;
}
public void updateQuality() {
for (Item item : items) {
if (SULFURAS_NAME.equals(item.name)) {
continue;
}
item.sellIn = item.sellIn - 1;
if (AGED_BRIE_NAME.equals(item.name)) {
updateAgedBrie(item);
} else if (BACKSTAGE_PASSES_NAME.equals(item.name)) {
updateBackstagePass(item);
} else {
updateNormalItem(item);
}
}
}
private void updateAgedBrie(Item item) {
increaseQuality(item);
if (item.sellIn < 0) {
increaseQuality(item);
}
}
private void updateBackstagePass(Item item) {
increaseQuality(item);
if (item.sellIn < 10) {
increaseQuality(item);
}
if (item.sellIn < 5) {
increaseQuality(item);
}
if (item.sellIn < 0) {
item.quality = 0;
}
}
private void updateNormalItem(Item item) {
decreaseQuality(item);
if (item.sellIn < 0) {
decreaseQuality(item);
}
}
private void increaseQuality(Item item) {
if (item.quality < 50) {
item.quality = item.quality + 1;
}
}
private void decreaseQuality(Item item) {
if (item.quality > 0) {
item.quality = item.quality - 1;
}
}
}
Author: Clément DevosStatus: PublishedQuestion passed 305 times
Edit
0
Community EvaluationsNo one has reviewed this question yet, be the first!
Similar QuestionsMore questions about Java
4
This code allows to randomly get numbers between 1 to 31 in results. Should have declard SimpleDateFormat in the Thread.2
Write a function that returns the first character of a string in Java1
Java code that replaces keys in a template with their values.1
A Java class that converts Arabic numbers to Roman numerals.1
Write a Java implementation of the FizzBuzz code kata.