segmentation per product

This commit is contained in:
Ludwig V 2021-11-11 17:49:09 +01:00
parent f322e70f1e
commit 0dcccbe6c4

View File

@ -12,7 +12,6 @@ export class Item {
static maxQualityThreshold = 50 static maxQualityThreshold = 50
static minQualityThreshold = 0 static minQualityThreshold = 0
static legendaryQuality = 80
} }
export class GildedRose { export class GildedRose {
@ -27,8 +26,12 @@ export class GildedRose {
return item.sellIn < 0 return item.sellIn < 0
} }
shouldDecreaseQuality(item: Item) { isNormalProduct(item: Item) {
return item.name != 'Aged Brie' && item.name != 'Backstage passes to a TAFKAL80ETC concert' return (
item.name !== 'Aged Brie' &&
item.name !== 'Backstage passes to a TAFKAL80ETC concert' &&
item.name !== 'Sulfuras, Hand of Ragnaros'
)
} }
isLegendayProduct(item: Item) { isLegendayProduct(item: Item) {
@ -43,47 +46,67 @@ export class GildedRose {
} }
decrementQuality(item: Item) { decrementQuality(item: Item) {
if (item.quality > Item.minQualityThreshold) { if (item.quality <= Item.minQualityThreshold) {
return item.quality - 1 return item.quality
} }
return item.quality return item.quality - 1
} }
updateQuality() { updateQuality() {
this.items.forEach(item => { this.items.forEach(item => {
//LEGENDARY
if (this.isLegendayProduct(item)) { if (this.isLegendayProduct(item)) {
item.quality = 80
return return
} }
const currentProductName = item.name const currentProductName = item.name
// PART 1
if (this.shouldDecreaseQuality(item)) { // NORMAL PRODUCT
item.quality = this.decrementQuality(item) if (this.isNormalProduct(item)) {
} else { item.sellIn = item.sellIn - 1
item.quality = this.incrementQuality(item)
if (currentProductName === 'Backstage passes to a TAFKAL80ETC concert') { if (this.isOutdated(item)) {
if (item.sellIn < 11) { item.quality = this.decrementQuality(item)
item.quality = this.incrementQuality(item) item.quality = this.decrementQuality(item)
} return
if (item.sellIn < 6) {
item.quality = this.incrementQuality(item)
}
} }
}
// part 2 item.quality = this.decrementQuality(item)
item.sellIn = item.sellIn - 1
// part 3
if (!this.isOutdated(item)) {
return return
} }
if (currentProductName !== 'Aged Brie') { // BACKSTAGE
if (currentProductName !== 'Backstage passes to a TAFKAL80ETC concert') { if (currentProductName === 'Backstage passes to a TAFKAL80ETC concert') {
item.quality = this.decrementQuality(item)
} else {
item.quality = item.quality - item.quality
}
} else {
item.quality = this.incrementQuality(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
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
} }
}) })