mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
- Code Refactoring : Adding code for Custom Items
This commit is contained in:
parent
f19d8141a4
commit
d84a836ca6
27
swift/Sources/GildedRose/CustomisedItemFactory.swift
Normal file
27
swift/Sources/GildedRose/CustomisedItemFactory.swift
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Manali Mogre on 16/08/2020.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
class CustomisedItemFactory {
|
||||||
|
func getCustomisedItem(item: Item) -> CustomisedItemProtocol {
|
||||||
|
switch item.name {
|
||||||
|
case ItemNameConstants.kAgedBrieItem:
|
||||||
|
return AgedBrieItem(item: item)
|
||||||
|
case ItemNameConstants.kBackstagePassesItem:
|
||||||
|
return BackstagePassesItem(item: item)
|
||||||
|
case ItemNameConstants.kSulfurasItem:
|
||||||
|
return SulfurasItem(item: item)
|
||||||
|
default:
|
||||||
|
return StandardItem(item: item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -6,6 +6,14 @@ public class GildedRose {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func updateQuality() {
|
public func updateQuality() {
|
||||||
|
for item in items {
|
||||||
|
let customFactoryObj = CustomisedItemFactory()
|
||||||
|
let customItem = customFactoryObj.getCustomisedItem(item: item)
|
||||||
|
customItem.updateCustomItemQuality()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* public func updateQuality() {
|
||||||
for item in items {
|
for item in items {
|
||||||
switch item.name {
|
switch item.name {
|
||||||
case ItemNameConstants.kBackstagePassesItem:
|
case ItemNameConstants.kBackstagePassesItem:
|
||||||
@ -37,7 +45,7 @@ public class GildedRose {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* public func updateQuality() {
|
public func updateQuality() {
|
||||||
for i in 0..<items.count {
|
for i in 0..<items.count {
|
||||||
if (items[i].name != "Aged Brie" && items[i].name != "Backstage passes to a TAFKAL80ETC concert") {
|
if (items[i].name != "Aged Brie" && items[i].name != "Backstage passes to a TAFKAL80ETC concert") {
|
||||||
if (items[i].quality > 0) {
|
if (items[i].quality > 0) {
|
||||||
|
|||||||
@ -12,3 +12,9 @@ struct ItemNameConstants {
|
|||||||
static let kBackstagePassesItem: String = "Backstage passes to a TAFKAL80ETC concert"
|
static let kBackstagePassesItem: String = "Backstage passes to a TAFKAL80ETC concert"
|
||||||
static let kSulfurasItem: String = "Sulfuras, Hand of Ragnaros"
|
static let kSulfurasItem: String = "Sulfuras, Hand of Ragnaros"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ValueConstants {
|
||||||
|
static let kHightestQualityValue = 50
|
||||||
|
static let kLowestQualityValue = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -7,13 +7,18 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
struct AgedBrieItem: CustomisedItemProtocol {
|
struct AgedBrieItem: CustomisedItemProtocol, ItemSellInDaysProtocol, ItemQualityProtocol {
|
||||||
var item: Item
|
var item: Item
|
||||||
|
|
||||||
public init(item: Item) {
|
public init(item: Item) {
|
||||||
self.item = item
|
self.item = item
|
||||||
}
|
}
|
||||||
func updateCustomItemQuality() {
|
func updateCustomItemQuality() {
|
||||||
|
// update the sell in days
|
||||||
|
reduceSellInDaysByOne(item: item)
|
||||||
|
// Increment the Item quality by 1 if the quality is less than 50
|
||||||
|
guard isItemUnderHighestQuality(item: item) else {return}
|
||||||
|
increaseQuality(for: item, by: 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,13 +7,34 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
struct BackstagePassesItem: CustomisedItemProtocol {
|
struct BackstagePassesItem: CustomisedItemProtocol, ItemQualityProtocol, ItemSellInDaysProtocol {
|
||||||
var item: Item
|
var item: Item
|
||||||
|
|
||||||
public init(item: Item) {
|
public init(item: Item) {
|
||||||
self.item = item
|
self.item = item
|
||||||
}
|
}
|
||||||
func updateCustomItemQuality() {
|
func updateCustomItemQuality() {
|
||||||
|
reduceSellInDaysByOne(item: item)
|
||||||
|
|
||||||
|
if isSellInDaysMoreThanOrEqualTo(days: 10) {
|
||||||
|
increaseQuality(for: item, by: 1)
|
||||||
|
}
|
||||||
|
else if isSellInDaysMoreThanOrEqualTo(days: 5) {
|
||||||
|
increaseQuality(for: item, by: 2)
|
||||||
|
}
|
||||||
|
else if isSellInDaysMoreThanOrEqualTo(days: 0) {
|
||||||
|
increaseQuality(for: item, by: 3)
|
||||||
|
}
|
||||||
|
if HasSellInDatePassed(item: item) {
|
||||||
|
item.quality = ValueConstants.kLowestQualityValue
|
||||||
|
}
|
||||||
|
if !isItemUnderHighestQuality(item: item) {
|
||||||
|
item.quality = ValueConstants.kHightestQualityValue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func isSellInDaysMoreThanOrEqualTo(days: Int) -> Bool {
|
||||||
|
return item.sellIn >= days
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,3 +12,37 @@ protocol CustomisedItemProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protocol ItemSellInDaysProtocol {
|
||||||
|
func reduceSellInDaysByOne(item: Item)
|
||||||
|
}
|
||||||
|
|
||||||
|
extension ItemSellInDaysProtocol {
|
||||||
|
func reduceSellInDaysByOne(item: Item) {
|
||||||
|
item.sellIn -= 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func HasSellInDatePassed(item: Item) -> Bool {
|
||||||
|
return item.sellIn < 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protocol ItemQualityProtocol {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
extension ItemQualityProtocol {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
struct StandardItem: CustomisedItemProtocol {
|
struct StandardItem: CustomisedItemProtocol, ItemQualityProtocol, ItemSellInDaysProtocol {
|
||||||
|
|
||||||
var item: Item
|
var item: Item
|
||||||
|
|
||||||
@ -15,6 +15,22 @@ struct StandardItem: CustomisedItemProtocol {
|
|||||||
self.item = item
|
self.item = item
|
||||||
}
|
}
|
||||||
func updateCustomItemQuality() {
|
func updateCustomItemQuality() {
|
||||||
|
// Reduce the sellIn days for Item by 1
|
||||||
|
reduceSellInDaysByOne(item: item)
|
||||||
|
|
||||||
|
// 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
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func decreasingValueOverZeroDaysToSell() -> Int {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
private func decreasingValueForZeroOrLessDaysToSell() -> Int {
|
||||||
|
return 2 * decreasingValueOverZeroDaysToSell()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,6 +14,6 @@ struct SulfurasItem: CustomisedItemProtocol {
|
|||||||
self.item = item
|
self.item = item
|
||||||
}
|
}
|
||||||
func updateCustomItemQuality() {
|
func updateCustomItemQuality() {
|
||||||
|
// No code as there is no change in quality or sell in days of sulfuras item
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user