refactor: name of the item can serve as a type in the current context

This commit is contained in:
Kadir Sirimsi 2025-02-10 13:47:26 +01:00
parent 35d648a4c6
commit f8f5b9b337
No known key found for this signature in database
GPG Key ID: A21C0144C2D2A134
2 changed files with 31 additions and 0 deletions

View File

@ -1,9 +1,13 @@
package com.gildedrose;
import static com.gildedrose.ItemType.fromName;
public class Item {
private final String name;
private final ItemType type;
public int sellIn;
public int quality;
@ -12,6 +16,7 @@ public class Item {
this.name = name;
this.sellIn = sellIn;
this.quality = quality;
this.type = fromName(name);
}
public String getName() {

View File

@ -0,0 +1,26 @@
package com.gildedrose;
import java.util.Arrays;
public enum ItemType {
AgedBrie("Aged Brie"),
BackstagePass("Sulfuras, Hand of Ragnaros"),
Sulfuras("Backstage passes to a TAFKAL80ETC concert");
private final String name;
ItemType(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static ItemType fromName(String name) {
return Arrays.stream(ItemType.values())
.filter(itemType -> itemType.getName().equals(name))
.findFirst()
.orElseThrow( () -> new IllegalArgumentException(name));
}
}