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