- Code Refactoring

This commit is contained in:
Manali Mogre 2020-08-17 20:14:47 +02:00
parent d363612674
commit db0db19d5d
7 changed files with 22 additions and 13 deletions

View File

@ -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:

View File

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

View File

@ -7,8 +7,7 @@
import Foundation
protocol ItemSellInUpdater {
var item: Item { get set }
protocol ItemSellInUpdater: CustomisedItem{
func reduceSellInDays(by days: Int)
}

View File

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

View File

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

View File

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

View File

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