mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 15:01:28 +00:00
refactor per product
This commit is contained in:
parent
0dcccbe6c4
commit
9e83d49374
@ -4,7 +4,7 @@ export class Item {
|
|||||||
sellIn: number
|
sellIn: number
|
||||||
quality: number
|
quality: number
|
||||||
|
|
||||||
constructor(name, sellIn, quality) {
|
constructor(name: string, sellIn: number, quality: number) {
|
||||||
this.name = name
|
this.name = name
|
||||||
this.sellIn = sellIn
|
this.sellIn = sellIn
|
||||||
this.quality = quality
|
this.quality = quality
|
||||||
@ -12,6 +12,7 @@ export class Item {
|
|||||||
|
|
||||||
static maxQualityThreshold = 50
|
static maxQualityThreshold = 50
|
||||||
static minQualityThreshold = 0
|
static minQualityThreshold = 0
|
||||||
|
static legendaryQuality = 80
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GildedRose {
|
export class GildedRose {
|
||||||
@ -21,95 +22,79 @@ export class GildedRose {
|
|||||||
this.items = items
|
this.items = items
|
||||||
}
|
}
|
||||||
|
|
||||||
// est périmité ?
|
|
||||||
isOutdated(item: Item) {
|
isOutdated(item: Item) {
|
||||||
return item.sellIn < 0
|
return item.sellIn < 0
|
||||||
}
|
}
|
||||||
|
|
||||||
isNormalProduct(item: Item) {
|
|
||||||
return (
|
|
||||||
item.name !== 'Aged Brie' &&
|
|
||||||
item.name !== 'Backstage passes to a TAFKAL80ETC concert' &&
|
|
||||||
item.name !== 'Sulfuras, Hand of Ragnaros'
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
isLegendayProduct(item: Item) {
|
|
||||||
return item.name === 'Sulfuras, Hand of Ragnaros'
|
|
||||||
}
|
|
||||||
|
|
||||||
incrementQuality(item: Item) {
|
incrementQuality(item: Item) {
|
||||||
if (item.quality < Item.maxQualityThreshold) {
|
return item.quality < Item.maxQualityThreshold ? item.quality + 1 : item.quality
|
||||||
return item.quality + 1
|
|
||||||
}
|
|
||||||
return item.quality
|
|
||||||
}
|
}
|
||||||
|
|
||||||
decrementQuality(item: Item) {
|
decrementQuality(item: Item) {
|
||||||
if (item.quality <= Item.minQualityThreshold) {
|
return item.quality > Item.minQualityThreshold ? item.quality - 1 : item.quality
|
||||||
return item.quality
|
|
||||||
}
|
|
||||||
return item.quality - 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
updateQuality() {
|
updateQuality() {
|
||||||
this.items.forEach(item => {
|
return this.items.map(item => {
|
||||||
//LEGENDARY
|
switch (item.name) {
|
||||||
if (this.isLegendayProduct(item)) {
|
case 'Sulfuras, Hand of Ragnaros':
|
||||||
item.quality = 80
|
this.updateLegendaryProduct(item)
|
||||||
return
|
return item
|
||||||
}
|
case 'Aged Brie':
|
||||||
const currentProductName = item.name
|
this.updateAgedBrie(item)
|
||||||
|
return item
|
||||||
// NORMAL PRODUCT
|
case 'Backstage passes to a TAFKAL80ETC concert':
|
||||||
if (this.isNormalProduct(item)) {
|
this.updateBackstageProduct(item)
|
||||||
item.sellIn = item.sellIn - 1
|
return item
|
||||||
|
default:
|
||||||
if (this.isOutdated(item)) {
|
this.updateNormalProduct(item)
|
||||||
item.quality = this.decrementQuality(item)
|
return item
|
||||||
item.quality = this.decrementQuality(item)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
item.quality = this.decrementQuality(item)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// BACKSTAGE
|
|
||||||
if (currentProductName === 'Backstage passes to a TAFKAL80ETC concert') {
|
|
||||||
item.quality = this.incrementQuality(item)
|
|
||||||
|
|
||||||
if (item.sellIn < 11) {
|
|
||||||
item.quality = this.incrementQuality(item)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.sellIn < 6) {
|
|
||||||
item.quality = this.incrementQuality(item)
|
|
||||||
}
|
|
||||||
|
|
||||||
item.sellIn = item.sellIn - 1
|
|
||||||
|
|
||||||
if (item.sellIn <= 0) {
|
|
||||||
item.quality = 0
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// AGED BRIE
|
|
||||||
if (currentProductName === 'Aged Brie') {
|
|
||||||
item.quality = this.incrementQuality(item)
|
|
||||||
item.sellIn = item.sellIn - 1
|
|
||||||
if (!this.isOutdated(item)) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
item.quality = this.incrementQuality(item)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return this.items
|
private updateLegendaryProduct(item: Item) {
|
||||||
|
item.quality = Item.legendaryQuality
|
||||||
|
}
|
||||||
|
|
||||||
|
private updateAgedBrie(item: Item) {
|
||||||
|
//
|
||||||
|
item.sellIn = item.sellIn - 1
|
||||||
|
|
||||||
|
item.quality = this.incrementQuality(item)
|
||||||
|
|
||||||
|
if (!this.isOutdated(item)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
item.quality = this.incrementQuality(item)
|
||||||
|
|
||||||
|
return { ...item, sellIn: item.sellIn - 1, quality: 0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
private updateBackstageProduct(item: Item) {
|
||||||
|
item.quality = this.incrementQuality(item)
|
||||||
|
|
||||||
|
if (item.sellIn < 11) {
|
||||||
|
item.quality = this.incrementQuality(item)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.sellIn < 6) {
|
||||||
|
item.quality = this.incrementQuality(item)
|
||||||
|
}
|
||||||
|
|
||||||
|
item.sellIn = item.sellIn - 1
|
||||||
|
|
||||||
|
if (item.sellIn <= 0) {
|
||||||
|
item.quality = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private updateNormalProduct(item: Item) {
|
||||||
|
item.sellIn = item.sellIn - 1
|
||||||
|
item.quality = this.decrementQuality(item)
|
||||||
|
if (this.isOutdated(item)) {
|
||||||
|
item.quality = this.decrementQuality(item)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,13 +13,13 @@ export const itemsGoldenMaster = [
|
|||||||
new Item('Conjured Mana Cake', 3, 6),
|
new Item('Conjured Mana Cake', 3, 6),
|
||||||
]
|
]
|
||||||
|
|
||||||
export const gildedRose = new GildedRose(itemsGoldenMaster)
|
// export const gildedRose = new GildedRose(itemsGoldenMaster)
|
||||||
const days = 2
|
// const days = 2
|
||||||
for (let i = 0; i < days; i++) {
|
// for (let i = 0; i < days; i++) {
|
||||||
console.log('-------- day ' + i + ' --------')
|
// console.log('-------- day ' + i + ' --------')
|
||||||
console.log('name, sellIn, quality')
|
// console.log('name, sellIn, quality')
|
||||||
itemsGoldenMaster.forEach(element => {
|
// itemsGoldenMaster.forEach(element => {
|
||||||
console.log(element.name + ' ' + element.sellIn + ' ' + element.quality)
|
// console.log(element.name + ' ' + element.sellIn + ' ' + element.quality)
|
||||||
})
|
// })
|
||||||
console.log()
|
// console.log()
|
||||||
}
|
// }
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user