Created
March 10, 2021 17:06
-
-
Save zivce/467d48e0fefc8e3b7d9e513534fa9e5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gildedrose; | |
import com.google.common.collect.ImmutableMap; | |
import java.lang.reflect.InvocationTargetException; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class GildedRose { | |
private static final Class<Item> DEFAULT_CLASS = Item.class; | |
private static final ImmutableMap<String, String> SPECIALIZED_CLASSES = | |
ImmutableMap.of("normal", Normal.class.getName(), | |
"Aged Brie", Brie.class.getName(), | |
"Backstage passes..", Backstage.class.getName()); | |
private List<Item> items; | |
GildedRose(final List<Item> items) { | |
this.items = items; | |
} | |
public void updateQuality() { | |
this.items = items.stream().map(this::qualityUpdater).collect(Collectors.toList()); | |
} | |
private Item qualityUpdater(final Item item) { | |
Class<?> clazz; | |
try { | |
clazz = Class.forName(SPECIALIZED_CLASSES.get(item.getName())); | |
} catch (final ClassNotFoundException e) { | |
clazz = DEFAULT_CLASS; | |
} | |
try { | |
return (Item) clazz.getConstructor(String.class, int.class, int.class) | |
.newInstance(item.getName(), item.getQuality(), item.getDaysRemaining()); | |
} catch (final InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { | |
// log failure for the item here | |
e.printStackTrace(); | |
} | |
// null item means it is unknown at the moment | |
return null; | |
} | |
} | |
class Item { | |
private final String name; | |
protected int quality; | |
protected int daysRemaining; | |
Item(final String name, final int quality, final int daysRemaining) { | |
this.name = name; | |
this.quality = quality; | |
this.daysRemaining = daysRemaining; | |
} | |
protected void tick() { | |
} | |
public String getName() { | |
return name; | |
} | |
public int getDaysRemaining() { | |
return daysRemaining; | |
} | |
public int getQuality() { | |
return quality; | |
} | |
} | |
class Normal extends Item { | |
Normal(final String name, final int quality, final int daysRemaining) { | |
super(name, quality, daysRemaining); | |
} | |
@Override | |
protected void tick() { | |
daysRemaining--; | |
if (quality == 0) { | |
return; | |
} | |
quality--; | |
if (daysRemaining <= 0) { | |
quality--; | |
} | |
} | |
} | |
class Brie extends Item { | |
Brie(final String name, final int quality, final int daysRemaining) { | |
super(name, quality, daysRemaining); | |
} | |
@Override | |
protected void tick() { | |
daysRemaining--; | |
if (quality >= 50) { | |
return; | |
} | |
quality++; | |
if (daysRemaining <= 0) { | |
quality++; | |
} | |
} | |
} | |
class Backstage extends Item { | |
Backstage(final String name, final int quality, final int daysRemaining) { | |
super(name, quality, daysRemaining); | |
} | |
@Override | |
protected void tick() { | |
daysRemaining--; | |
if (quality >= 50) { | |
return; | |
} | |
if (daysRemaining < 0) { | |
quality = 0; | |
} | |
quality++; | |
if (daysRemaining < 10) { | |
quality++; | |
} | |
if (daysRemaining < 5) { | |
quality++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment