refactoring

This commit is contained in:
GYENESE Tamas 2023-05-17 15:10:53 +02:00
parent 5a4e92199b
commit 276f9315bc
8 changed files with 154 additions and 86 deletions

View File

@ -1,62 +0,0 @@
package com.gildedrose;
class GildedRose {
Item[] items;
public GildedRose(Item[] items) {
this.items = items;
}
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;
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;
}
}
}
}
}
}

View File

@ -1,21 +0,0 @@
package com.gildedrose;
public class Item {
public String name;
public int sellIn;
public int quality;
public Item(String name, int sellIn, int quality) {
this.name = name;
this.sellIn = sellIn;
this.quality = quality;
}
@Override
public String toString() {
return this.name + ", " + this.sellIn + ", " + this.quality;
}
}

View File

@ -0,0 +1,13 @@
package com.gildedrose.common;
public final class Constants {
private Constants() {
}
public static final String ITEM_NAME_AGED_BRIE = "Aged Brie";
public static final String ITEM_NAME_BACKSTAGE_PASSES_TO_TAFKAL80ETC = "Backstage passes to a TAFKAL80ETC concert";
public static final String ITEM_NAME_BACKSTAGE_SULFURAS = "Sulfuras, Hand of Ragnaros";
public static final String ITEM_NAME_CONJURED = "Conjured";
}

View File

@ -0,0 +1,8 @@
package com.gildedrose.common;
public class InvalidQualityException extends Exception{
public InvalidQualityException(String message) {
super(message);
}
}

View File

@ -0,0 +1,45 @@
package com.gildedrose.domain;
public class Item {
private String name;
private int sellIn;
private int quality;
public Item(String name, int sellIn, int quality) {
this.name = name;
this.sellIn = sellIn;
this.quality = quality;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSellIn() {
return sellIn;
}
public void setSellIn(int sellIn) {
this.sellIn = sellIn;
}
public int getQuality() {
return quality;
}
public void setQuality(int quality) {
this.quality = quality;
}
@Override
public String toString() {
return this.name + ", " + this.sellIn + ", " + this.quality;
}
}

View File

@ -0,0 +1,78 @@
package com.gildedrose.service;
import com.gildedrose.common.InvalidQualityException;
import com.gildedrose.domain.Item;
import static com.gildedrose.common.Constants.*;
public class GildedRose {
public Item[] items;
public GildedRose(Item[] items) {
this.items = items;
}
public void updateQuality() throws InvalidQualityException {
for (Item item : items) {
switch (item.getName()) {
case ITEM_NAME_AGED_BRIE:
handleAgedBrie(item);
break;
case ITEM_NAME_BACKSTAGE_SULFURAS:
break;
case ITEM_NAME_BACKSTAGE_PASSES_TO_TAFKAL80ETC:
handleBackStageItem(item);
break;
case ITEM_NAME_CONJURED:
handleConjuredItem(item);
break;
default:
handleOtherItem(item);
}
}
}
private void validateQuality(Item item, final int min, final int max) throws InvalidQualityException {
if (item.getQuality() < min) {
throw new InvalidQualityException("The quality of the item: " + item.getName() + " is less than " + min + "!");
}
if (item.getQuality() > 50) {
throw new InvalidQualityException("The quality of the item: " + item.getName() + " is more than " + max + "!");
}
}
private void handleAgedBrie(Item item) throws InvalidQualityException {
validateQuality(item, 0, 50);
item.setQuality(item.getQuality() + 1);
}
private void handleBackStageItem(Item item) throws InvalidQualityException {
validateQuality(item, 0, 50);
int changeBy = 0;
if (item.getSellIn() <= 10) {
changeBy += 2;
}
if (item.getSellIn() <= 5) {
changeBy++;
}
if (item.getSellIn() < 0) {
changeBy = 0;
}
item.setQuality(changeBy);
}
private void handleConjuredItem(Item item) throws InvalidQualityException {
validateQuality(item, 0, 80);
item.setQuality(item.getQuality() - 2);
}
private void handleOtherItem(Item item) throws InvalidQualityException {
validateQuality(item, 0, 50);
if (item.getSellIn() > 0) {
item.setQuality(item.getQuality() - 1);
} else {
item.setQuality(item.getQuality() - 2);
}
}
}

View File

@ -1,5 +1,8 @@
package com.gildedrose;
import com.gildedrose.common.InvalidQualityException;
import com.gildedrose.domain.Item;
import com.gildedrose.service.GildedRose;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -7,11 +10,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
class GildedRoseTest {
@Test
void foo() {
void foo() throws InvalidQualityException {
Item[] items = new Item[] { new Item("foo", 0, 0) };
GildedRose app = new GildedRose(items);
app.updateQuality();
assertEquals("fixme", app.items[0].name);
assertEquals("fixme", app.items[0].getName());
}
}

View File

@ -1,7 +1,11 @@
package com.gildedrose;
import com.gildedrose.common.InvalidQualityException;
import com.gildedrose.domain.Item;
import com.gildedrose.service.GildedRose;
public class TexttestFixture {
public static void main(String[] args) {
public static void main(String[] args) throws InvalidQualityException {
System.out.println("OMGHAI!");
Item[] items = new Item[] {