diff --git a/swift/Sources/GildedRose/CustomisedItemFactory.swift b/swift/Sources/GildedRose/CustomisedItemFactory.swift new file mode 100644 index 00000000..5073c431 --- /dev/null +++ b/swift/Sources/GildedRose/CustomisedItemFactory.swift @@ -0,0 +1,50 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 16/08/2020. +// + +import Foundation + +protocol CustomisedItemFactoryCreator { + func getCustomisedItem(item: Item) -> CustomisedItem +} + +class CustomisedItemFactory: CustomisedItemFactoryCreator { + // 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) + case ItemNameConstants.kBackstagePassesItem: + return BackstagePassesItem(item: item) + case ItemNameConstants.kSulfurasItem: + return SulfurasItem(item: item) + default: + return StandardItem(item: item) + } + } +} + +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 9b975467..a07bfd64 100644 --- a/swift/Sources/GildedRose/GildedRose.swift +++ b/swift/Sources/GildedRose/GildedRose.swift @@ -6,54 +6,7 @@ public class GildedRose { } 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 - } - } - } - } + let itemFactoryManager = CustomItemFactoryManager(customItemFactory: CustomisedItemFactoryWithNewItems()) + _ = items.map({ itemFactoryManager.getCustomisedItem(item: $0).updateItemState()}) } } diff --git a/swift/Sources/GildedRose/GlidedRoseConstants.swift b/swift/Sources/GildedRose/GlidedRoseConstants.swift new file mode 100644 index 00000000..369c96ed --- /dev/null +++ b/swift/Sources/GildedRose/GlidedRoseConstants.swift @@ -0,0 +1,20 @@ +// +// 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" + static let kConjuredItem: String = "Conjured Mana Cake" +} + +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 new file mode 100644 index 00000000..2528b9eb --- /dev/null +++ b/swift/Sources/GildedRose/Items/AgedBrieItem.swift @@ -0,0 +1,30 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 16/08/2020. +// + +import Foundation + +struct AgedBrieItem: CustomisedItem, ItemStateUpdater{ + + var item: Item + + private var isItemUnderHighestQuality: Bool { + return item.quality < ValueConstants.kHightestQualityValue + } + + public init(item: Item) { + self.item = item + } + + func updateItemState() { + // update the sell in days. Reduce the Sell In days by 1 + self.updateSellInDays() + + // Increment the Item quality by 1 if the quality is less than 50 + guard isItemUnderHighestQuality else { return } + increaseItemQuality(by: 1) + } +} diff --git a/swift/Sources/GildedRose/Items/BackstagePassesItem.swift b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift new file mode 100644 index 00000000..ec29068b --- /dev/null +++ b/swift/Sources/GildedRose/Items/BackstagePassesItem.swift @@ -0,0 +1,51 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 16/08/2020. +// + +import Foundation + +struct BackstagePassesItem: CustomisedItem, 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 + } + + func updateItemState() { + // Reduce the Sell in days by 1 + updateSellInDays() + + // If the sell in date is 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) // If the sell in days is more than 10, increase quality by 1 + case 5..<10: + increaseItemQuality(by: 2) // If sell in days is between 5 and 10, increase quality by 2 + case 0..<5: + 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/ConjuredItem.swift b/swift/Sources/GildedRose/Items/ConjuredItem.swift new file mode 100644 index 00000000..c40c390a --- /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{ + // 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) { + super.init(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/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/Items/StandardItem.swift b/swift/Sources/GildedRose/Items/StandardItem.swift new file mode 100644 index 00000000..2bead369 --- /dev/null +++ b/swift/Sources/GildedRose/Items/StandardItem.swift @@ -0,0 +1,45 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 16/08/2020. +// + +import Foundation + +class StandardItem: CustomisedItem, ItemStateUpdater { + var item: Item + + private var isSellInDatePassed: Bool{ + return item.sellIn < 0 + } + + var decreasingQualityValueBeforeSellInDate: Int { + return 1 // Quality degrades by 1 if the sellIn date is not passed + } + private var decreasingQualityValueAfterSellInDate: Int { + return 2 * decreasingQualityValueBeforeSellInDate // Quality degrades twice after SellIn date is passed + } + + private var isItemMoreThanLowestQuality: Bool { + return item.quality > ValueConstants.kLowestQualityValue + } + + public init(item: Item) { + self.item = item + } + + func updateItemState() { + // Reduce the sellIn days for Item by 1 + updateSellInDays() + + // Reduce the item quality by 1 , if the sell in date is passed decrement by double the value + isSellInDatePassed ? reduceItemQuality(by: decreasingQualityValueAfterSellInDate) : reduceItemQuality(by: decreasingQualityValueBeforeSellInDate) + + 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 new file mode 100644 index 00000000..3c7a4a99 --- /dev/null +++ b/swift/Sources/GildedRose/Items/SulfurasItem.swift @@ -0,0 +1,20 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 16/08/2020. +// + +import Foundation + +struct SulfurasItem: CustomisedItem { + var item: Item + + public init(item: Item) { + self.item = item + } + func updateItemState() { + // No code as there is no change in quality or sell in days of sulfuras item + return + } +} diff --git a/swift/Sources/GildedRose/Protocols/ItemQualityUpdater.swift b/swift/Sources/GildedRose/Protocols/ItemQualityUpdater.swift new file mode 100644 index 00000000..a4458e22 --- /dev/null +++ b/swift/Sources/GildedRose/Protocols/ItemQualityUpdater.swift @@ -0,0 +1,33 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 17/08/2020. +// + +import Foundation + +protocol ItemQualityUpdater: CustomisedItem { + func reduceItemQuality(by value:Int) + func increaseItemQuality(by value:Int) + func setItemQuality(to value: Int) +} + +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 + } +} + +typealias ItemStateUpdater = ItemSellInUpdater & ItemQualityUpdater diff --git a/swift/Sources/GildedRose/Protocols/ItemSellInUpdater.swift b/swift/Sources/GildedRose/Protocols/ItemSellInUpdater.swift new file mode 100644 index 00000000..e850808e --- /dev/null +++ b/swift/Sources/GildedRose/Protocols/ItemSellInUpdater.swift @@ -0,0 +1,24 @@ +// +// File.swift +// +// +// Created by Manali Mogre on 17/08/2020. +// + +import Foundation + +protocol ItemSellInUpdater: CustomisedItem{ + var decreasingNumberOfSellInDays: Int { get } + func updateSellInDays() +} + +extension ItemSellInUpdater { + var decreasingNumberOfSellInDays: Int { + return 1 + } + + // Reduces the sell in days by 1 + func updateSellInDays() { + item.sellIn -= decreasingNumberOfSellInDays + } +} 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 [ ("testFoo", testFoo), 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..