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 } }