- Updating Code Comments

- Updating variable names
This commit is contained in:
Manali Mogre 2020-08-18 22:49:14 +02:00
parent 71459293bb
commit 2738713929
8 changed files with 52 additions and 20 deletions

View File

@ -8,12 +8,12 @@
import Foundation import Foundation
protocol CustomisedItemFactoryCreator { protocol CustomisedItemFactoryCreator {
static func getCustomisedItem(item: Item) -> CustomisedItem func getCustomisedItem(item: Item) -> CustomisedItem
} }
class CustomisedItemFactory: CustomisedItemFactoryCreator { class CustomisedItemFactory: CustomisedItemFactoryCreator {
// Returns the Customised Item based on the Item name // Returns the Created Customised Item based on the Item name
class func getCustomisedItem(item: Item) -> CustomisedItem { func getCustomisedItem(item: Item) -> CustomisedItem {
switch item.name { switch item.name {
case ItemNameConstants.kAgedBrieItem: case ItemNameConstants.kAgedBrieItem:
return AgedBrieItem(item: item) 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)
}
}

View File

@ -6,6 +6,7 @@ public class GildedRose {
} }
public func updateQuality() { public func updateQuality() {
_ = items.map({CustomisedItemFactory.getCustomisedItem(item: $0).updateItemState()}) let itemFactoryManager = CustomItemFactoryManager(customItemFactory: CustomisedItemFactoryWithNewItems())
_ = items.map({ itemFactoryManager.getCustomisedItem(item: $0).updateItemState()})
} }
} }

View File

@ -14,15 +14,17 @@ protocol ItemQualityUpdater: CustomisedItem {
} }
extension ItemQualityUpdater { extension ItemQualityUpdater {
// Reduces the item Quality by the value passed as parameter
func reduceItemQuality(by value:Int) { func reduceItemQuality(by value:Int) {
item.quality -= value item.quality -= value
} }
// Increases Item Quality by the value passed as parameter
func increaseItemQuality(by value:Int) { func increaseItemQuality(by value:Int) {
item.quality += value item.quality += value
} }
// Sets the Item Quality to the value passed as parameter
func setItemQuality(to value: Int){ func setItemQuality(to value: Int){
item.quality = value item.quality = value
} }

View File

@ -8,11 +8,17 @@
import Foundation import Foundation
protocol ItemSellInUpdater: CustomisedItem{ protocol ItemSellInUpdater: CustomisedItem{
func reduceSellInDays(by days: Int) var decreasingNumberOfSellInDays: Int { get }
func updateSellInDays()
} }
extension ItemSellInUpdater { extension ItemSellInUpdater {
func reduceSellInDays(by days: Int) { var decreasingNumberOfSellInDays: Int {
item.sellIn -= days return 1
}
// Reduces the sell in days by 1
func updateSellInDays() {
item.sellIn -= decreasingNumberOfSellInDays
} }
} }

View File

@ -8,6 +8,7 @@
import Foundation import Foundation
struct AgedBrieItem: CustomisedItem, ItemStateUpdater{ struct AgedBrieItem: CustomisedItem, ItemStateUpdater{
var item: Item var item: Item
private var isItemUnderHighestQuality: Bool { private var isItemUnderHighestQuality: Bool {
@ -20,7 +21,7 @@ struct AgedBrieItem: CustomisedItem, ItemStateUpdater{
func updateItemState() { func updateItemState() {
// update the sell in days. Reduce the Sell In days by 1 // 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 // Increment the Item quality by 1 if the quality is less than 50
guard isItemUnderHighestQuality else { return } guard isItemUnderHighestQuality else { return }

View File

@ -8,6 +8,7 @@
import Foundation import Foundation
struct BackstagePassesItem: CustomisedItem, ItemStateUpdater { struct BackstagePassesItem: CustomisedItem, ItemStateUpdater {
var item: Item var item: Item
private var isItemUnderHighestQuality: Bool { private var isItemUnderHighestQuality: Bool {
@ -24,9 +25,9 @@ struct BackstagePassesItem: CustomisedItem, ItemStateUpdater {
func updateItemState() { func updateItemState() {
// Reduce the Sell in days by 1 // 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 { guard !isSellInDatePassed else {
setItemQuality(to: ValueConstants.kLowestQualityValue) setItemQuality(to: ValueConstants.kLowestQualityValue)
return return

View File

@ -8,8 +8,8 @@
import Foundation import Foundation
class ConjuredItem: StandardItem{ class ConjuredItem: StandardItem{
// Overrides decreasingQualityValueBeforeSellInDate property of Standard item as the Conjured Item degrades twice fast than Standard Iten
override var decreasingValueOverZeroDaysToSell: Int { override var decreasingQualityValueBeforeSellInDate: Int {
return 2 return 2
} }
override init(item: Item) { override init(item: Item) {

View File

@ -11,14 +11,14 @@ class StandardItem: CustomisedItem, ItemStateUpdater {
var item: Item var item: Item
private var isSellInDatePassed: Bool{ private var isSellInDatePassed: Bool{
return item.sellIn < 0 return item.sellIn < 0
} }
var decreasingValueOverZeroDaysToSell: Int { var decreasingQualityValueBeforeSellInDate: Int {
return 1 return 1 // Quality degrades by 1 if the sellIn date is not passed
} }
private var decreasingValueForZeroOrLessDaysToSell: Int { private var decreasingQualityValueAfterSellInDate: Int {
return 2 * decreasingValueOverZeroDaysToSell return 2 * decreasingQualityValueBeforeSellInDate // Quality degrades twice after SellIn date is passed
} }
private var isItemMoreThanLowestQuality: Bool { private var isItemMoreThanLowestQuality: Bool {
@ -31,10 +31,10 @@ class StandardItem: CustomisedItem, ItemStateUpdater {
func updateItemState() { func updateItemState() {
// Reduce the sellIn days for Item by 1 // 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 // 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 { guard isItemMoreThanLowestQuality else {
// Sets the quality to zero if the quality is negative // Sets the quality to zero if the quality is negative