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
b2b61a1f57
commit
d363612674
@ -7,8 +7,12 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
protocol CustomisedItem {
|
||||||
|
func updateItemState()
|
||||||
|
}
|
||||||
|
|
||||||
class CustomisedItemFactory {
|
class CustomisedItemFactory {
|
||||||
func getCustomisedItem(item: Item) -> CustomisedItemProtocol {
|
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)
|
||||||
|
|||||||
@ -8,6 +8,5 @@ public class GildedRose {
|
|||||||
public func updateQuality() {
|
public func updateQuality() {
|
||||||
let customFactoryObj = CustomisedItemFactory()
|
let customFactoryObj = CustomisedItemFactory()
|
||||||
_ = items.map({customFactoryObj.getCustomisedItem(item: $0).updateItemState()})
|
_ = 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
|
import Foundation
|
||||||
|
|
||||||
struct AgedBrieItem: CustomisedItemProtocol, ItemSellInUpdater, ItemQualityUpdater {
|
struct AgedBrieItem: CustomisedItem, ItemSellInUpdater, ItemQualityUpdater {
|
||||||
var item: Item
|
var item: Item
|
||||||
|
|
||||||
public init(item: Item) {
|
public init(item: Item) {
|
||||||
self.item = item
|
self.item = item
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateItemState() {
|
func updateItemState() {
|
||||||
// update the sell in days
|
// 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
|
// Increment the Item quality by 1 if the quality is less than 50
|
||||||
guard isItemUnderHighestQuality(item: item) else { return }
|
guard isItemUnderHighestQuality else { return }
|
||||||
increaseQuality(for: item, by: 1)
|
increaseItemQuality(by: 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
struct BackstagePassesItem: CustomisedItemProtocol, ItemQualityUpdater, ItemSellInUpdater {
|
struct BackstagePassesItem: CustomisedItem, ItemQualityUpdater, ItemSellInUpdater {
|
||||||
var item: Item
|
var item: Item
|
||||||
|
|
||||||
public init(item: Item) {
|
public init(item: Item) {
|
||||||
@ -15,26 +15,25 @@ struct BackstagePassesItem: CustomisedItemProtocol, ItemQualityUpdater, ItemSell
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updateItemState() {
|
func updateItemState() {
|
||||||
reduceSellInDays(for: item, by: 1)
|
reduceSellInDays(by: 1)
|
||||||
|
|
||||||
guard !HasSellInDatePassed(item: item) else {
|
guard !isSellInDatePassed else {
|
||||||
setItemQualityToZero(item: item)
|
setItemQuality(to: ValueConstants.kLowestQualityValue)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
guard isItemUnderHighestQuality(item: item) else {
|
guard isItemUnderHighestQuality else {
|
||||||
setItemQualityToFifty(item: item)
|
setItemQuality(to: ValueConstants.kHightestQualityValue)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch item.sellIn {
|
switch item.sellIn {
|
||||||
case 10...:
|
case 10...:
|
||||||
increaseQuality(for: item, by: 1)
|
increaseItemQuality(by: 1)
|
||||||
case 5..<10:
|
case 5..<10:
|
||||||
increaseQuality(for: item, by: 2)
|
increaseItemQuality(by: 2)
|
||||||
case 0..<5:
|
case 0..<5:
|
||||||
increaseQuality(for: item, by: 3)
|
increaseItemQuality(by: 3)
|
||||||
default:
|
default:
|
||||||
break
|
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
|
import Foundation
|
||||||
|
|
||||||
struct StandardItem: CustomisedItemProtocol, ItemQualityUpdater, ItemSellInUpdater {
|
struct StandardItem: CustomisedItem, ItemQualityUpdater, ItemSellInUpdater {
|
||||||
var item: Item
|
var item: Item
|
||||||
|
|
||||||
public init(item: Item) {
|
public init(item: Item) {
|
||||||
self.item = item
|
self.item = item
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateItemState() {
|
func updateItemState() {
|
||||||
// Reduce the sellIn days for Item by 1
|
// 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
|
// 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())
|
isSellInDatePassed ? reduceItemQuality(by: decreasingValueForZeroOrLessDaysToSell()) : reduceItemQuality(by: decreasingValueOverZeroDaysToSell())
|
||||||
guard isItemOverLowestQuality(item: item) else {
|
guard isItemMoreThanLowestQuality else {
|
||||||
item.quality = ValueConstants.kLowestQualityValue
|
setItemQuality(to: ValueConstants.kLowestQualityValue)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
struct SulfurasItem: CustomisedItemProtocol {
|
struct SulfurasItem: CustomisedItem {
|
||||||
var item: Item
|
var item: Item
|
||||||
|
|
||||||
public init(item: Item) {
|
public init(item: Item) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user