Updated updateQuality function

This commit is contained in:
Niveditha Rajala 2021-02-10 16:00:44 -06:00
parent 33373d6fd5
commit 9e1f267d80
2 changed files with 176 additions and 67 deletions

View File

@ -1,69 +1,120 @@
export class Item {
name: string;
sellIn: number;
quality: number;
name: string
sellIn: number
quality: number
constructor(name, sellIn, quality) {
this.name = name;
this.sellIn = sellIn;
this.quality = quality;
}
constructor(name, sellIn, quality) {
this.name = name
this.sellIn = sellIn
this.quality = quality
}
}
export const LegendaryItemList = [
'Sulfuras, Hand of Ragnaros'
]
export const QualityIncreaseList = [
'Backstage passes to a TAFKAL80ETC concert'
]
export const QualityIncreaseAtHigherRateList = [
'Backstage passes to a TAFKAL80ETC concert'
]
export const QualityDecreaseAtHigherRateList = [
'Conjured'
]
export const QualityIncreaseAfterSellInList = [
'Aged Brie'
]
export const QualityBecomesZeroAfterSellIn = [
'Backstage passes to a TAFKAL80ETC concert'
]
export const ItemNames = [
'Backstage passes to a TAFKAL80ETC concert',
'Aged Brie',
'Conjured',
'Sulfuras, Hand of Ragnaros'
]
export class GildedRose {
items: Array<Item>;
items: Array<Item>
constructor(items = [] as Array<Item>) {
this.items = items;
}
constructor(items = [] as Array<Item>) {
this.items = items
}
updateQuality() {
for (let i = 0; i < this.items.length; i++) {
if (this.items[i].name != 'Aged Brie' && this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].quality > 0) {
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
this.items[i].quality = this.items[i].quality - 1
}
}
} else {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].sellIn < 11) {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
}
}
if (this.items[i].sellIn < 6) {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
}
}
}
}
}
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
this.items[i].sellIn = this.items[i].sellIn - 1;
}
if (this.items[i].sellIn < 0) {
if (this.items[i].name != 'Aged Brie') {
if (this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].quality > 0) {
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
this.items[i].quality = this.items[i].quality - 1
}
}
} else {
this.items[i].quality = this.items[i].quality - this.items[i].quality
}
} else {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
}
}
}
updateQuality() {
let legendaryItems: Array<Item>
legendaryItems = this.items.filter((item: Item): boolean => !this.shouldQualityChange(item))
this.items = this.items.filter((item: Item): boolean => this.shouldQualityChange(item))
for (const item of this.items) {
if (ItemNames.indexOf(item.name) !== -1) {
item.sellIn--
if (this.shouldQualityIncrease(item)) {
this.increaseQuality(item)
} else {
this.decreaseQuality(item)
}
return this.items;
} else {
item.name = 'fixme'
}
}
return legendaryItems.concat(this.items)
}
private decreaseQuality(item: Item): void {
let amountToDecrease = 1
if (item.sellIn < 0) {
amountToDecrease = amountToDecrease * 2
}
if (QualityDecreaseAtHigherRateList.indexOf(item.name) !== -1) {
amountToDecrease = amountToDecrease * 2
}
item.quality -= amountToDecrease
if (item.quality < 0) {
item.quality = 0
}
}
private increaseQuality(item: Item): void {
item.quality++
if (QualityIncreaseAtHigherRateList.indexOf(item.name) !== -1 && item.sellIn <= 10) {
item.quality++
if (item.sellIn <= 5) {
item.quality++
}
}
if (item.sellIn < 0 && QualityBecomesZeroAfterSellIn.indexOf(item.name) !== -1) {
item.quality = 0
}
if (item.quality > 50) {
item.quality = 50
}
}
private shouldQualityIncrease(item: Item): boolean {
if (QualityIncreaseList.indexOf(item.name) !== -1) {
return true
}
if (QualityIncreaseAfterSellInList.indexOf(item.name) !== -1 && item.sellIn < 0 ) {
return true
}
return false
}
private shouldQualityChange(item: Item): boolean {
if (LegendaryItemList.indexOf(item.name) !== -1) {
return false
}
return true
}
}

View File

@ -1,12 +1,70 @@
import { expect } from 'chai';
import { Item, GildedRose } from '../app/gilded-rose';
import { expect } from 'chai'
import { Item, GildedRose } from '../app/gilded-rose'
describe('Gilded Rose', function () {
it('should foo', function () {
const gildedRose = new GildedRose([new Item('foo', 0, 0)])
const items = gildedRose.updateQuality()
expect(items[0].name).to.equal('fixme')
})
it('should foo', function() {
const gildedRose = new GildedRose([ new Item('foo', 0, 0) ]);
const items = gildedRose.updateQuality();
expect(items[0].name).to.equal('fixme');
});
it('should not change quality on legendary items', function () {
const gildedRose = new GildedRose([new Item('Sulfuras, Hand of Ragnaros', 12, 80)])
const items = gildedRose.updateQuality()
expect(items[0].quality).to.equal(80)
})
});
it('should set the quality to 0 when the Sellin day is 0 for tickets', function () {
const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 0, 12)])
const items = gildedRose.updateQuality()
expect(items[0].quality).to.equal(0)
})
it('should increase the quality by 2 when the Sellin day is below 10 for tickets', function () {
const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 11, 10)])
const items = gildedRose.updateQuality()
expect(items[0].quality).to.equal(12)
})
it('should increase the quality by 3 when the Sellin day is below 5 for tickets', function () {
const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 6, 10)])
const items = gildedRose.updateQuality()
expect(items[0].quality).to.equal(13)
})
it('should increase the quality by 1 when the Sellin day is below 0 for Brie', function () {
const gildedRose = new GildedRose([new Item('Aged Brie', -1, 4)])
const items = gildedRose.updateQuality()
expect(items[0].quality).to.equal(5)
})
it('should decrease the quality by 1 when the Sellin day is above 0 for Brie', function () {
const gildedRose = new GildedRose([new Item('Aged Brie', 5, 4)])
const items = gildedRose.updateQuality()
expect(items[0].quality).to.equal(3)
})
it('should decrease the quality by 4 when the Sellin day is below 0 for Conjured', function () {
const gildedRose = new GildedRose([new Item('Conjured', 0, 8)])
const items = gildedRose.updateQuality()
expect(items[0].quality).to.equal(4)
})
it('should decrease the quality by 2 when the Sellin day is above 0 for Conjured', function () {
const gildedRose = new GildedRose([new Item('Conjured', 3, 8)])
const items = gildedRose.updateQuality()
expect(items[0].quality).to.equal(6)
})
it('should not decrease the quality when it is already 0', function () {
const gildedRose = new GildedRose([new Item('Conjured', 5, 0)])
const items = gildedRose.updateQuality()
expect(items[0].quality).to.equal(0)
})
it('should not increase the quality when it is already 50', function () {
const gildedRose = new GildedRose([new Item('Aged Brie', 0, 50)])
const items = gildedRose.updateQuality()
expect(items[0].quality).to.equal(50)
})
})