Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edited comments in GildedRose Class #554

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 25 additions & 48 deletions Java/src/main/java/com/gildedrose/GildedRose.java
Original file line number Diff line number Diff line change
@@ -1,61 +1,38 @@
package com.gildedrose;

import com.gildedrose.ItemUpdater;

class GildedRose {
Item[] items;
Item[] items; // Inventory*

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;
}
// Instantiate updaters for each item type
ItemUpdater defaultUpdater = new DefaultItemUpdater();
ItemUpdater agedBrieUpdater = new AgedBrieUpdater();
ItemUpdater backstagePassUpdater = new BackStagePassUpdater();
ItemUpdater conjuredItemUpdater = new ConjuredItemUpdater();

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;
}
}
// Iterate through items and call corresponding updater
for (Item item : items) {
String name = item.name;
switch (name) {
case "Aged Brie":
agedBrieUpdater.update(item);
break;
case "Backstage passes to a TAFKAL80ETC concert":
backstagePassUpdater.update(item);
break;
case "Sulfuras, Hand of Ragnaros":
break;
case "Conjured":
conjuredItemUpdater.update(item);
break;
default:
defaultUpdater.update(item);
}
}
}
Expand Down
52 changes: 52 additions & 0 deletions Java/src/main/java/com/gildedrose/ItemUpdater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.gildedrose;

public interface ItemUpdater {
void update(Item item);
}

class DefaultItemUpdater implements ItemUpdater {
@Override
public void update(Item item) {
decreaseQuality(item);
item.sellIn--;
if (item.sellIn < 0)
decreaseQuality(item);
}

protected void decreaseQuality(Item item) {
if (item.quality > 0)
item.quality--;
}
}

class AgedBrieUpdater implements ItemUpdater {
@Override
public void update(Item item) {
if (item.quality < 50)
item.quality++;
item.sellIn--;
}
}

class BackStagePassUpdater implements ItemUpdater {
@Override
public void update(Item item) {
if (item.quality < 50)
item.quality++;
if (item.sellIn <= 10 && item.quality < 50)
item.quality++;
if (item.sellIn <= 5 && item.quality < 50)
item.quality++;
item.sellIn--;
if (item.sellIn < 0)
item.quality = 0;
}
}

class ConjuredItemUpdater extends DefaultItemUpdater {
@Override
protected void decreaseQuality(Item item) {
if (item.quality > 0)
item.quality -= 2;
}
}
87 changes: 84 additions & 3 deletions Java/src/test/java/com/gildedrose/GildedRoseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,96 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class GildedRoseTest {

@Test
void foo() {
Item[] items = new Item[] { new Item("foo", 0, 0) };
void ElixirDepreciates() throws Exception{
Item[] items = new Item[]{new Item("Elixir", 0, 40)};
GildedRose app = new GildedRose(items);
app.updateQuality();
assertEquals("fixme", app.items[0].name);
app.updateQuality();
assertEquals(-2, app.items[0].sellIn);
assertEquals(36, app.items[0].quality);
}

// TODO Write test cases for each item. Check Edge cases.

@Test
void backStage_Pass_Expires_After_Concert() throws Exception {
Item[] items = new Item[]{new Item("Backstage passes to a TAFKAL80ETC concert", 2, 15)};
GildedRose app = new GildedRose(items);
app.updateQuality();
app.updateQuality();
app.updateQuality();
assertEquals(-1, app.items[0].sellIn);
assertEquals(0, app.items[0].quality);
}

@Test
public void backStage_quality_increases_twiceAsFast_10daysOrLess_before_concert() throws Exception {
Item[] items = new Item[]{new Item("Backstage passes to a TAFKAL80ETC concert", 10, 20)};
GildedRose app = new GildedRose(items);

app.updateQuality();

assertEquals(9, app.items[0].sellIn);
assertEquals(22, app.items[0].quality);
}

@Test
public void quality_Never_Below_zero() throws Exception {
Item[] items = new Item[]{new Item("Bread", 10, 0)};
GildedRose app = new GildedRose(items);

app.updateQuality();

assertEquals(9, app.items[0].sellIn);
assertEquals(0, app.items[0].quality);
}

@Test
public void sulfuras_hasConstant_quality_and_sellIn() throws Exception {
Item[] items = new Item[]{new Item("Sulfuras, Hand of Ragnaros", 8, 30)};
GildedRose app = new GildedRose(items);

app.updateQuality();

assertEquals(8, app.items[0].sellIn);
assertEquals(30, app.items[0].quality);
}

@Test
public void aged_brie_quality_isMax50() throws Exception {
Item[] items = new Item[]{new Item("Aged Brie", 10, 50)};
GildedRose app = new GildedRose(items);

app.updateQuality();

assertEquals(9, app.items[0].sellIn);
assertEquals(50, app.items[0].quality);
}

@Test
public void backStage_quality_isMax50() throws Exception {
Item[] items = new Item[]{new Item("Backstage passes to a TAFKAL80ETC concert", 10, 50)};
GildedRose app = new GildedRose(items);

app.updateQuality();

assertEquals(9, app.items[0].sellIn);
assertEquals(50, app.items[0].quality);
}
@Test
public void anyItem_quality_isMax50() throws Exception {
Item[] items = new Item[]{new Item("Bread", 10, 50)};
GildedRose app = new GildedRose(items);

app.updateQuality();

assertEquals(9, app.items[0].sellIn);
assertEquals(49, app.items[0].quality);
}

}
2 changes: 1 addition & 1 deletion Java/src/test/java/com/gildedrose/TexttestFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void main(String[] args) {

GildedRose app = new GildedRose(items);

int days = 2;
int days = 20;
if (args.length > 0) {
days = Integer.parseInt(args[0]) + 1;
}
Expand Down