mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
- Code Refactoring
This commit is contained in:
parent
d363612674
commit
db0db19d5d
@ -8,10 +8,12 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
protocol CustomisedItem {
|
protocol CustomisedItem {
|
||||||
|
var item: Item {get set}
|
||||||
func updateItemState()
|
func updateItemState()
|
||||||
}
|
}
|
||||||
|
|
||||||
class CustomisedItemFactory {
|
class CustomisedItemFactory {
|
||||||
|
// Returns the Customised Item based on the Item name
|
||||||
func getCustomisedItem(item: Item) -> CustomisedItem {
|
func getCustomisedItem(item: Item) -> CustomisedItem {
|
||||||
switch item.name {
|
switch item.name {
|
||||||
case ItemNameConstants.kAgedBrieItem:
|
case ItemNameConstants.kAgedBrieItem:
|
||||||
|
|||||||
@ -7,8 +7,7 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
protocol ItemQualityUpdater {
|
protocol ItemQualityUpdater: CustomisedItem {
|
||||||
var item: Item {get set}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension ItemQualityUpdater {
|
extension ItemQualityUpdater {
|
||||||
@ -32,3 +31,6 @@ extension ItemQualityUpdater {
|
|||||||
item.quality = value
|
item.quality = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typealias ItemStateUpdater = ItemSellInUpdater & ItemQualityUpdater
|
||||||
|
|
||||||
|
|||||||
@ -7,8 +7,7 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
protocol ItemSellInUpdater {
|
protocol ItemSellInUpdater: CustomisedItem{
|
||||||
var item: Item { get set }
|
|
||||||
func reduceSellInDays(by days: Int)
|
func reduceSellInDays(by days: Int)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
struct AgedBrieItem: CustomisedItem, ItemSellInUpdater, ItemQualityUpdater {
|
struct AgedBrieItem: ItemStateUpdater{
|
||||||
var item: Item
|
var item: Item
|
||||||
|
|
||||||
public init(item: Item) {
|
public init(item: Item) {
|
||||||
@ -15,7 +15,7 @@ struct AgedBrieItem: CustomisedItem, ItemSellInUpdater, ItemQualityUpdater {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updateItemState() {
|
func updateItemState() {
|
||||||
// update the sell in days
|
// update the sell in days. Reduce the Sell In days by 1
|
||||||
reduceSellInDays(by: 1)
|
reduceSellInDays(by: 1)
|
||||||
|
|
||||||
// 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
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
struct BackstagePassesItem: CustomisedItem, ItemQualityUpdater, ItemSellInUpdater {
|
struct BackstagePassesItem: ItemStateUpdater {
|
||||||
var item: Item
|
var item: Item
|
||||||
|
|
||||||
public init(item: Item) {
|
public init(item: Item) {
|
||||||
@ -15,23 +15,26 @@ struct BackstagePassesItem: CustomisedItem, ItemQualityUpdater, ItemSellInUpdate
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updateItemState() {
|
func updateItemState() {
|
||||||
|
// Reduce the Sell in days by 1
|
||||||
reduceSellInDays(by: 1)
|
reduceSellInDays(by: 1)
|
||||||
|
|
||||||
|
// If the sell in date id passed, sets the quality of item to 0
|
||||||
guard !isSellInDatePassed else {
|
guard !isSellInDatePassed else {
|
||||||
setItemQuality(to: ValueConstants.kLowestQualityValue)
|
setItemQuality(to: ValueConstants.kLowestQualityValue)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// If the Quality of item is above 50, return
|
||||||
guard isItemUnderHighestQuality else {
|
guard isItemUnderHighestQuality else {
|
||||||
setItemQuality(to: ValueConstants.kHightestQualityValue)
|
setItemQuality(to: ValueConstants.kHightestQualityValue)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch item.sellIn {
|
switch item.sellIn {
|
||||||
case 10...:
|
case 10...:
|
||||||
increaseItemQuality(by: 1)
|
increaseItemQuality(by: 1) // If the sell in days is more than 10, increase quality by 1
|
||||||
case 5..<10:
|
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:
|
case 0..<5:
|
||||||
increaseItemQuality(by: 3)
|
increaseItemQuality(by: 3) // If sell in days is between 0 and 5, increase quality by 3
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
struct StandardItem: CustomisedItem, ItemQualityUpdater, ItemSellInUpdater {
|
struct StandardItem: ItemStateUpdater {
|
||||||
var item: Item
|
var item: Item
|
||||||
|
|
||||||
public init(item: Item) {
|
public init(item: Item) {
|
||||||
@ -18,9 +18,11 @@ struct StandardItem: CustomisedItem, ItemQualityUpdater, ItemSellInUpdater {
|
|||||||
// Reduce the sellIn days for Item by 1
|
// Reduce the sellIn days for Item by 1
|
||||||
reduceSellInDays(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())
|
isSellInDatePassed ? reduceItemQuality(by: decreasingValueForZeroOrLessDaysToSell()) : reduceItemQuality(by: decreasingValueOverZeroDaysToSell())
|
||||||
|
|
||||||
guard isItemMoreThanLowestQuality else {
|
guard isItemMoreThanLowestQuality else {
|
||||||
|
// Sets the quality to zero if the quality is negative
|
||||||
setItemQuality(to: ValueConstants.kLowestQualityValue)
|
setItemQuality(to: ValueConstants.kLowestQualityValue)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,5 +15,6 @@ struct SulfurasItem: CustomisedItem {
|
|||||||
}
|
}
|
||||||
func updateItemState() {
|
func updateItemState() {
|
||||||
// No code as there is no change in quality or sell in days of sulfuras item
|
// No code as there is no change in quality or sell in days of sulfuras item
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user