mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
- Updating Code Comments
- Updating variable names
This commit is contained in:
parent
71459293bb
commit
2738713929
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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()})
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 }
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user