From 3cdbf4bc87a2bf57563a27b7b93b43685fae38c9 Mon Sep 17 00:00:00 2001 From: Manali Mogre Date: Sat, 15 Aug 2020 23:42:40 +0200 Subject: [PATCH 01/17] - Adding Test cases for existing code --- .../GildedRoseTests/GildedRoseTests.swift | 174 +++++++++++++++++- 1 file changed, 173 insertions(+), 1 deletion(-) diff --git a/swift/Tests/GildedRoseTests/GildedRoseTests.swift b/swift/Tests/GildedRoseTests/GildedRoseTests.swift index 67a7c251..22315f80 100644 --- a/swift/Tests/GildedRoseTests/GildedRoseTests.swift +++ b/swift/Tests/GildedRoseTests/GildedRoseTests.swift @@ -7,9 +7,181 @@ class GildedRoseTests: XCTestCase { let items = [Item(name: "foo", sellIn: 0, quality: 0)] let app = GildedRose(items: items); app.updateQuality(); - XCTAssertEqual("fixme", app.items[0].name); + XCTAssertEqual("foo", app.items[0].name); } + // MARK: - Test Cases for Standard Items + func testStandardItemsForSellInDatePassed() { + let items = [ Item(name: "Elixir of the Mongoose", sellIn: 1, quality: 7), + Item(name: "+5 Dexterity Vest", sellIn: 0, quality: 10) + ] + let app = GildedRose(items: items) + let days = 3 + for _ in 0.. () throws -> Void)] { return [ ("testFoo", testFoo), From 083029297599794dae18e5c078f206814cae94a3 Mon Sep 17 00:00:00 2001 From: Manali Mogre Date: Sun, 16 Aug 2020 14:13:53 +0200 Subject: [PATCH 02/17] - Adding Constants for Item Names - Refactoring Code with Switch cases --- swift/Sources/GildedRose/GildedRose.swift | 34 ++++++++++++++++++- .../GildedRose/GlidedRoseConstants.swift | 14 ++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 swift/Sources/GildedRose/GlidedRoseConstants.swift diff --git a/swift/Sources/GildedRose/GildedRose.swift b/swift/Sources/GildedRose/GildedRose.swift index 9b975467..a25d8117 100644 --- a/swift/Sources/GildedRose/GildedRose.swift +++ b/swift/Sources/GildedRose/GildedRose.swift @@ -6,6 +6,38 @@ public class GildedRose { } public func updateQuality() { + for item in items { + switch item.name { + case ItemNameConstants.kBackstagePassesItem: + item.sellIn = item.sellIn - 1 + if item.sellIn < 0 { + item.quality = 0 + } + else { + item.quality = item.quality < 50 ? item.quality + 1 : 50 + if item.sellIn < 10 { + item.quality = item.quality < 50 ? item.quality + 1 : 50 + } + if item.sellIn < 5 { + item.quality = item.quality < 50 ? item.quality + 1 : 50 + } + } + case ItemNameConstants.kAgedBrieItem: + item.sellIn = item.sellIn - 1 + item.quality = item.quality < 50 ? (item.quality + 1) : 50 + case ItemNameConstants.kSulfurasItem: + break + default: + item.sellIn = item.sellIn - 1 + item.quality = item.quality > 0 ? (item.quality - 1) : 0 + if(item.sellIn < 0) { + item.quality = item.quality > 0 ? (item.quality - 1) : 0 + } + } + } + } + + /* public func updateQuality() { for i in 0.. 0) { @@ -55,5 +87,5 @@ public class GildedRose { } } } - } + } */ } diff --git a/swift/Sources/GildedRose/GlidedRoseConstants.swift b/swift/Sources/GildedRose/GlidedRoseConstants.swift new file mode 100644 index 00000000..a869beec --- /dev/null +++ b/swift/Sources/GildedRose/GlidedRoseConstants.swift @@ -0,0 +1,14 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 16/08/2020. +// + +import Foundation + +struct ItemNameConstants { + static let kAgedBrieItem: String = "Aged Brie" + static let kBackstagePassesItem: String = "Backstage passes to a TAFKAL80ETC concert" + static let kSulfurasItem: String = "Sulfuras, Hand of Ragnaros" +} From f19d8141a4a0dc68d530e271026c7c5c5dd031ad Mon Sep 17 00:00:00 2001 From: Manali Mogre Date: Sun, 16 Aug 2020 14:25:03 +0200 Subject: [PATCH 03/17] - Creating separate files for different Items --- .../GildedRose/Items/AgedBrieItem.swift | 19 ++++++++++++++++++ .../Items/BackstagePassesItem.swift | 19 ++++++++++++++++++ .../GildedRose/Items/CustomisedItem.swift | 14 +++++++++++++ .../GildedRose/Items/StandardItem.swift | 20 +++++++++++++++++++ .../GildedRose/Items/SulfurasItem.swift | 19 ++++++++++++++++++ 5 files changed, 91 insertions(+) create mode 100644 swift/Sources/GildedRose/Items/AgedBrieItem.swift create mode 100644 swift/Sources/GildedRose/Items/BackstagePassesItem.swift create mode 100644 swift/Sources/GildedRose/Items/CustomisedItem.swift create mode 100644 swift/Sources/GildedRose/Items/StandardItem.swift create mode 100644 swift/Sources/GildedRose/Items/SulfurasItem.swift diff --git a/swift/Sources/GildedRose/Items/AgedBrieItem.swift b/swift/Sources/GildedRose/Items/AgedBrieItem.swift new file mode 100644 index 00000000..be140852 --- /dev/null +++ b/swift/Sources/GildedRose/Items/AgedBrieItem.swift @@ -0,0 +1,19 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 16/08/2020. +// + +import Foundation + +struct AgedBrieItem: CustomisedItemProtocol { + var item: Item + + public init(item: Item) { + self.item = item + } + func updateCustomItemQuality() { + + } +} diff --git a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift new file mode 100644 index 00000000..35c2b6b3 --- /dev/null +++ b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift @@ -0,0 +1,19 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 16/08/2020. +// + +import Foundation + +struct BackstagePassesItem: CustomisedItemProtocol { + var item: Item + + public init(item: Item) { + self.item = item + } + func updateCustomItemQuality() { + + } +} diff --git a/swift/Sources/GildedRose/Items/CustomisedItem.swift b/swift/Sources/GildedRose/Items/CustomisedItem.swift new file mode 100644 index 00000000..2bacf60b --- /dev/null +++ b/swift/Sources/GildedRose/Items/CustomisedItem.swift @@ -0,0 +1,14 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 16/08/2020. +// + +import Foundation + +protocol CustomisedItemProtocol { + func updateCustomItemQuality() +} + + diff --git a/swift/Sources/GildedRose/Items/StandardItem.swift b/swift/Sources/GildedRose/Items/StandardItem.swift new file mode 100644 index 00000000..85110f12 --- /dev/null +++ b/swift/Sources/GildedRose/Items/StandardItem.swift @@ -0,0 +1,20 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 16/08/2020. +// + +import Foundation + +struct StandardItem: CustomisedItemProtocol { + + var item: Item + + public init(item: Item) { + self.item = item + } + func updateCustomItemQuality() { + + } +} diff --git a/swift/Sources/GildedRose/Items/SulfurasItem.swift b/swift/Sources/GildedRose/Items/SulfurasItem.swift new file mode 100644 index 00000000..ae2006f7 --- /dev/null +++ b/swift/Sources/GildedRose/Items/SulfurasItem.swift @@ -0,0 +1,19 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 16/08/2020. +// + +import Foundation + +struct SulfurasItem: CustomisedItemProtocol { + var item: Item + + public init(item: Item) { + self.item = item + } + func updateCustomItemQuality() { + + } +} From d84a836ca6effb301d5a8e7c3c29a9a1696ff518 Mon Sep 17 00:00:00 2001 From: Manali Mogre Date: Sun, 16 Aug 2020 18:03:19 +0200 Subject: [PATCH 04/17] - Code Refactoring : Adding code for Custom Items --- .../GildedRose/CustomisedItemFactory.swift | 27 +++++++++++++++ swift/Sources/GildedRose/GildedRose.swift | 10 +++++- .../GildedRose/GlidedRoseConstants.swift | 6 ++++ .../GildedRose/Items/AgedBrieItem.swift | 9 +++-- .../Items/BackstagePassesItem.swift | 23 ++++++++++++- .../GildedRose/Items/CustomisedItem.swift | 34 +++++++++++++++++++ .../GildedRose/Items/StandardItem.swift | 18 +++++++++- .../GildedRose/Items/SulfurasItem.swift | 2 +- 8 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 swift/Sources/GildedRose/CustomisedItemFactory.swift diff --git a/swift/Sources/GildedRose/CustomisedItemFactory.swift b/swift/Sources/GildedRose/CustomisedItemFactory.swift new file mode 100644 index 00000000..40410e2e --- /dev/null +++ b/swift/Sources/GildedRose/CustomisedItemFactory.swift @@ -0,0 +1,27 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 16/08/2020. +// + +import Foundation + +class CustomisedItemFactory { + func getCustomisedItem(item: Item) -> CustomisedItemProtocol { + switch item.name { + case ItemNameConstants.kAgedBrieItem: + return AgedBrieItem(item: item) + case ItemNameConstants.kBackstagePassesItem: + return BackstagePassesItem(item: item) + case ItemNameConstants.kSulfurasItem: + return SulfurasItem(item: item) + default: + return StandardItem(item: item) + } + } +} + + + + diff --git a/swift/Sources/GildedRose/GildedRose.swift b/swift/Sources/GildedRose/GildedRose.swift index a25d8117..7790197b 100644 --- a/swift/Sources/GildedRose/GildedRose.swift +++ b/swift/Sources/GildedRose/GildedRose.swift @@ -6,6 +6,14 @@ public class GildedRose { } public func updateQuality() { + for item in items { + let customFactoryObj = CustomisedItemFactory() + let customItem = customFactoryObj.getCustomisedItem(item: item) + customItem.updateCustomItemQuality() + } + } + + /* public func updateQuality() { for item in items { switch item.name { case ItemNameConstants.kBackstagePassesItem: @@ -37,7 +45,7 @@ public class GildedRose { } } - /* public func updateQuality() { + public func updateQuality() { for i in 0.. 0) { diff --git a/swift/Sources/GildedRose/GlidedRoseConstants.swift b/swift/Sources/GildedRose/GlidedRoseConstants.swift index a869beec..4d4161e0 100644 --- a/swift/Sources/GildedRose/GlidedRoseConstants.swift +++ b/swift/Sources/GildedRose/GlidedRoseConstants.swift @@ -12,3 +12,9 @@ struct ItemNameConstants { static let kBackstagePassesItem: String = "Backstage passes to a TAFKAL80ETC concert" static let kSulfurasItem: String = "Sulfuras, Hand of Ragnaros" } + +struct ValueConstants { + static let kHightestQualityValue = 50 + static let kLowestQualityValue = 0 +} + diff --git a/swift/Sources/GildedRose/Items/AgedBrieItem.swift b/swift/Sources/GildedRose/Items/AgedBrieItem.swift index be140852..92191db2 100644 --- a/swift/Sources/GildedRose/Items/AgedBrieItem.swift +++ b/swift/Sources/GildedRose/Items/AgedBrieItem.swift @@ -7,13 +7,18 @@ import Foundation -struct AgedBrieItem: CustomisedItemProtocol { +struct AgedBrieItem: CustomisedItemProtocol, ItemSellInDaysProtocol, ItemQualityProtocol { var item: Item public init(item: Item) { self.item = item } func updateCustomItemQuality() { - + // update the sell in days + reduceSellInDaysByOne(item: item) + // Increment the Item quality by 1 if the quality is less than 50 + guard isItemUnderHighestQuality(item: item) else {return} + increaseQuality(for: item, by: 1) } + } diff --git a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift index 35c2b6b3..ab5ffeaf 100644 --- a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift +++ b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift @@ -7,13 +7,34 @@ import Foundation -struct BackstagePassesItem: CustomisedItemProtocol { +struct BackstagePassesItem: CustomisedItemProtocol, ItemQualityProtocol, ItemSellInDaysProtocol { var item: Item public init(item: Item) { self.item = item } func updateCustomItemQuality() { + reduceSellInDaysByOne(item: item) + if isSellInDaysMoreThanOrEqualTo(days: 10) { + increaseQuality(for: item, by: 1) + } + else if isSellInDaysMoreThanOrEqualTo(days: 5) { + increaseQuality(for: item, by: 2) + } + else if isSellInDaysMoreThanOrEqualTo(days: 0) { + increaseQuality(for: item, by: 3) + } + if HasSellInDatePassed(item: item) { + item.quality = ValueConstants.kLowestQualityValue + } + if !isItemUnderHighestQuality(item: item) { + item.quality = ValueConstants.kHightestQualityValue + } } + + private func isSellInDaysMoreThanOrEqualTo(days: Int) -> Bool { + return item.sellIn >= days + } + } diff --git a/swift/Sources/GildedRose/Items/CustomisedItem.swift b/swift/Sources/GildedRose/Items/CustomisedItem.swift index 2bacf60b..b3beff4e 100644 --- a/swift/Sources/GildedRose/Items/CustomisedItem.swift +++ b/swift/Sources/GildedRose/Items/CustomisedItem.swift @@ -12,3 +12,37 @@ protocol CustomisedItemProtocol { } +protocol ItemSellInDaysProtocol { + func reduceSellInDaysByOne(item: Item) +} + +extension ItemSellInDaysProtocol { + func reduceSellInDaysByOne(item: Item) { + item.sellIn -= 1 + } + + func HasSellInDatePassed(item: Item) -> Bool { + return item.sellIn < 0 + } +} + +protocol ItemQualityProtocol { + +} + +extension ItemQualityProtocol { + func isItemUnderHighestQuality(item: Item) -> Bool { + return item.quality < ValueConstants.kHightestQualityValue + } + func reduceQuality(for item: Item, by value:Int) { + item.quality -= value + } + + func increaseQuality(for item: Item, by value:Int) { + item.quality += value + } + + func isItemOverLowestQuality(item: Item) -> Bool { + return item.quality > ValueConstants.kLowestQualityValue + } +} diff --git a/swift/Sources/GildedRose/Items/StandardItem.swift b/swift/Sources/GildedRose/Items/StandardItem.swift index 85110f12..6552e96e 100644 --- a/swift/Sources/GildedRose/Items/StandardItem.swift +++ b/swift/Sources/GildedRose/Items/StandardItem.swift @@ -7,7 +7,7 @@ import Foundation -struct StandardItem: CustomisedItemProtocol { +struct StandardItem: CustomisedItemProtocol, ItemQualityProtocol, ItemSellInDaysProtocol { var item: Item @@ -15,6 +15,22 @@ struct StandardItem: CustomisedItemProtocol { self.item = item } func updateCustomItemQuality() { + // Reduce the sellIn days for Item by 1 + reduceSellInDaysByOne(item: item) + // Reduce the item quality by 1 , if the sell in date is passed decrement by 2 + HasSellInDatePassed(item: item) ? reduceQuality(for: item, by: decreasingValueForZeroOrLessDaysToSell()) : reduceQuality(for: item, by: decreasingValueOverZeroDaysToSell()) + guard isItemOverLowestQuality(item: item) else { + item.quality = ValueConstants.kLowestQualityValue + return + } + } + + func decreasingValueOverZeroDaysToSell() -> Int { + return 1 + } + + private func decreasingValueForZeroOrLessDaysToSell() -> Int { + return 2 * decreasingValueOverZeroDaysToSell() } } diff --git a/swift/Sources/GildedRose/Items/SulfurasItem.swift b/swift/Sources/GildedRose/Items/SulfurasItem.swift index ae2006f7..8ede0b84 100644 --- a/swift/Sources/GildedRose/Items/SulfurasItem.swift +++ b/swift/Sources/GildedRose/Items/SulfurasItem.swift @@ -14,6 +14,6 @@ struct SulfurasItem: CustomisedItemProtocol { self.item = item } func updateCustomItemQuality() { - + // No code as there is no change in quality or sell in days of sulfuras item } } From 5bec9bdff4c1c7bd7258155bd5cc99ed621653ec Mon Sep 17 00:00:00 2001 From: Manali Mogre Date: Sun, 16 Aug 2020 20:46:04 +0200 Subject: [PATCH 05/17] - Code Foramtting --- swift/Sources/GildedRose/GildedRose.swift | 92 +------------------ .../GildedRose/Items/AgedBrieItem.swift | 5 +- .../Items/BackstagePassesItem.swift | 30 +++--- .../GildedRose/Items/CustomisedItem.swift | 8 ++ .../GildedRose/Items/StandardItem.swift | 1 - 5 files changed, 30 insertions(+), 106 deletions(-) diff --git a/swift/Sources/GildedRose/GildedRose.swift b/swift/Sources/GildedRose/GildedRose.swift index 7790197b..4644e48e 100644 --- a/swift/Sources/GildedRose/GildedRose.swift +++ b/swift/Sources/GildedRose/GildedRose.swift @@ -6,94 +6,8 @@ public class GildedRose { } public func updateQuality() { - for item in items { - let customFactoryObj = CustomisedItemFactory() - let customItem = customFactoryObj.getCustomisedItem(item: item) - customItem.updateCustomItemQuality() - } + let customFactoryObj = CustomisedItemFactory() + _ = items.map({customFactoryObj.getCustomisedItem(item: $0).updateCustomItemQuality()}) + } - - /* public func updateQuality() { - for item in items { - switch item.name { - case ItemNameConstants.kBackstagePassesItem: - item.sellIn = item.sellIn - 1 - if item.sellIn < 0 { - item.quality = 0 - } - else { - item.quality = item.quality < 50 ? item.quality + 1 : 50 - if item.sellIn < 10 { - item.quality = item.quality < 50 ? item.quality + 1 : 50 - } - if item.sellIn < 5 { - item.quality = item.quality < 50 ? item.quality + 1 : 50 - } - } - case ItemNameConstants.kAgedBrieItem: - item.sellIn = item.sellIn - 1 - item.quality = item.quality < 50 ? (item.quality + 1) : 50 - case ItemNameConstants.kSulfurasItem: - break - default: - item.sellIn = item.sellIn - 1 - item.quality = item.quality > 0 ? (item.quality - 1) : 0 - if(item.sellIn < 0) { - item.quality = item.quality > 0 ? (item.quality - 1) : 0 - } - } - } - } - - public func updateQuality() { - for i in 0.. 0) { - if (items[i].name != "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 == "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 != "Sulfuras, Hand of Ragnaros") { - items[i].sellIn = items[i].sellIn - 1 - } - - if (items[i].sellIn < 0) { - if (items[i].name != "Aged Brie") { - if (items[i].name != "Backstage passes to a TAFKAL80ETC concert") { - if (items[i].quality > 0) { - if (items[i].name != "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 - } - } - } - } - } */ } diff --git a/swift/Sources/GildedRose/Items/AgedBrieItem.swift b/swift/Sources/GildedRose/Items/AgedBrieItem.swift index 92191db2..b3a17d37 100644 --- a/swift/Sources/GildedRose/Items/AgedBrieItem.swift +++ b/swift/Sources/GildedRose/Items/AgedBrieItem.swift @@ -9,13 +9,16 @@ import Foundation struct AgedBrieItem: CustomisedItemProtocol, ItemSellInDaysProtocol, ItemQualityProtocol { var item: Item - + public init(item: Item) { self.item = item } + func updateCustomItemQuality() { // update the sell in days + reduceSellInDaysByOne(item: item) + // Increment the Item quality by 1 if the quality is less than 50 guard isItemUnderHighestQuality(item: item) else {return} increaseQuality(for: item, by: 1) diff --git a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift index ab5ffeaf..1d040ced 100644 --- a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift +++ b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift @@ -13,28 +13,28 @@ struct BackstagePassesItem: CustomisedItemProtocol, ItemQualityProtocol, ItemSel public init(item: Item) { self.item = item } + func updateCustomItemQuality() { reduceSellInDaysByOne(item: item) - if isSellInDaysMoreThanOrEqualTo(days: 10) { + guard !HasSellInDatePassed(item: item) else { + setItemQualityToZero(item: item) + return + } + guard isItemUnderHighestQuality(item: item) else { + setItemQualityToFifty(item: item) + return + } + switch item.sellIn { + case 10...: increaseQuality(for: item, by: 1) - } - else if isSellInDaysMoreThanOrEqualTo(days: 5) { + case 5..<10: increaseQuality(for: item, by: 2) - } - else if isSellInDaysMoreThanOrEqualTo(days: 0) { + case 0..<5: increaseQuality(for: item, by: 3) - } - if HasSellInDatePassed(item: item) { - item.quality = ValueConstants.kLowestQualityValue - } - if !isItemUnderHighestQuality(item: item) { - item.quality = ValueConstants.kHightestQualityValue + default: + break } } - - private func isSellInDaysMoreThanOrEqualTo(days: Int) -> Bool { - return item.sellIn >= days - } } diff --git a/swift/Sources/GildedRose/Items/CustomisedItem.swift b/swift/Sources/GildedRose/Items/CustomisedItem.swift index b3beff4e..ce7f1f31 100644 --- a/swift/Sources/GildedRose/Items/CustomisedItem.swift +++ b/swift/Sources/GildedRose/Items/CustomisedItem.swift @@ -45,4 +45,12 @@ extension ItemQualityProtocol { func isItemOverLowestQuality(item: Item) -> Bool { return item.quality > ValueConstants.kLowestQualityValue } + + func setItemQualityToZero(item: Item) { + item.quality = ValueConstants.kLowestQualityValue + } + + func setItemQualityToFifty(item: Item) { + item.quality = ValueConstants.kHightestQualityValue + } } diff --git a/swift/Sources/GildedRose/Items/StandardItem.swift b/swift/Sources/GildedRose/Items/StandardItem.swift index 6552e96e..34dd80c1 100644 --- a/swift/Sources/GildedRose/Items/StandardItem.swift +++ b/swift/Sources/GildedRose/Items/StandardItem.swift @@ -8,7 +8,6 @@ import Foundation struct StandardItem: CustomisedItemProtocol, ItemQualityProtocol, ItemSellInDaysProtocol { - var item: Item public init(item: Item) { From b2b61a1f57000b40d5270ca2f9b63ecc71fa0d0d Mon Sep 17 00:00:00 2001 From: Manali Mogre Date: Sun, 16 Aug 2020 21:04:26 +0200 Subject: [PATCH 06/17] - Updated Naming Conventions --- swift/Sources/GildedRose/GildedRose.swift | 2 +- .../Sources/GildedRose/Items/AgedBrieItem.swift | 9 ++++----- .../GildedRose/Items/BackstagePassesItem.swift | 8 ++++---- .../GildedRose/Items/CustomisedItem.swift | 16 ++++++++-------- .../Sources/GildedRose/Items/StandardItem.swift | 8 ++++---- .../Sources/GildedRose/Items/SulfurasItem.swift | 2 +- 6 files changed, 22 insertions(+), 23 deletions(-) diff --git a/swift/Sources/GildedRose/GildedRose.swift b/swift/Sources/GildedRose/GildedRose.swift index 4644e48e..d1a5702e 100644 --- a/swift/Sources/GildedRose/GildedRose.swift +++ b/swift/Sources/GildedRose/GildedRose.swift @@ -7,7 +7,7 @@ public class GildedRose { public func updateQuality() { let customFactoryObj = CustomisedItemFactory() - _ = items.map({customFactoryObj.getCustomisedItem(item: $0).updateCustomItemQuality()}) + _ = items.map({customFactoryObj.getCustomisedItem(item: $0).updateItemState()}) } } diff --git a/swift/Sources/GildedRose/Items/AgedBrieItem.swift b/swift/Sources/GildedRose/Items/AgedBrieItem.swift index b3a17d37..4ba7dac9 100644 --- a/swift/Sources/GildedRose/Items/AgedBrieItem.swift +++ b/swift/Sources/GildedRose/Items/AgedBrieItem.swift @@ -7,20 +7,19 @@ import Foundation -struct AgedBrieItem: CustomisedItemProtocol, ItemSellInDaysProtocol, ItemQualityProtocol { +struct AgedBrieItem: CustomisedItemProtocol, ItemSellInUpdater, ItemQualityUpdater { var item: Item public init(item: Item) { self.item = item } - func updateCustomItemQuality() { + func updateItemState() { // update the sell in days - - reduceSellInDaysByOne(item: item) + reduceSellInDays(for: item, by: 1) // Increment the Item quality by 1 if the quality is less than 50 - guard isItemUnderHighestQuality(item: item) else {return} + guard isItemUnderHighestQuality(item: item) else { return } increaseQuality(for: item, by: 1) } diff --git a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift index 1d040ced..4bd338ed 100644 --- a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift +++ b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift @@ -7,16 +7,16 @@ import Foundation -struct BackstagePassesItem: CustomisedItemProtocol, ItemQualityProtocol, ItemSellInDaysProtocol { +struct BackstagePassesItem: CustomisedItemProtocol, ItemQualityUpdater, ItemSellInUpdater { var item: Item public init(item: Item) { self.item = item } - func updateCustomItemQuality() { - reduceSellInDaysByOne(item: item) - + func updateItemState() { + reduceSellInDays(for: item, by: 1) + guard !HasSellInDatePassed(item: item) else { setItemQualityToZero(item: item) return diff --git a/swift/Sources/GildedRose/Items/CustomisedItem.swift b/swift/Sources/GildedRose/Items/CustomisedItem.swift index ce7f1f31..ef085513 100644 --- a/swift/Sources/GildedRose/Items/CustomisedItem.swift +++ b/swift/Sources/GildedRose/Items/CustomisedItem.swift @@ -8,17 +8,17 @@ import Foundation protocol CustomisedItemProtocol { - func updateCustomItemQuality() + func updateItemState() } -protocol ItemSellInDaysProtocol { - func reduceSellInDaysByOne(item: Item) +protocol ItemSellInUpdater { + func reduceSellInDays(for item: Item, by days: Int) } -extension ItemSellInDaysProtocol { - func reduceSellInDaysByOne(item: Item) { - item.sellIn -= 1 +extension ItemSellInUpdater { + func reduceSellInDays(for item: Item, by days: Int) { + item.sellIn -= days } func HasSellInDatePassed(item: Item) -> Bool { @@ -26,11 +26,11 @@ extension ItemSellInDaysProtocol { } } -protocol ItemQualityProtocol { +protocol ItemQualityUpdater { } -extension ItemQualityProtocol { +extension ItemQualityUpdater { func isItemUnderHighestQuality(item: Item) -> Bool { return item.quality < ValueConstants.kHightestQualityValue } diff --git a/swift/Sources/GildedRose/Items/StandardItem.swift b/swift/Sources/GildedRose/Items/StandardItem.swift index 34dd80c1..0d4145f0 100644 --- a/swift/Sources/GildedRose/Items/StandardItem.swift +++ b/swift/Sources/GildedRose/Items/StandardItem.swift @@ -7,16 +7,16 @@ import Foundation -struct StandardItem: CustomisedItemProtocol, ItemQualityProtocol, ItemSellInDaysProtocol { +struct StandardItem: CustomisedItemProtocol, ItemQualityUpdater, ItemSellInUpdater { var item: Item public init(item: Item) { self.item = item } - func updateCustomItemQuality() { + func updateItemState() { // Reduce the sellIn days for Item by 1 - reduceSellInDaysByOne(item: item) - + reduceSellInDays(for: item, by: 1) + // Reduce the item quality by 1 , if the sell in date is passed decrement by 2 HasSellInDatePassed(item: item) ? reduceQuality(for: item, by: decreasingValueForZeroOrLessDaysToSell()) : reduceQuality(for: item, by: decreasingValueOverZeroDaysToSell()) guard isItemOverLowestQuality(item: item) else { diff --git a/swift/Sources/GildedRose/Items/SulfurasItem.swift b/swift/Sources/GildedRose/Items/SulfurasItem.swift index 8ede0b84..1eb44e5c 100644 --- a/swift/Sources/GildedRose/Items/SulfurasItem.swift +++ b/swift/Sources/GildedRose/Items/SulfurasItem.swift @@ -13,7 +13,7 @@ struct SulfurasItem: CustomisedItemProtocol { public init(item: Item) { self.item = item } - func updateCustomItemQuality() { + func updateItemState() { // No code as there is no change in quality or sell in days of sulfuras item } } From d3636126741eb32820187df32822de85a5ad5c36 Mon Sep 17 00:00:00 2001 From: Manali Mogre Date: Mon, 17 Aug 2020 13:26:05 +0200 Subject: [PATCH 07/17] - Code Refactoring --- .../GildedRose/CustomisedItemFactory.swift | 6 +- swift/Sources/GildedRose/GildedRose.swift | 1 - .../GildedRose/ItemQualityUpdater.swift | 34 +++++++++++ .../GildedRose/ItemSellInUpdater.swift | 22 ++++++++ .../GildedRose/Items/AgedBrieItem.swift | 14 +++-- .../Items/BackstagePassesItem.swift | 21 ++++--- .../GildedRose/Items/CustomisedItem.swift | 56 ------------------- .../GildedRose/Items/StandardItem.swift | 11 ++-- .../GildedRose/Items/SulfurasItem.swift | 2 +- 9 files changed, 86 insertions(+), 81 deletions(-) create mode 100644 swift/Sources/GildedRose/ItemQualityUpdater.swift create mode 100644 swift/Sources/GildedRose/ItemSellInUpdater.swift delete mode 100644 swift/Sources/GildedRose/Items/CustomisedItem.swift diff --git a/swift/Sources/GildedRose/CustomisedItemFactory.swift b/swift/Sources/GildedRose/CustomisedItemFactory.swift index 40410e2e..8e16ec76 100644 --- a/swift/Sources/GildedRose/CustomisedItemFactory.swift +++ b/swift/Sources/GildedRose/CustomisedItemFactory.swift @@ -7,8 +7,12 @@ import Foundation +protocol CustomisedItem { + func updateItemState() +} + class CustomisedItemFactory { - func getCustomisedItem(item: Item) -> CustomisedItemProtocol { + func getCustomisedItem(item: Item) -> CustomisedItem { switch item.name { case ItemNameConstants.kAgedBrieItem: return AgedBrieItem(item: item) diff --git a/swift/Sources/GildedRose/GildedRose.swift b/swift/Sources/GildedRose/GildedRose.swift index d1a5702e..3b4746b5 100644 --- a/swift/Sources/GildedRose/GildedRose.swift +++ b/swift/Sources/GildedRose/GildedRose.swift @@ -8,6 +8,5 @@ public class GildedRose { public func updateQuality() { let customFactoryObj = CustomisedItemFactory() _ = items.map({customFactoryObj.getCustomisedItem(item: $0).updateItemState()}) - } } diff --git a/swift/Sources/GildedRose/ItemQualityUpdater.swift b/swift/Sources/GildedRose/ItemQualityUpdater.swift new file mode 100644 index 00000000..b4f74e8f --- /dev/null +++ b/swift/Sources/GildedRose/ItemQualityUpdater.swift @@ -0,0 +1,34 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 17/08/2020. +// + +import Foundation + +protocol ItemQualityUpdater { + var item: Item {get set} +} + +extension ItemQualityUpdater { + var isItemUnderHighestQuality: Bool { + return item.quality < ValueConstants.kHightestQualityValue + } + + var isItemMoreThanLowestQuality: Bool { + return item.quality > ValueConstants.kLowestQualityValue + } + + func reduceItemQuality(by value:Int) { + item.quality -= value + } + + func increaseItemQuality(by value:Int) { + item.quality += value + } + + func setItemQuality(to value: Int){ + item.quality = value + } +} diff --git a/swift/Sources/GildedRose/ItemSellInUpdater.swift b/swift/Sources/GildedRose/ItemSellInUpdater.swift new file mode 100644 index 00000000..7e4cfce0 --- /dev/null +++ b/swift/Sources/GildedRose/ItemSellInUpdater.swift @@ -0,0 +1,22 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 17/08/2020. +// + +import Foundation + +protocol ItemSellInUpdater { + var item: Item { get set } + func reduceSellInDays(by days: Int) +} + +extension ItemSellInUpdater { + var isSellInDatePassed: Bool{ + return item.sellIn < 0 + } + func reduceSellInDays(by days: Int) { + item.sellIn -= days + } +} diff --git a/swift/Sources/GildedRose/Items/AgedBrieItem.swift b/swift/Sources/GildedRose/Items/AgedBrieItem.swift index 4ba7dac9..5c1fbe91 100644 --- a/swift/Sources/GildedRose/Items/AgedBrieItem.swift +++ b/swift/Sources/GildedRose/Items/AgedBrieItem.swift @@ -7,20 +7,22 @@ import Foundation -struct AgedBrieItem: CustomisedItemProtocol, ItemSellInUpdater, ItemQualityUpdater { +struct AgedBrieItem: CustomisedItem, ItemSellInUpdater, ItemQualityUpdater { var item: Item - + public init(item: Item) { self.item = item } func updateItemState() { // update the sell in days - reduceSellInDays(for: item, by: 1) + reduceSellInDays(by: 1) // Increment the Item quality by 1 if the quality is less than 50 - guard isItemUnderHighestQuality(item: item) else { return } - increaseQuality(for: item, by: 1) + guard isItemUnderHighestQuality else { return } + increaseItemQuality(by: 1) } - + + + } diff --git a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift index 4bd338ed..b989e137 100644 --- a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift +++ b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift @@ -7,7 +7,7 @@ import Foundation -struct BackstagePassesItem: CustomisedItemProtocol, ItemQualityUpdater, ItemSellInUpdater { +struct BackstagePassesItem: CustomisedItem, ItemQualityUpdater, ItemSellInUpdater { var item: Item public init(item: Item) { @@ -15,26 +15,25 @@ struct BackstagePassesItem: CustomisedItemProtocol, ItemQualityUpdater, ItemSell } func updateItemState() { - reduceSellInDays(for: item, by: 1) - - guard !HasSellInDatePassed(item: item) else { - setItemQualityToZero(item: item) + reduceSellInDays(by: 1) + + guard !isSellInDatePassed else { + setItemQuality(to: ValueConstants.kLowestQualityValue) return } - guard isItemUnderHighestQuality(item: item) else { - setItemQualityToFifty(item: item) + guard isItemUnderHighestQuality else { + setItemQuality(to: ValueConstants.kHightestQualityValue) return } switch item.sellIn { case 10...: - increaseQuality(for: item, by: 1) + increaseItemQuality(by: 1) case 5..<10: - increaseQuality(for: item, by: 2) + increaseItemQuality(by: 2) case 0..<5: - increaseQuality(for: item, by: 3) + increaseItemQuality(by: 3) default: break } } - } diff --git a/swift/Sources/GildedRose/Items/CustomisedItem.swift b/swift/Sources/GildedRose/Items/CustomisedItem.swift deleted file mode 100644 index ef085513..00000000 --- a/swift/Sources/GildedRose/Items/CustomisedItem.swift +++ /dev/null @@ -1,56 +0,0 @@ -// -// File.swift -// -// -// Created by Manali Mogre on 16/08/2020. -// - -import Foundation - -protocol CustomisedItemProtocol { - func updateItemState() -} - - -protocol ItemSellInUpdater { - func reduceSellInDays(for item: Item, by days: Int) -} - -extension ItemSellInUpdater { - func reduceSellInDays(for item: Item, by days: Int) { - item.sellIn -= days - } - - func HasSellInDatePassed(item: Item) -> Bool { - return item.sellIn < 0 - } -} - -protocol ItemQualityUpdater { - -} - -extension ItemQualityUpdater { - func isItemUnderHighestQuality(item: Item) -> Bool { - return item.quality < ValueConstants.kHightestQualityValue - } - func reduceQuality(for item: Item, by value:Int) { - item.quality -= value - } - - func increaseQuality(for item: Item, by value:Int) { - item.quality += value - } - - func isItemOverLowestQuality(item: Item) -> Bool { - return item.quality > ValueConstants.kLowestQualityValue - } - - func setItemQualityToZero(item: Item) { - item.quality = ValueConstants.kLowestQualityValue - } - - func setItemQualityToFifty(item: Item) { - item.quality = ValueConstants.kHightestQualityValue - } -} diff --git a/swift/Sources/GildedRose/Items/StandardItem.swift b/swift/Sources/GildedRose/Items/StandardItem.swift index 0d4145f0..e1dbd016 100644 --- a/swift/Sources/GildedRose/Items/StandardItem.swift +++ b/swift/Sources/GildedRose/Items/StandardItem.swift @@ -7,20 +7,21 @@ import Foundation -struct StandardItem: CustomisedItemProtocol, ItemQualityUpdater, ItemSellInUpdater { +struct StandardItem: CustomisedItem, ItemQualityUpdater, ItemSellInUpdater { var item: Item public init(item: Item) { self.item = item } + func updateItemState() { // Reduce the sellIn days for Item by 1 - reduceSellInDays(for: item, by: 1) + reduceSellInDays(by: 1) // Reduce the item quality by 1 , if the sell in date is passed decrement by 2 - HasSellInDatePassed(item: item) ? reduceQuality(for: item, by: decreasingValueForZeroOrLessDaysToSell()) : reduceQuality(for: item, by: decreasingValueOverZeroDaysToSell()) - guard isItemOverLowestQuality(item: item) else { - item.quality = ValueConstants.kLowestQualityValue + isSellInDatePassed ? reduceItemQuality(by: decreasingValueForZeroOrLessDaysToSell()) : reduceItemQuality(by: decreasingValueOverZeroDaysToSell()) + guard isItemMoreThanLowestQuality else { + setItemQuality(to: ValueConstants.kLowestQualityValue) return } } diff --git a/swift/Sources/GildedRose/Items/SulfurasItem.swift b/swift/Sources/GildedRose/Items/SulfurasItem.swift index 1eb44e5c..064de3dc 100644 --- a/swift/Sources/GildedRose/Items/SulfurasItem.swift +++ b/swift/Sources/GildedRose/Items/SulfurasItem.swift @@ -7,7 +7,7 @@ import Foundation -struct SulfurasItem: CustomisedItemProtocol { +struct SulfurasItem: CustomisedItem { var item: Item public init(item: Item) { From db0db19d5d2520b4bb437506d510a87155da6f86 Mon Sep 17 00:00:00 2001 From: Manali Mogre Date: Mon, 17 Aug 2020 20:14:47 +0200 Subject: [PATCH 08/17] - Code Refactoring --- swift/Sources/GildedRose/CustomisedItemFactory.swift | 2 ++ swift/Sources/GildedRose/ItemQualityUpdater.swift | 6 ++++-- swift/Sources/GildedRose/ItemSellInUpdater.swift | 3 +-- swift/Sources/GildedRose/Items/AgedBrieItem.swift | 6 +++--- .../GildedRose/Items/BackstagePassesItem.swift | 11 +++++++---- swift/Sources/GildedRose/Items/StandardItem.swift | 6 ++++-- swift/Sources/GildedRose/Items/SulfurasItem.swift | 1 + 7 files changed, 22 insertions(+), 13 deletions(-) diff --git a/swift/Sources/GildedRose/CustomisedItemFactory.swift b/swift/Sources/GildedRose/CustomisedItemFactory.swift index 8e16ec76..964011ec 100644 --- a/swift/Sources/GildedRose/CustomisedItemFactory.swift +++ b/swift/Sources/GildedRose/CustomisedItemFactory.swift @@ -8,10 +8,12 @@ import Foundation protocol CustomisedItem { + var item: Item {get set} func updateItemState() } class CustomisedItemFactory { + // Returns the Customised Item based on the Item name func getCustomisedItem(item: Item) -> CustomisedItem { switch item.name { case ItemNameConstants.kAgedBrieItem: diff --git a/swift/Sources/GildedRose/ItemQualityUpdater.swift b/swift/Sources/GildedRose/ItemQualityUpdater.swift index b4f74e8f..9f56b128 100644 --- a/swift/Sources/GildedRose/ItemQualityUpdater.swift +++ b/swift/Sources/GildedRose/ItemQualityUpdater.swift @@ -7,8 +7,7 @@ import Foundation -protocol ItemQualityUpdater { - var item: Item {get set} +protocol ItemQualityUpdater: CustomisedItem { } extension ItemQualityUpdater { @@ -32,3 +31,6 @@ extension ItemQualityUpdater { item.quality = value } } + +typealias ItemStateUpdater = ItemSellInUpdater & ItemQualityUpdater + diff --git a/swift/Sources/GildedRose/ItemSellInUpdater.swift b/swift/Sources/GildedRose/ItemSellInUpdater.swift index 7e4cfce0..2dafaff8 100644 --- a/swift/Sources/GildedRose/ItemSellInUpdater.swift +++ b/swift/Sources/GildedRose/ItemSellInUpdater.swift @@ -7,8 +7,7 @@ import Foundation -protocol ItemSellInUpdater { - var item: Item { get set } +protocol ItemSellInUpdater: CustomisedItem{ func reduceSellInDays(by days: Int) } diff --git a/swift/Sources/GildedRose/Items/AgedBrieItem.swift b/swift/Sources/GildedRose/Items/AgedBrieItem.swift index 5c1fbe91..512dbe79 100644 --- a/swift/Sources/GildedRose/Items/AgedBrieItem.swift +++ b/swift/Sources/GildedRose/Items/AgedBrieItem.swift @@ -7,15 +7,15 @@ import Foundation -struct AgedBrieItem: CustomisedItem, ItemSellInUpdater, ItemQualityUpdater { +struct AgedBrieItem: ItemStateUpdater{ var item: Item - + public init(item: Item) { self.item = item } func updateItemState() { - // update the sell in days + // update the sell in days. Reduce the Sell In days by 1 reduceSellInDays(by: 1) // Increment the Item quality by 1 if the quality is less than 50 diff --git a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift index b989e137..293df2f1 100644 --- a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift +++ b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift @@ -7,7 +7,7 @@ import Foundation -struct BackstagePassesItem: CustomisedItem, ItemQualityUpdater, ItemSellInUpdater { +struct BackstagePassesItem: ItemStateUpdater { var item: Item public init(item: Item) { @@ -15,23 +15,26 @@ struct BackstagePassesItem: CustomisedItem, ItemQualityUpdater, ItemSellInUpdate } func updateItemState() { + // Reduce the Sell in days by 1 reduceSellInDays(by: 1) + // If the sell in date id passed, sets the quality of item to 0 guard !isSellInDatePassed else { setItemQuality(to: ValueConstants.kLowestQualityValue) return } + // If the Quality of item is above 50, return guard isItemUnderHighestQuality else { setItemQuality(to: ValueConstants.kHightestQualityValue) return } switch item.sellIn { case 10...: - increaseItemQuality(by: 1) + increaseItemQuality(by: 1) // If the sell in days is more than 10, increase quality by 1 case 5..<10: - increaseItemQuality(by: 2) + increaseItemQuality(by: 2) // If sell in days is between 5 and 10, increase quality by 2 case 0..<5: - increaseItemQuality(by: 3) + increaseItemQuality(by: 3) // If sell in days is between 0 and 5, increase quality by 3 default: break } diff --git a/swift/Sources/GildedRose/Items/StandardItem.swift b/swift/Sources/GildedRose/Items/StandardItem.swift index e1dbd016..60e5b86d 100644 --- a/swift/Sources/GildedRose/Items/StandardItem.swift +++ b/swift/Sources/GildedRose/Items/StandardItem.swift @@ -7,7 +7,7 @@ import Foundation -struct StandardItem: CustomisedItem, ItemQualityUpdater, ItemSellInUpdater { +struct StandardItem: ItemStateUpdater { var item: Item public init(item: Item) { @@ -18,9 +18,11 @@ struct StandardItem: CustomisedItem, ItemQualityUpdater, ItemSellInUpdater { // Reduce the sellIn days for Item by 1 reduceSellInDays(by: 1) - // Reduce the item quality by 1 , if the sell in date is passed decrement by 2 + // Reduce the item quality by 1 , if the sell in date is passed decrement by double the value isSellInDatePassed ? reduceItemQuality(by: decreasingValueForZeroOrLessDaysToSell()) : reduceItemQuality(by: decreasingValueOverZeroDaysToSell()) + guard isItemMoreThanLowestQuality else { + // Sets the quality to zero if the quality is negative setItemQuality(to: ValueConstants.kLowestQualityValue) return } diff --git a/swift/Sources/GildedRose/Items/SulfurasItem.swift b/swift/Sources/GildedRose/Items/SulfurasItem.swift index 064de3dc..3c7a4a99 100644 --- a/swift/Sources/GildedRose/Items/SulfurasItem.swift +++ b/swift/Sources/GildedRose/Items/SulfurasItem.swift @@ -15,5 +15,6 @@ struct SulfurasItem: CustomisedItem { } func updateItemState() { // No code as there is no change in quality or sell in days of sulfuras item + return } } From bf62bf2ceb8f349f18501e89a954678593cae5a7 Mon Sep 17 00:00:00 2001 From: Manali Mogre Date: Mon, 17 Aug 2020 20:49:39 +0200 Subject: [PATCH 09/17] - Creating a Protocol for Factory Method --- .../Sources/GildedRose/CustomisedItemFactory.swift | 13 +++++++------ swift/Sources/GildedRose/GildedRose.swift | 3 +-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/swift/Sources/GildedRose/CustomisedItemFactory.swift b/swift/Sources/GildedRose/CustomisedItemFactory.swift index 964011ec..9605fee1 100644 --- a/swift/Sources/GildedRose/CustomisedItemFactory.swift +++ b/swift/Sources/GildedRose/CustomisedItemFactory.swift @@ -12,9 +12,14 @@ protocol CustomisedItem { func updateItemState() } -class CustomisedItemFactory { +protocol CustomisedItemFactoryCreator { + static func getCustomisedItem(item: Item) -> CustomisedItem +} + + +struct CustomisedItemFactory: CustomisedItemFactoryCreator { // Returns the Customised Item based on the Item name - func getCustomisedItem(item: Item) -> CustomisedItem { + static func getCustomisedItem(item: Item) -> CustomisedItem { switch item.name { case ItemNameConstants.kAgedBrieItem: return AgedBrieItem(item: item) @@ -27,7 +32,3 @@ class CustomisedItemFactory { } } } - - - - diff --git a/swift/Sources/GildedRose/GildedRose.swift b/swift/Sources/GildedRose/GildedRose.swift index 3b4746b5..8329da80 100644 --- a/swift/Sources/GildedRose/GildedRose.swift +++ b/swift/Sources/GildedRose/GildedRose.swift @@ -6,7 +6,6 @@ public class GildedRose { } public func updateQuality() { - let customFactoryObj = CustomisedItemFactory() - _ = items.map({customFactoryObj.getCustomisedItem(item: $0).updateItemState()}) + _ = items.map({CustomisedItemFactory.getCustomisedItem(item: $0).updateItemState()}) } } From 08c1415e17a9ca38b7c3b494cb59b654e6142abb Mon Sep 17 00:00:00 2001 From: Manali Mogre Date: Mon, 17 Aug 2020 22:38:57 +0200 Subject: [PATCH 10/17] - Code refactoring as per SOLID Principles --- .../GildedRose/CustomisedItemFactory.swift | 6 ----- .../GildedRose/GlidedRoseConstants.swift | 1 - .../GildedRose/ItemQualityUpdater.swift | 10 +------ .../GildedRose/ItemSellInUpdater.swift | 3 --- .../GildedRose/Items/AgedBrieItem.swift | 9 ++++--- .../Items/BackstagePassesItem.swift | 8 ++++++ .../GildedRose/Items/CustomisedItem.swift | 13 +++++++++ .../GildedRose/Items/StandardItem.swift | 27 ++++++++++++------- 8 files changed, 44 insertions(+), 33 deletions(-) create mode 100644 swift/Sources/GildedRose/Items/CustomisedItem.swift diff --git a/swift/Sources/GildedRose/CustomisedItemFactory.swift b/swift/Sources/GildedRose/CustomisedItemFactory.swift index 9605fee1..a2194f58 100644 --- a/swift/Sources/GildedRose/CustomisedItemFactory.swift +++ b/swift/Sources/GildedRose/CustomisedItemFactory.swift @@ -7,16 +7,10 @@ import Foundation -protocol CustomisedItem { - var item: Item {get set} - func updateItemState() -} - protocol CustomisedItemFactoryCreator { static func getCustomisedItem(item: Item) -> CustomisedItem } - struct CustomisedItemFactory: CustomisedItemFactoryCreator { // Returns the Customised Item based on the Item name static func getCustomisedItem(item: Item) -> CustomisedItem { diff --git a/swift/Sources/GildedRose/GlidedRoseConstants.swift b/swift/Sources/GildedRose/GlidedRoseConstants.swift index 4d4161e0..57dce581 100644 --- a/swift/Sources/GildedRose/GlidedRoseConstants.swift +++ b/swift/Sources/GildedRose/GlidedRoseConstants.swift @@ -17,4 +17,3 @@ struct ValueConstants { static let kHightestQualityValue = 50 static let kLowestQualityValue = 0 } - diff --git a/swift/Sources/GildedRose/ItemQualityUpdater.swift b/swift/Sources/GildedRose/ItemQualityUpdater.swift index 9f56b128..305ac4b6 100644 --- a/swift/Sources/GildedRose/ItemQualityUpdater.swift +++ b/swift/Sources/GildedRose/ItemQualityUpdater.swift @@ -11,14 +11,7 @@ protocol ItemQualityUpdater: CustomisedItem { } extension ItemQualityUpdater { - var isItemUnderHighestQuality: Bool { - return item.quality < ValueConstants.kHightestQualityValue - } - - var isItemMoreThanLowestQuality: Bool { - return item.quality > ValueConstants.kLowestQualityValue - } - + func reduceItemQuality(by value:Int) { item.quality -= value } @@ -33,4 +26,3 @@ extension ItemQualityUpdater { } typealias ItemStateUpdater = ItemSellInUpdater & ItemQualityUpdater - diff --git a/swift/Sources/GildedRose/ItemSellInUpdater.swift b/swift/Sources/GildedRose/ItemSellInUpdater.swift index 2dafaff8..d7332ecc 100644 --- a/swift/Sources/GildedRose/ItemSellInUpdater.swift +++ b/swift/Sources/GildedRose/ItemSellInUpdater.swift @@ -12,9 +12,6 @@ protocol ItemSellInUpdater: CustomisedItem{ } extension ItemSellInUpdater { - var isSellInDatePassed: Bool{ - return item.sellIn < 0 - } func reduceSellInDays(by days: Int) { item.sellIn -= days } diff --git a/swift/Sources/GildedRose/Items/AgedBrieItem.swift b/swift/Sources/GildedRose/Items/AgedBrieItem.swift index 512dbe79..d8a4ba8c 100644 --- a/swift/Sources/GildedRose/Items/AgedBrieItem.swift +++ b/swift/Sources/GildedRose/Items/AgedBrieItem.swift @@ -9,7 +9,11 @@ import Foundation struct AgedBrieItem: ItemStateUpdater{ var item: Item - + + private var isItemUnderHighestQuality: Bool { + return item.quality < ValueConstants.kHightestQualityValue + } + public init(item: Item) { self.item = item } @@ -22,7 +26,4 @@ struct AgedBrieItem: ItemStateUpdater{ guard isItemUnderHighestQuality else { return } increaseItemQuality(by: 1) } - - - } diff --git a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift index 293df2f1..c33ce1f0 100644 --- a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift +++ b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift @@ -10,6 +10,14 @@ import Foundation struct BackstagePassesItem: ItemStateUpdater { var item: Item + private var isItemUnderHighestQuality: Bool { + return item.quality < ValueConstants.kHightestQualityValue + } + + private var isSellInDatePassed: Bool{ + return item.sellIn < 0 + } + public init(item: Item) { self.item = item } diff --git a/swift/Sources/GildedRose/Items/CustomisedItem.swift b/swift/Sources/GildedRose/Items/CustomisedItem.swift new file mode 100644 index 00000000..aed08bb2 --- /dev/null +++ b/swift/Sources/GildedRose/Items/CustomisedItem.swift @@ -0,0 +1,13 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 17/08/2020. +// + +import Foundation + +protocol CustomisedItem { + var item: Item {get set} + func updateItemState() +} diff --git a/swift/Sources/GildedRose/Items/StandardItem.swift b/swift/Sources/GildedRose/Items/StandardItem.swift index 60e5b86d..dea032f7 100644 --- a/swift/Sources/GildedRose/Items/StandardItem.swift +++ b/swift/Sources/GildedRose/Items/StandardItem.swift @@ -10,6 +10,21 @@ import Foundation struct StandardItem: ItemStateUpdater { var item: Item + private var isSellInDatePassed: Bool{ + return item.sellIn < 0 + } + + var decreasingValueOverZeroDaysToSell: Int { + return 1 + } + private var decreasingValueForZeroOrLessDaysToSell: Int { + return 2 * decreasingValueOverZeroDaysToSell + } + + private var isItemMoreThanLowestQuality: Bool { + return item.quality > ValueConstants.kLowestQualityValue + } + public init(item: Item) { self.item = item } @@ -17,9 +32,9 @@ struct StandardItem: ItemStateUpdater { func updateItemState() { // Reduce the sellIn days for Item by 1 reduceSellInDays(by: 1) - + // Reduce the item quality by 1 , if the sell in date is passed decrement by double the value - isSellInDatePassed ? reduceItemQuality(by: decreasingValueForZeroOrLessDaysToSell()) : reduceItemQuality(by: decreasingValueOverZeroDaysToSell()) + isSellInDatePassed ? reduceItemQuality(by: decreasingValueForZeroOrLessDaysToSell) : reduceItemQuality(by: decreasingValueOverZeroDaysToSell) guard isItemMoreThanLowestQuality else { // Sets the quality to zero if the quality is negative @@ -27,12 +42,4 @@ struct StandardItem: ItemStateUpdater { return } } - - func decreasingValueOverZeroDaysToSell() -> Int { - return 1 - } - - private func decreasingValueForZeroOrLessDaysToSell() -> Int { - return 2 * decreasingValueOverZeroDaysToSell() - } } From 5e32a34b84d71a239b0156f6532d90242e62e0de Mon Sep 17 00:00:00 2001 From: Manali Mogre Date: Mon, 17 Aug 2020 22:49:38 +0200 Subject: [PATCH 11/17] - Code Refactoring : Adding function declarations in protocol --- swift/Sources/GildedRose/ItemQualityUpdater.swift | 3 +++ swift/Sources/GildedRose/Items/AgedBrieItem.swift | 4 ++-- swift/Sources/GildedRose/Items/BackstagePassesItem.swift | 2 +- swift/Sources/GildedRose/Items/StandardItem.swift | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/swift/Sources/GildedRose/ItemQualityUpdater.swift b/swift/Sources/GildedRose/ItemQualityUpdater.swift index 305ac4b6..3712e775 100644 --- a/swift/Sources/GildedRose/ItemQualityUpdater.swift +++ b/swift/Sources/GildedRose/ItemQualityUpdater.swift @@ -8,6 +8,9 @@ import Foundation protocol ItemQualityUpdater: CustomisedItem { + func reduceItemQuality(by value:Int) + func increaseItemQuality(by value:Int) + func setItemQuality(to value: Int) } extension ItemQualityUpdater { diff --git a/swift/Sources/GildedRose/Items/AgedBrieItem.swift b/swift/Sources/GildedRose/Items/AgedBrieItem.swift index d8a4ba8c..c7127bfd 100644 --- a/swift/Sources/GildedRose/Items/AgedBrieItem.swift +++ b/swift/Sources/GildedRose/Items/AgedBrieItem.swift @@ -7,7 +7,7 @@ import Foundation -struct AgedBrieItem: ItemStateUpdater{ +struct AgedBrieItem: CustomisedItem, ItemStateUpdater{ var item: Item private var isItemUnderHighestQuality: Bool { @@ -20,7 +20,7 @@ struct AgedBrieItem: ItemStateUpdater{ func updateItemState() { // update the sell in days. Reduce the Sell In days by 1 - reduceSellInDays(by: 1) + self.reduceSellInDays(by: 1) // Increment the Item quality by 1 if the quality is less than 50 guard isItemUnderHighestQuality else { return } diff --git a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift index c33ce1f0..2fd0b752 100644 --- a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift +++ b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift @@ -7,7 +7,7 @@ import Foundation -struct BackstagePassesItem: ItemStateUpdater { +struct BackstagePassesItem: CustomisedItem, ItemStateUpdater { var item: Item private var isItemUnderHighestQuality: Bool { diff --git a/swift/Sources/GildedRose/Items/StandardItem.swift b/swift/Sources/GildedRose/Items/StandardItem.swift index dea032f7..93ecb58f 100644 --- a/swift/Sources/GildedRose/Items/StandardItem.swift +++ b/swift/Sources/GildedRose/Items/StandardItem.swift @@ -7,7 +7,7 @@ import Foundation -struct StandardItem: ItemStateUpdater { +struct StandardItem: CustomisedItem, ItemStateUpdater { var item: Item private var isSellInDatePassed: Bool{ From 892c869e2f0f283efcfa44c529571558e281cc6c Mon Sep 17 00:00:00 2001 From: Manali Mogre Date: Mon, 17 Aug 2020 22:59:11 +0200 Subject: [PATCH 12/17] Adding Test cases for Conjured Items --- .../GildedRoseTests/GildedRoseTests.swift | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/swift/Tests/GildedRoseTests/GildedRoseTests.swift b/swift/Tests/GildedRoseTests/GildedRoseTests.swift index 22315f80..675b7b9f 100644 --- a/swift/Tests/GildedRoseTests/GildedRoseTests.swift +++ b/swift/Tests/GildedRoseTests/GildedRoseTests.swift @@ -182,6 +182,71 @@ class GildedRoseTests: XCTestCase { XCTAssertEqual(80, app.items[1].quality) } + // MARK: -Test Cases for Conjured Items + + func testConjuredItems() { + let items = [ + Item(name: "Conjured Mana Cake", sellIn: 3, quality: 50), + Item(name: "Conjured Mana Cake", sellIn: 2, quality: 3)] + + let app = GildedRose(items: items) + + let days = 2 + for _ in 0.. () throws -> Void)] { return [ ("testFoo", testFoo), From 576ecc136d2f20b9a21f320885b6250016dcb22f Mon Sep 17 00:00:00 2001 From: Manali Mogre Date: Mon, 17 Aug 2020 23:08:28 +0200 Subject: [PATCH 13/17] - Moving Test Cases for Items in separate files --- .../GildedRoseTests/AgedBrieItemTests.swift | 47 ++++ .../BackstagePassesItemTests.swift | 72 ++++++ .../GildedRoseTests/ConjuredItemTests.swift | 76 ++++++ .../GildedRoseTests/GildedRoseTests.swift | 235 +----------------- .../GildedRoseTests/StandardItemTests.swift | 65 +++++ .../GildedRoseTests/SulfurasItemTests.swift | 32 +++ 6 files changed, 293 insertions(+), 234 deletions(-) create mode 100644 swift/Tests/GildedRoseTests/AgedBrieItemTests.swift create mode 100644 swift/Tests/GildedRoseTests/BackstagePassesItemTests.swift create mode 100644 swift/Tests/GildedRoseTests/ConjuredItemTests.swift create mode 100644 swift/Tests/GildedRoseTests/StandardItemTests.swift create mode 100644 swift/Tests/GildedRoseTests/SulfurasItemTests.swift diff --git a/swift/Tests/GildedRoseTests/AgedBrieItemTests.swift b/swift/Tests/GildedRoseTests/AgedBrieItemTests.swift new file mode 100644 index 00000000..26d5fe26 --- /dev/null +++ b/swift/Tests/GildedRoseTests/AgedBrieItemTests.swift @@ -0,0 +1,47 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 17/08/2020. +// + +@testable import GildedRose +import XCTest + +class AgedBrieItemTests: XCTestCase { + // MARK: - Test Cases for Aged Brie Items + func testAgedBrieItems() { + let items = [ + Item(name: "Aged Brie", sellIn: 10, quality: 20), + Item(name: "Aged Brie", sellIn: 2, quality: 0)] + + let app = GildedRose(items: items) + + let days = 2 + for _ in 0.. () throws -> Void)] { return [ diff --git a/swift/Tests/GildedRoseTests/StandardItemTests.swift b/swift/Tests/GildedRoseTests/StandardItemTests.swift new file mode 100644 index 00000000..72c3bfe0 --- /dev/null +++ b/swift/Tests/GildedRoseTests/StandardItemTests.swift @@ -0,0 +1,65 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 17/08/2020. +// + +@testable import GildedRose +import XCTest + +class StandardItemTests: XCTestCase { + // MARK: - Test Cases for Standard Items + func testStandardItemsForSellInDatePassed() { + let items = [ Item(name: "Elixir of the Mongoose", sellIn: 1, quality: 7), + Item(name: "+5 Dexterity Vest", sellIn: 0, quality: 10) + ] + let app = GildedRose(items: items) + let days = 3 + for _ in 0.. Date: Tue, 18 Aug 2020 21:32:37 +0200 Subject: [PATCH 14/17] Changing static function to class function to allow method overriding --- swift/Sources/GildedRose/CustomisedItemFactory.swift | 5 +++-- swift/Sources/GildedRose/Items/StandardItem.swift | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/swift/Sources/GildedRose/CustomisedItemFactory.swift b/swift/Sources/GildedRose/CustomisedItemFactory.swift index a2194f58..539e0e15 100644 --- a/swift/Sources/GildedRose/CustomisedItemFactory.swift +++ b/swift/Sources/GildedRose/CustomisedItemFactory.swift @@ -11,9 +11,9 @@ protocol CustomisedItemFactoryCreator { static func getCustomisedItem(item: Item) -> CustomisedItem } -struct CustomisedItemFactory: CustomisedItemFactoryCreator { +class CustomisedItemFactory: CustomisedItemFactoryCreator { // Returns the Customised Item based on the Item name - static func getCustomisedItem(item: Item) -> CustomisedItem { + class func getCustomisedItem(item: Item) -> CustomisedItem { switch item.name { case ItemNameConstants.kAgedBrieItem: return AgedBrieItem(item: item) @@ -26,3 +26,4 @@ struct CustomisedItemFactory: CustomisedItemFactoryCreator { } } } + diff --git a/swift/Sources/GildedRose/Items/StandardItem.swift b/swift/Sources/GildedRose/Items/StandardItem.swift index 93ecb58f..f0b1e370 100644 --- a/swift/Sources/GildedRose/Items/StandardItem.swift +++ b/swift/Sources/GildedRose/Items/StandardItem.swift @@ -7,7 +7,7 @@ import Foundation -struct StandardItem: CustomisedItem, ItemStateUpdater { +class StandardItem: CustomisedItem, ItemStateUpdater { var item: Item private var isSellInDatePassed: Bool{ From 71459293bbdbef82032c558526630efad6e5574b Mon Sep 17 00:00:00 2001 From: Manali Mogre Date: Tue, 18 Aug 2020 21:34:07 +0200 Subject: [PATCH 15/17] Adding Conjured Item --- .../GildedRose/GlidedRoseConstants.swift | 1 + .../GildedRose/Items/ConjuredItem.swift | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 swift/Sources/GildedRose/Items/ConjuredItem.swift diff --git a/swift/Sources/GildedRose/GlidedRoseConstants.swift b/swift/Sources/GildedRose/GlidedRoseConstants.swift index 57dce581..369c96ed 100644 --- a/swift/Sources/GildedRose/GlidedRoseConstants.swift +++ b/swift/Sources/GildedRose/GlidedRoseConstants.swift @@ -11,6 +11,7 @@ struct ItemNameConstants { static let kAgedBrieItem: String = "Aged Brie" static let kBackstagePassesItem: String = "Backstage passes to a TAFKAL80ETC concert" static let kSulfurasItem: String = "Sulfuras, Hand of Ragnaros" + static let kConjuredItem: String = "Conjured Mana Cake" } struct ValueConstants { diff --git a/swift/Sources/GildedRose/Items/ConjuredItem.swift b/swift/Sources/GildedRose/Items/ConjuredItem.swift new file mode 100644 index 00000000..6cccc1b0 --- /dev/null +++ b/swift/Sources/GildedRose/Items/ConjuredItem.swift @@ -0,0 +1,18 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 18/08/2020. +// + +import Foundation + +class ConjuredItem: StandardItem{ + + override var decreasingValueOverZeroDaysToSell: Int { + return 2 + } + override init(item: Item) { + super.init(item: item) + } +} From 27387139298963ff22b6e0c739456c0496516989 Mon Sep 17 00:00:00 2001 From: Manali Mogre Date: Tue, 18 Aug 2020 22:49:14 +0200 Subject: [PATCH 16/17] - Updating Code Comments - Updating variable names --- .../GildedRose/CustomisedItemFactory.swift | 27 ++++++++++++++++--- swift/Sources/GildedRose/GildedRose.swift | 3 ++- .../GildedRose/ItemQualityUpdater.swift | 4 ++- .../GildedRose/ItemSellInUpdater.swift | 12 ++++++--- .../GildedRose/Items/AgedBrieItem.swift | 3 ++- .../Items/BackstagePassesItem.swift | 5 ++-- .../GildedRose/Items/ConjuredItem.swift | 4 +-- .../GildedRose/Items/StandardItem.swift | 14 +++++----- 8 files changed, 52 insertions(+), 20 deletions(-) diff --git a/swift/Sources/GildedRose/CustomisedItemFactory.swift b/swift/Sources/GildedRose/CustomisedItemFactory.swift index 539e0e15..5073c431 100644 --- a/swift/Sources/GildedRose/CustomisedItemFactory.swift +++ b/swift/Sources/GildedRose/CustomisedItemFactory.swift @@ -8,12 +8,12 @@ import Foundation protocol CustomisedItemFactoryCreator { - static func getCustomisedItem(item: Item) -> CustomisedItem + func getCustomisedItem(item: Item) -> CustomisedItem } class CustomisedItemFactory: CustomisedItemFactoryCreator { - // Returns the Customised Item based on the Item name - class func getCustomisedItem(item: Item) -> CustomisedItem { + // Returns the Created Customised Item based on the Item name + func getCustomisedItem(item: Item) -> CustomisedItem { switch item.name { case ItemNameConstants.kAgedBrieItem: return AgedBrieItem(item: item) @@ -27,3 +27,24 @@ class CustomisedItemFactory: CustomisedItemFactoryCreator { } } +class CustomisedItemFactoryWithNewItems: CustomisedItemFactory { + // Creates Conjured Item for newly added Conjured Item. For the old items calls the super class function + override func getCustomisedItem(item: Item) -> CustomisedItem { + switch item.name { + case ItemNameConstants.kConjuredItem: + return ConjuredItem(item: item) + default: + return super.getCustomisedItem(item: item) + } + } +} + +final class CustomItemFactoryManager { + var itemFactory: CustomisedItemFactoryCreator + public init(customItemFactory: CustomisedItemFactoryCreator) { + self.itemFactory = customItemFactory + } + func getCustomisedItem(item: Item) -> CustomisedItem { + return itemFactory.getCustomisedItem(item: item) + } +} diff --git a/swift/Sources/GildedRose/GildedRose.swift b/swift/Sources/GildedRose/GildedRose.swift index 8329da80..a07bfd64 100644 --- a/swift/Sources/GildedRose/GildedRose.swift +++ b/swift/Sources/GildedRose/GildedRose.swift @@ -6,6 +6,7 @@ public class GildedRose { } public func updateQuality() { - _ = items.map({CustomisedItemFactory.getCustomisedItem(item: $0).updateItemState()}) + let itemFactoryManager = CustomItemFactoryManager(customItemFactory: CustomisedItemFactoryWithNewItems()) + _ = items.map({ itemFactoryManager.getCustomisedItem(item: $0).updateItemState()}) } } diff --git a/swift/Sources/GildedRose/ItemQualityUpdater.swift b/swift/Sources/GildedRose/ItemQualityUpdater.swift index 3712e775..a4458e22 100644 --- a/swift/Sources/GildedRose/ItemQualityUpdater.swift +++ b/swift/Sources/GildedRose/ItemQualityUpdater.swift @@ -14,15 +14,17 @@ protocol ItemQualityUpdater: CustomisedItem { } extension ItemQualityUpdater { - + // Reduces the item Quality by the value passed as parameter func reduceItemQuality(by value:Int) { item.quality -= value } + // Increases Item Quality by the value passed as parameter func increaseItemQuality(by value:Int) { item.quality += value } + // Sets the Item Quality to the value passed as parameter func setItemQuality(to value: Int){ item.quality = value } diff --git a/swift/Sources/GildedRose/ItemSellInUpdater.swift b/swift/Sources/GildedRose/ItemSellInUpdater.swift index d7332ecc..e850808e 100644 --- a/swift/Sources/GildedRose/ItemSellInUpdater.swift +++ b/swift/Sources/GildedRose/ItemSellInUpdater.swift @@ -8,11 +8,17 @@ import Foundation protocol ItemSellInUpdater: CustomisedItem{ - func reduceSellInDays(by days: Int) + var decreasingNumberOfSellInDays: Int { get } + func updateSellInDays() } extension ItemSellInUpdater { - func reduceSellInDays(by days: Int) { - item.sellIn -= days + var decreasingNumberOfSellInDays: Int { + return 1 + } + + // Reduces the sell in days by 1 + func updateSellInDays() { + item.sellIn -= decreasingNumberOfSellInDays } } diff --git a/swift/Sources/GildedRose/Items/AgedBrieItem.swift b/swift/Sources/GildedRose/Items/AgedBrieItem.swift index c7127bfd..2528b9eb 100644 --- a/swift/Sources/GildedRose/Items/AgedBrieItem.swift +++ b/swift/Sources/GildedRose/Items/AgedBrieItem.swift @@ -8,6 +8,7 @@ import Foundation struct AgedBrieItem: CustomisedItem, ItemStateUpdater{ + var item: Item private var isItemUnderHighestQuality: Bool { @@ -20,7 +21,7 @@ struct AgedBrieItem: CustomisedItem, ItemStateUpdater{ func updateItemState() { // update the sell in days. Reduce the Sell In days by 1 - self.reduceSellInDays(by: 1) + self.updateSellInDays() // Increment the Item quality by 1 if the quality is less than 50 guard isItemUnderHighestQuality else { return } diff --git a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift index 2fd0b752..ec29068b 100644 --- a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift +++ b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift @@ -8,6 +8,7 @@ import Foundation struct BackstagePassesItem: CustomisedItem, ItemStateUpdater { + var item: Item private var isItemUnderHighestQuality: Bool { @@ -24,9 +25,9 @@ struct BackstagePassesItem: CustomisedItem, ItemStateUpdater { func updateItemState() { // Reduce the Sell in days by 1 - reduceSellInDays(by: 1) + updateSellInDays() - // If the sell in date id passed, sets the quality of item to 0 + // If the sell in date is passed, sets the quality of item to 0 guard !isSellInDatePassed else { setItemQuality(to: ValueConstants.kLowestQualityValue) return diff --git a/swift/Sources/GildedRose/Items/ConjuredItem.swift b/swift/Sources/GildedRose/Items/ConjuredItem.swift index 6cccc1b0..c40c390a 100644 --- a/swift/Sources/GildedRose/Items/ConjuredItem.swift +++ b/swift/Sources/GildedRose/Items/ConjuredItem.swift @@ -8,8 +8,8 @@ import Foundation class ConjuredItem: StandardItem{ - - override var decreasingValueOverZeroDaysToSell: Int { + // Overrides decreasingQualityValueBeforeSellInDate property of Standard item as the Conjured Item degrades twice fast than Standard Iten + override var decreasingQualityValueBeforeSellInDate: Int { return 2 } override init(item: Item) { diff --git a/swift/Sources/GildedRose/Items/StandardItem.swift b/swift/Sources/GildedRose/Items/StandardItem.swift index f0b1e370..2bead369 100644 --- a/swift/Sources/GildedRose/Items/StandardItem.swift +++ b/swift/Sources/GildedRose/Items/StandardItem.swift @@ -11,14 +11,14 @@ class StandardItem: CustomisedItem, ItemStateUpdater { var item: Item private var isSellInDatePassed: Bool{ - return item.sellIn < 0 + return item.sellIn < 0 } - var decreasingValueOverZeroDaysToSell: Int { - return 1 + var decreasingQualityValueBeforeSellInDate: Int { + return 1 // Quality degrades by 1 if the sellIn date is not passed } - private var decreasingValueForZeroOrLessDaysToSell: Int { - return 2 * decreasingValueOverZeroDaysToSell + private var decreasingQualityValueAfterSellInDate: Int { + return 2 * decreasingQualityValueBeforeSellInDate // Quality degrades twice after SellIn date is passed } private var isItemMoreThanLowestQuality: Bool { @@ -31,10 +31,10 @@ class StandardItem: CustomisedItem, ItemStateUpdater { func updateItemState() { // Reduce the sellIn days for Item by 1 - reduceSellInDays(by: 1) + updateSellInDays() // Reduce the item quality by 1 , if the sell in date is passed decrement by double the value - isSellInDatePassed ? reduceItemQuality(by: decreasingValueForZeroOrLessDaysToSell) : reduceItemQuality(by: decreasingValueOverZeroDaysToSell) + isSellInDatePassed ? reduceItemQuality(by: decreasingQualityValueAfterSellInDate) : reduceItemQuality(by: decreasingQualityValueBeforeSellInDate) guard isItemMoreThanLowestQuality else { // Sets the quality to zero if the quality is negative From 9a4efc2c9819961ab56d8b7cb0633cd728c9f466 Mon Sep 17 00:00:00 2001 From: Manali Mogre Date: Wed, 19 Aug 2020 13:44:54 +0200 Subject: [PATCH 17/17] - Grouping the files --- swift/Sources/GildedRose/{ => Items}/Item.swift | 0 swift/Sources/GildedRose/{ => Protocols}/ItemQualityUpdater.swift | 0 swift/Sources/GildedRose/{ => Protocols}/ItemSellInUpdater.swift | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename swift/Sources/GildedRose/{ => Items}/Item.swift (100%) rename swift/Sources/GildedRose/{ => Protocols}/ItemQualityUpdater.swift (100%) rename swift/Sources/GildedRose/{ => Protocols}/ItemSellInUpdater.swift (100%) diff --git a/swift/Sources/GildedRose/Item.swift b/swift/Sources/GildedRose/Items/Item.swift similarity index 100% rename from swift/Sources/GildedRose/Item.swift rename to swift/Sources/GildedRose/Items/Item.swift diff --git a/swift/Sources/GildedRose/ItemQualityUpdater.swift b/swift/Sources/GildedRose/Protocols/ItemQualityUpdater.swift similarity index 100% rename from swift/Sources/GildedRose/ItemQualityUpdater.swift rename to swift/Sources/GildedRose/Protocols/ItemQualityUpdater.swift diff --git a/swift/Sources/GildedRose/ItemSellInUpdater.swift b/swift/Sources/GildedRose/Protocols/ItemSellInUpdater.swift similarity index 100% rename from swift/Sources/GildedRose/ItemSellInUpdater.swift rename to swift/Sources/GildedRose/Protocols/ItemSellInUpdater.swift