mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
- Code Refactoring
This commit is contained in:
parent
b2b61a1f57
commit
d363612674
@ -7,8 +7,12 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol CustomisedItem {
|
||||
func updateItemState()
|
||||
}
|
||||
|
||||
class CustomisedItemFactory {
|
||||
func getCustomisedItem(item: Item) -> CustomisedItemProtocol {
|
||||
func getCustomisedItem(item: Item) -> CustomisedItem {
|
||||
switch item.name {
|
||||
case ItemNameConstants.kAgedBrieItem:
|
||||
return AgedBrieItem(item: item)
|
||||
|
||||
@ -8,6 +8,5 @@ public class GildedRose {
|
||||
public func updateQuality() {
|
||||
let customFactoryObj = CustomisedItemFactory()
|
||||
_ = items.map({customFactoryObj.getCustomisedItem(item: $0).updateItemState()})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
34
swift/Sources/GildedRose/ItemQualityUpdater.swift
Normal file
34
swift/Sources/GildedRose/ItemQualityUpdater.swift
Normal file
@ -0,0 +1,34 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Manali Mogre on 17/08/2020.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol ItemQualityUpdater {
|
||||
var item: Item {get set}
|
||||
}
|
||||
|
||||
extension ItemQualityUpdater {
|
||||
var isItemUnderHighestQuality: Bool {
|
||||
return item.quality < ValueConstants.kHightestQualityValue
|
||||
}
|
||||
|
||||
var isItemMoreThanLowestQuality: Bool {
|
||||
return item.quality > ValueConstants.kLowestQualityValue
|
||||
}
|
||||
|
||||
func reduceItemQuality(by value:Int) {
|
||||
item.quality -= value
|
||||
}
|
||||
|
||||
func increaseItemQuality(by value:Int) {
|
||||
item.quality += value
|
||||
}
|
||||
|
||||
func setItemQuality(to value: Int){
|
||||
item.quality = value
|
||||
}
|
||||
}
|
||||
22
swift/Sources/GildedRose/ItemSellInUpdater.swift
Normal file
22
swift/Sources/GildedRose/ItemSellInUpdater.swift
Normal file
@ -0,0 +1,22 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Manali Mogre on 17/08/2020.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol ItemSellInUpdater {
|
||||
var item: Item { get set }
|
||||
func reduceSellInDays(by days: Int)
|
||||
}
|
||||
|
||||
extension ItemSellInUpdater {
|
||||
var isSellInDatePassed: Bool{
|
||||
return item.sellIn < 0
|
||||
}
|
||||
func reduceSellInDays(by days: Int) {
|
||||
item.sellIn -= days
|
||||
}
|
||||
}
|
||||
@ -7,20 +7,22 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
struct AgedBrieItem: CustomisedItemProtocol, ItemSellInUpdater, ItemQualityUpdater {
|
||||
struct AgedBrieItem: CustomisedItem, ItemSellInUpdater, ItemQualityUpdater {
|
||||
var item: Item
|
||||
|
||||
|
||||
public init(item: Item) {
|
||||
self.item = item
|
||||
}
|
||||
|
||||
func updateItemState() {
|
||||
// update the sell in days
|
||||
reduceSellInDays(for: item, by: 1)
|
||||
reduceSellInDays(by: 1)
|
||||
|
||||
// Increment the Item quality by 1 if the quality is less than 50
|
||||
guard isItemUnderHighestQuality(item: item) else { return }
|
||||
increaseQuality(for: item, by: 1)
|
||||
guard isItemUnderHighestQuality else { return }
|
||||
increaseItemQuality(by: 1)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
struct BackstagePassesItem: CustomisedItemProtocol, ItemQualityUpdater, ItemSellInUpdater {
|
||||
struct BackstagePassesItem: CustomisedItem, ItemQualityUpdater, ItemSellInUpdater {
|
||||
var item: Item
|
||||
|
||||
public init(item: Item) {
|
||||
@ -15,26 +15,25 @@ struct BackstagePassesItem: CustomisedItemProtocol, ItemQualityUpdater, ItemSell
|
||||
}
|
||||
|
||||
func updateItemState() {
|
||||
reduceSellInDays(for: item, by: 1)
|
||||
|
||||
guard !HasSellInDatePassed(item: item) else {
|
||||
setItemQualityToZero(item: item)
|
||||
reduceSellInDays(by: 1)
|
||||
|
||||
guard !isSellInDatePassed else {
|
||||
setItemQuality(to: ValueConstants.kLowestQualityValue)
|
||||
return
|
||||
}
|
||||
guard isItemUnderHighestQuality(item: item) else {
|
||||
setItemQualityToFifty(item: item)
|
||||
guard isItemUnderHighestQuality else {
|
||||
setItemQuality(to: ValueConstants.kHightestQualityValue)
|
||||
return
|
||||
}
|
||||
switch item.sellIn {
|
||||
case 10...:
|
||||
increaseQuality(for: item, by: 1)
|
||||
increaseItemQuality(by: 1)
|
||||
case 5..<10:
|
||||
increaseQuality(for: item, by: 2)
|
||||
increaseItemQuality(by: 2)
|
||||
case 0..<5:
|
||||
increaseQuality(for: item, by: 3)
|
||||
increaseItemQuality(by: 3)
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,56 +0,0 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Manali Mogre on 16/08/2020.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol CustomisedItemProtocol {
|
||||
func updateItemState()
|
||||
}
|
||||
|
||||
|
||||
protocol ItemSellInUpdater {
|
||||
func reduceSellInDays(for item: Item, by days: Int)
|
||||
}
|
||||
|
||||
extension ItemSellInUpdater {
|
||||
func reduceSellInDays(for item: Item, by days: Int) {
|
||||
item.sellIn -= days
|
||||
}
|
||||
|
||||
func HasSellInDatePassed(item: Item) -> Bool {
|
||||
return item.sellIn < 0
|
||||
}
|
||||
}
|
||||
|
||||
protocol ItemQualityUpdater {
|
||||
|
||||
}
|
||||
|
||||
extension ItemQualityUpdater {
|
||||
func isItemUnderHighestQuality(item: Item) -> Bool {
|
||||
return item.quality < ValueConstants.kHightestQualityValue
|
||||
}
|
||||
func reduceQuality(for item: Item, by value:Int) {
|
||||
item.quality -= value
|
||||
}
|
||||
|
||||
func increaseQuality(for item: Item, by value:Int) {
|
||||
item.quality += value
|
||||
}
|
||||
|
||||
func isItemOverLowestQuality(item: Item) -> Bool {
|
||||
return item.quality > ValueConstants.kLowestQualityValue
|
||||
}
|
||||
|
||||
func setItemQualityToZero(item: Item) {
|
||||
item.quality = ValueConstants.kLowestQualityValue
|
||||
}
|
||||
|
||||
func setItemQualityToFifty(item: Item) {
|
||||
item.quality = ValueConstants.kHightestQualityValue
|
||||
}
|
||||
}
|
||||
@ -7,20 +7,21 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
struct StandardItem: CustomisedItemProtocol, ItemQualityUpdater, ItemSellInUpdater {
|
||||
struct StandardItem: CustomisedItem, ItemQualityUpdater, ItemSellInUpdater {
|
||||
var item: Item
|
||||
|
||||
public init(item: Item) {
|
||||
self.item = item
|
||||
}
|
||||
|
||||
func updateItemState() {
|
||||
// Reduce the sellIn days for Item by 1
|
||||
reduceSellInDays(for: item, by: 1)
|
||||
reduceSellInDays(by: 1)
|
||||
|
||||
// Reduce the item quality by 1 , if the sell in date is passed decrement by 2
|
||||
HasSellInDatePassed(item: item) ? reduceQuality(for: item, by: decreasingValueForZeroOrLessDaysToSell()) : reduceQuality(for: item, by: decreasingValueOverZeroDaysToSell())
|
||||
guard isItemOverLowestQuality(item: item) else {
|
||||
item.quality = ValueConstants.kLowestQualityValue
|
||||
isSellInDatePassed ? reduceItemQuality(by: decreasingValueForZeroOrLessDaysToSell()) : reduceItemQuality(by: decreasingValueOverZeroDaysToSell())
|
||||
guard isItemMoreThanLowestQuality else {
|
||||
setItemQuality(to: ValueConstants.kLowestQualityValue)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
struct SulfurasItem: CustomisedItemProtocol {
|
||||
struct SulfurasItem: CustomisedItem {
|
||||
var item: Item
|
||||
|
||||
public init(item: Item) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user