Created
January 25, 2019 22:58
-
-
Save ptagr/679cdae89d14a2b5970885a858f79b37 to your computer and use it in GitHub Desktop.
carta.java
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
public class Item { | |
public Item(String name, int daysLeft, int quality) { | |
this.name = name; | |
this.daysLeft = daysLeft; | |
this.quality = quality; | |
} | |
public String name; | |
public int daysLeft; | |
public int quality; | |
public int hashCode() { | |
int result = 0; | |
result = ((result * 5) ^ this.name.hashCode()); | |
result = ((result * 7) ^ this.daysLeft); | |
result = ((result * 11) ^ this.quality); | |
return result; | |
} | |
public boolean equals(Object obj) { | |
if (obj == null) { | |
return false; | |
} | |
if (!(obj instanceof Item)) { | |
return false; | |
} | |
Item other = (Item) obj; | |
return | |
this.name.equals(other.name) | |
&& this.daysLeft == other.daysLeft | |
&& this.quality == other.quality; | |
} | |
public String toString() { | |
return String.format("<Item: %s, %s, %s>", this.name, this.daysLeft, this.quality); | |
} | |
} | |
import java.util.HashMap; | |
import java.util.Map; | |
public interface ItemHandlerInterface { | |
void updateItem(Item item); | |
} | |
class DefaultHandler implements ItemHandlerInterface { | |
@Override | |
public void updateItem(Item item) { | |
} | |
public void decreaseQuantity(Item item) { | |
item.daysLeft--; | |
} | |
} | |
class CheeseHandler extends DefaultHandler{ | |
@Override | |
public void updateItem(Item item) { | |
} | |
} | |
class ItemHandleFactory { | |
private static final Map<String, ItemHandlerInterface> handlerMap = new HashMap<>(); | |
public static ItemHandlerInterface getHandler(Item item) { | |
if(handlerMap.containsKey(item.name)) { | |
return handlerMap.get(item.name); | |
} | |
if(ItemType.CHEESE.getType().equals(item.name)) { | |
handlerMap.put(item.name, new CheeseHandler()); | |
} | |
return handlerMap.get(item.name); | |
} | |
} | |
import java.util.Objects; | |
public class ItemType { | |
public final static ItemType CHEESE = new ItemType("Cheese"); | |
public final static ItemType EMERALD = new ItemType("Emerald"); | |
public final static ItemType TICKET = new ItemType("Ticket"); | |
public final static ItemType DEFAULT = new ItemType("Default"); | |
private String type; | |
ItemType(String type) { | |
this.type = type; | |
} | |
public String getType() { | |
return type; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) { | |
return true; | |
} | |
if (o == null || getClass() != o.getClass()) { | |
return false; | |
} | |
ItemType itemType = (ItemType) o; | |
return this.type.equals(itemType.type); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(type); | |
} | |
} | |
// The Store | |
// | |
// We have items for sale, but most decrease in quality as they near their expiration. | |
// | |
// Some Information: | |
// | |
// * All items have a "daysLeft" value which is the number of days until expiration. | |
// * All items have a "quality" value which denotes how valuable the item is. | |
// * At the end of each day our system recalculates the values for every item. | |
// | |
// Pretty simple, right? Well this is where it gets interesting: | |
// | |
// * Usually, most items decrease in "quality" by 1 every day. | |
// * Once the sell by date (daysLeft) reaches 0, "quality" decreases by 2 every day. | |
// * The "quality" of an item is never negative. | |
// * "Cheese" increases in "quality" the older it gets. | |
// * "quality" increases by 1 when there are 11 days or more | |
// * "quality" increases by 2 when there are 10 days or less | |
// * "quality" increases by 3 when there are 5 days or less | |
// * "Ticket" works the same as "Cheese" EXCEPT: | |
// * "quality" becomes 0 once expired ("daysLeft" is 0) | |
// * The "quality" of an item is never more than 50. | |
// * "Emerald" is a unique item, its "quality" is always 80 and it never changes. | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.JUnitCore; | |
public class Solution { | |
// ############################################# | |
// Store | |
public static class Store { | |
public static Item[] updateQuality(Item[] items) { | |
for (Item item: items) { | |
ItemHandlerInterface itemHandler = ItemHandleFactory.getHandler(item); | |
itemHandler.updateItem(item); | |
if (((!"Cheese".equals(items[i].name)) && (!"Ticket".equals(items[i].name)))) { | |
// TODO: Improve this code | |
if ((items[i].quality > 0)) { | |
if ((!"Emerald".equals(items[i].name))) { | |
items[i].quality = (items[i].quality - 1); | |
} | |
} | |
} | |
else if ((items[i].quality < 50)) { | |
items[i].quality = (items[i].quality + 1); | |
if (("Cheese".equals(items[i].name))) { | |
if ((items[i].daysLeft < 6)) { | |
items[i].quality = (items[i].quality + 1); | |
// Increases the quality of the cheese | |
} | |
} | |
if (("Cheese".equals(items[i].name))) { | |
if ((items[i].daysLeft < 11)) { | |
items[i].quality = (items[i].quality + 1); | |
} | |
} | |
if (("Ticket".equals(items[i].name))) { | |
if ((items[i].daysLeft < 11)) { | |
// See commit fec84d on git. | |
if ((items[i].quality < 50)) { | |
items[i].quality = (items[i].quality + 1); | |
// Increases the quality of Ticker | |
} | |
} | |
if ((items[i].daysLeft < 6)) { | |
if ((items[i].quality < 50)) { | |
items[i].quality = (items[i].quality + 1); | |
} | |
} | |
} | |
} | |
if ((!"Emerald".equals(items[i].name))) { | |
items[i].daysLeft = (items[i].daysLeft - 1); | |
} | |
if ((items[i].daysLeft < 1)) { | |
if ((!"Cheese".equals(items[i].name))) { | |
if ((!"Ticket".equals(items[i].name))) { | |
if ((items[i].quality > 0)) { | |
if ((!"Emerald".equals(items[i].name))) { | |
items[i].quality = (items[i].quality - 1); | |
} | |
} | |
} | |
else { | |
// TODO: Fix this! | |
items[i].quality = (items[i].quality - items[i].quality); | |
} | |
} | |
else if ((items[i].quality > 50)) { | |
items[i].quality = 50; | |
// of for. | |
} | |
} | |
if ((!"Emerald".equals(items[i].name))) { | |
if ((items[i].quality > 50)) { | |
items[i].quality = 50; | |
} | |
} | |
} | |
return items; | |
} | |
} | |
// ############################################# | |
// Item | |
public Item[] items; | |
public Item[] result; | |
@Before | |
public void setup() { | |
this.items = new Item[1]; | |
this.result = new Item[1]; | |
} | |
@Test | |
public final void testRegularItemsDecreaseByOne() { | |
this.items[0] = new Item("Bread", 10, 20); | |
this.result = Store.updateQuality(this.items); | |
Assert.assertEquals(this.result[0], new Item("Bread", 9, 19)); | |
} | |
@Test | |
public final void testRegularItemQualityDoesNotGoBelowZero() { | |
this.items[0] = new Item("Bread", 2, 0); | |
this.result = Store.updateQuality(this.items); | |
Assert.assertEquals(this.result[0], new Item("Bread", 1, 0)); | |
} | |
@Test | |
public final void testQualityGoesUpForCheese() { | |
this.items[0] = new Item("Cheese", 20, 30); | |
this.result = Store.updateQuality(this.items); | |
Assert.assertEquals(this.result[0], new Item("Cheese", 19, 31)); | |
} | |
@Test | |
public final void testQualityGoesUpForTicket() { | |
this.items[0] = new Item("Ticket", 19, 31); | |
this.result = Store.updateQuality(this.items); | |
Assert.assertEquals(this.result[0], new Item("Ticket", 18, 32)); | |
} | |
@Test | |
public final void testQualityGoesUpBy2ForCheeseWith10DaysOrLessLeft() { | |
this.items[0] = new Item("Cheese", 10, 30); | |
this.result = Store.updateQuality(this.items); | |
Assert.assertEquals(this.result[0], new Item("Cheese", 9, 32)); | |
} | |
@Test() | |
public final void testQualityGoesUpBy2ForTicketWith10DaysOrLessLeft() { | |
this.items[0] = new Item("Ticket", 9, 31); | |
this.result = Store.updateQuality(this.items); | |
Assert.assertEquals(this.result[0], new Item("Ticket", 8, 33)); | |
} | |
@Test | |
public final void testQualityGoesUpBy3ForCheeseWith5DaysOrLessLeft() { | |
this.items[0] = new Item("Cheese", 4, 11); | |
this.result = Store.updateQuality(this.items); | |
Assert.assertEquals(this.result[0], new Item("Cheese", 3, 14)); | |
} | |
@Test | |
public final void testQualityGoesUpBy3ForTicketWith5DaysOrLessLeft() { | |
this.items[0] = new Item("Ticket", 4, 11); | |
this.result = Store.updateQuality(this.items); | |
Assert.assertEquals(this.result[0], new Item("Ticket", 3, 14)); | |
} | |
@Test | |
public final void testGoingFromUnexpiredToExpired() { | |
this.items[0] = new Item("Bread", 1, 11); | |
this.result = Store.updateQuality(this.items); | |
Assert.assertEquals(this.result[0], new Item("Bread", 0, 9)); | |
} | |
@Test | |
public final void testQualityDecreasesTwiceAsFastAfterExpired() { | |
this.items[0] = new Item("Bread", 0, 11); | |
this.result = Store.updateQuality(this.items); | |
Assert.assertEquals(this.result[0], new Item("Bread", -1, 9)); | |
} | |
@Test | |
public final void testCheeseQualityIncreasesBy3AfterExpired() { | |
this.items[0] = new Item("Cheese", 0, 20); | |
this.result = Store.updateQuality(this.items); | |
Assert.assertEquals(this.result[0], new Item("Cheese", -1, 23)); | |
} | |
@Test | |
public final void testTicketGoesToQuality0AfterExpired() { | |
this.items[0] = new Item("Ticket", 0, 20); | |
this.result = Store.updateQuality(this.items); | |
Assert.assertEquals(this.result[0], new Item("Ticket", -1, 0)); | |
} | |
@Test | |
public final void testEmerald() { | |
this.items[0] = new Item("Emerald", 0, 80); | |
this.result = Store.updateQuality(this.items); | |
Assert.assertEquals(this.result[0], new Item("Emerald", 0, 80)); | |
} | |
@Test | |
public final void testQualityDoesNotIncreasePast50() { | |
this.items[0] = new Item("Cheese", 4, 49); | |
this.result = Store.updateQuality(this.items); | |
Assert.assertEquals(this.result[0], new Item("Cheese", 3, 50)); | |
} | |
public static void main(String[] args) { | |
JUnitCore.main("Solution"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment