mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-09 19:51:41 +00:00
refactor: name of the item can serve as a type in the current context
This commit is contained in:
parent
35d648a4c6
commit
f8f5b9b337
@ -1,9 +1,13 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import static com.gildedrose.ItemType.fromName;
|
||||||
|
|
||||||
public class Item {
|
public class Item {
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
|
private final ItemType type;
|
||||||
|
|
||||||
public int sellIn;
|
public int sellIn;
|
||||||
|
|
||||||
public int quality;
|
public int quality;
|
||||||
@ -12,6 +16,7 @@ public class Item {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
this.sellIn = sellIn;
|
this.sellIn = sellIn;
|
||||||
this.quality = quality;
|
this.quality = quality;
|
||||||
|
this.type = fromName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
|||||||
26
Java/src/main/java/com/gildedrose/ItemType.java
Normal file
26
Java/src/main/java/com/gildedrose/ItemType.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user