From 71ae3f3f0a660da33bfa8bc38ad6ccbeba7d59bf Mon Sep 17 00:00:00 2001 From: ihietani Date: Fri, 9 Sep 2016 16:44:08 +0300 Subject: [PATCH 1/2] Project Finished --- .../oulu/tol/sqat/tests/GildedRoseTest.java | 102 +++++++++++++++++- 1 file changed, 100 insertions(+), 2 deletions(-) diff --git a/src/fi/oulu/tol/sqat/tests/GildedRoseTest.java b/src/fi/oulu/tol/sqat/tests/GildedRoseTest.java index 7aceb2e..f58dc53 100644 --- a/src/fi/oulu/tol/sqat/tests/GildedRoseTest.java +++ b/src/fi/oulu/tol/sqat/tests/GildedRoseTest.java @@ -6,6 +6,7 @@ import java.util.List; import org.junit.Test; +import org.junit.Before; import fi.oulu.tol.sqat.GildedRose; import fi.oulu.tol.sqat.Item; @@ -19,11 +20,26 @@ public class GildedRoseTest { // Item("Sulfuras, Hand of Ragnaros", 0, 80)); // Item("Backstage passes to a TAFKAL80ETC concert", 15, 20)); // Item("Conjured Mana Cake", 3, 6)); + + public GildedRose store; + + public GildedRoseTest(){ + store = new GildedRose(); + } + @Before + public void addItemsToStore(){ + store.addItem(new Item("+5 Dexterity Vest", 10, 20)); + store.addItem(new Item("Aged Brie", 2, 0)); + store.addItem(new Item("Elixir of the Mongoose", 5, 7)); + store.addItem(new Item("Sulfuras, Hand of Ragnaros", 0, 80)); + store.addItem(new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20)); + store.addItem(new Item("Conjured Mana Cake", 3, 6)); + } @Test public void testUpdateEndOfDay_AgedBrie_Quality_10_11() { // Arrange - GildedRose store = new GildedRose(); + store = new GildedRose(); store.addItem(new Item("Aged Brie", 2, 10) ); // Act @@ -37,6 +53,88 @@ public void testUpdateEndOfDay_AgedBrie_Quality_10_11() { @Test public void testUpdateEndOfDay() { - fail("Test not implemented"); + //Arrange + + + //Act + store.updateEndOfDay(); + + //Assert + List items = store.getItems(); + Item itemDex = items.get(0); + assertEquals(19, itemDex.getQuality()); + } + @Test + public void testUpdateEndOfDaysNegative(){ + //Arrange + + + //Act + for(int i = 30; i > 0; i--){ + store.updateEndOfDay(); + } + List items = store.getItems(); + Item itemDex = items.get(0); + assertEquals(0, itemDex.getQuality()); + } + @Test + public void testUpdateEndOfDaysSeveralItems(){ + //Arrange + + //Act + store.updateEndOfDay(); + + List items = store.getItems(); + Item item = items.get(3); + assertEquals(80, item.getQuality()); + } + @Test + public void testUpdateSellIn(){ + //Arrange + + //Act + store.updateEndOfDay(); + + List items = store.getItems(); + Item item = items.get(2); + assertEquals(4, item.getSellIn()); + } + @Test + public void testQualityDecreaseFast(){ + //Arrange + + //Act + for(int i = 0; i < 12; i++){ + store.updateEndOfDay(); + } + + List items = store.getItems(); + Item item = items.get(0); + assertEquals(6, item.getQuality()); + } + @Test + public void testBrieMaxQuality(){ + //Arrange + + //Act + for(int i = 0; i < 60; i++){ + store.updateEndOfDay(); + } + + List items = store.getItems(); + Item item = items.get(1); + assertEquals(50, item.getQuality()); + } + @Test + public void tesBrieThreeDays(){ + //Arrange + + //Act + store.updateEndOfDay(); + store.updateEndOfDay(); + store.updateEndOfDay(); + List items = store.getItems(); + Item item = items.get(1); + assertEquals(0, item.getQuality()); } } From 0dfa1b7a8a4288ef71fa3d683ec0551fabefd3a1 Mon Sep 17 00:00:00 2001 From: Ilkka Date: Mon, 17 Oct 2016 16:48:48 +0300 Subject: [PATCH 2/2] Refactored --- src/fi/oulu/tol/sqat/GildedRose.java | 84 +++++++------------ src/fi/oulu/tol/sqat/Item.java | 24 ++++++ .../oulu/tol/sqat/tests/GildedRoseTest.java | 14 +++- 3 files changed, 66 insertions(+), 56 deletions(-) diff --git a/src/fi/oulu/tol/sqat/GildedRose.java b/src/fi/oulu/tol/sqat/GildedRose.java index 24741c6..f92351a 100644 --- a/src/fi/oulu/tol/sqat/GildedRose.java +++ b/src/fi/oulu/tol/sqat/GildedRose.java @@ -6,6 +6,10 @@ public class GildedRose { + private static final String SULFURAS = "Sulfuras, Hand of Ragnaros"; + private static final String BACKSTAGE_PASSES = "Backstage passes to a TAFKAL80ETC concert"; + private static final String AGED_BRIE = "Aged Brie"; + private static final int SELL_IN_TEN = 10, SELL_IN_FIVE = 5; private static List items = null; public List getItems() { @@ -21,78 +25,48 @@ public GildedRose() { } public static void updateEndOfDay() { - for (int i = 0; i < items.size(); i++) - { - if ((!"Aged Brie".equals(items.get(i).getName())) && !"Backstage passes to a TAFKAL80ETC concert".equals(items.get(i).getName())) + for(Item item:items){ + + if (!SULFURAS.equals(item.getName())) { - if (items.get(i).getQuality() > 0) - { - if (!"Sulfuras, Hand of Ragnaros".equals(items.get(i).getName())) - { - items.get(i).setQuality(items.get(i).getQuality() - 1); - } - } + item.decreaseSellIn(); } - else + + if ((AGED_BRIE.equals(item.getName())) || BACKSTAGE_PASSES.equals(item.getName())) { - if (items.get(i).getQuality() < 50) + if (BACKSTAGE_PASSES.equals(item.getName()) && item.getSellIn() <= SELL_IN_TEN) { - items.get(i).setQuality(items.get(i).getQuality() + 1); - - if ("Backstage passes to a TAFKAL80ETC concert".equals(items.get(i).getName())) + item.increaseQuality(); + + if (item.getSellIn() <= SELL_IN_FIVE) { - if (items.get(i).getSellIn() < 11) - { - if (items.get(i).getQuality() < 50) - { - items.get(i).setQuality(items.get(i).getQuality() + 1); - } - } - - if (items.get(i).getSellIn() < 6) - { - if (items.get(i).getQuality() < 50) - { - items.get(i).setQuality(items.get(i).getQuality() + 1); - } - } + item.increaseQuality(); } } + item.increaseQuality(); } - - if (!"Sulfuras, Hand of Ragnaros".equals(items.get(i).getName())) + else if (!SULFURAS.equals(item.getName())) { - items.get(i).setSellIn(items.get(i).getSellIn() - 1); + item.decreaseQuality(); } - if (items.get(i).getSellIn() < 0) + + if (item.isExpired()) { - if (!"Aged Brie".equals(items.get(i).getName())) + if (AGED_BRIE.equals(item.getName())) { - if (!"Backstage passes to a TAFKAL80ETC concert".equals(items.get(i).getName())) - { - if (items.get(i).getQuality() > 0) - { - if (!"Sulfuras, Hand of Ragnaros".equals(items.get(i).getName())) - { - items.get(i).setQuality(items.get(i).getQuality() - 1); - } - } - } - else - { - items.get(i).setQuality(items.get(i).getQuality() - items.get(i).getQuality()); - } + item.increaseQuality(); } - else + else if (BACKSTAGE_PASSES.equals(item.getName())) { - if (items.get(i).getQuality() < 50) - { - items.get(i).setQuality(items.get(i).getQuality() + 1); - } + item.setQuality(0); } + else if(!SULFURAS.equals(item.getName())) + { + item.decreaseQuality(); + } } } } - + } diff --git a/src/fi/oulu/tol/sqat/Item.java b/src/fi/oulu/tol/sqat/Item.java index 447b7fe..a64dec6 100644 --- a/src/fi/oulu/tol/sqat/Item.java +++ b/src/fi/oulu/tol/sqat/Item.java @@ -2,6 +2,9 @@ public class Item { + + final int MAX_QUALITY = 50, MIN_QUALITY = 0; + String name; int sellIn; int quality; @@ -31,5 +34,26 @@ public int getQuality() { public void setQuality(int quality) { this.quality = quality; } + public void decreaseQuality(){ + if(this.quality > MIN_QUALITY){ + this.quality--; + } + } + public void increaseQuality(){ + if(this.quality < MAX_QUALITY){ + this.quality++; + } + } + public void decreaseSellIn(){ + this.sellIn--; + } + public boolean isExpired(){ + if(this.sellIn < 0){ + return true; + } + else{ + return false; + } + } } diff --git a/src/fi/oulu/tol/sqat/tests/GildedRoseTest.java b/src/fi/oulu/tol/sqat/tests/GildedRoseTest.java index f58dc53..ec10ecd 100644 --- a/src/fi/oulu/tol/sqat/tests/GildedRoseTest.java +++ b/src/fi/oulu/tol/sqat/tests/GildedRoseTest.java @@ -135,6 +135,18 @@ public void tesBrieThreeDays(){ store.updateEndOfDay(); List items = store.getItems(); Item item = items.get(1); - assertEquals(0, item.getQuality()); + assertEquals(4, item.getQuality()); + } + @Test + public void testUpdateEndOfDay_AgedBrie_Quality_0_10() { + // Arrange + GildedRose store1 = new GildedRose(); + store1.addItem(new Item("Aged Brie", 0, 10) ); + // Act + store1.updateEndOfDay(); + // Assert + int quality = store.getItems().get(0).getQuality(); + String failMessage = "The Quality of Aged Brie increases twice afterSellIn date has passed"; + assertEquals(failMessage, 12, quality); } }