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