From 59becfb7f642a7282fb9a1ccaef133df5be980b3 Mon Sep 17 00:00:00 2001 From: Sai Sharan Kumar Vuppala Date: Tue, 7 Feb 2023 18:13:35 -0600 Subject: [PATCH] update quality code --- TypeScript/app/gilded-rose.ts | 78 +++++------- TypeScript/app/updateQuality.ts | 71 +++++++++++ TypeScript/test/mocha/gilded-rose.spec.ts | 139 +++++++++++++++++++++- 3 files changed, 237 insertions(+), 51 deletions(-) create mode 100644 TypeScript/app/updateQuality.ts diff --git a/TypeScript/app/gilded-rose.ts b/TypeScript/app/gilded-rose.ts index db58d678..b6371a05 100644 --- a/TypeScript/app/gilded-rose.ts +++ b/TypeScript/app/gilded-rose.ts @@ -1,3 +1,11 @@ +import { + updateQualityForAgedBrieItem, + updateQualityForBackStageConcert, + updateQualityForSulfurasItem, + updateQualityForConjuredItem, + updateQualityForNormalItem +} from './updateQuality'; + export class Item { name: string; sellIn: number; @@ -17,53 +25,31 @@ export class GildedRose { 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(): Item[] { + this.items.forEach(currentItem => { + switch (currentItem.name) { + case 'Aged Brie': { + currentItem = updateQualityForAgedBrieItem(currentItem) + break; + } + case 'Backstage passes to a TAFKAL80ETC concert': { + currentItem = updateQualityForBackStageConcert(currentItem) + break; + } + case 'Sulfuras, Hand of Ragnaros': { + currentItem = updateQualityForSulfurasItem(currentItem) + break; + } + case 'Conjured': { + currentItem = updateQualityForConjuredItem(currentItem) + break; + } + default: { + currentItem = updateQualityForNormalItem(currentItem) + } + } + }); return this.items; } } diff --git a/TypeScript/app/updateQuality.ts b/TypeScript/app/updateQuality.ts new file mode 100644 index 00000000..7228282e --- /dev/null +++ b/TypeScript/app/updateQuality.ts @@ -0,0 +1,71 @@ +import { Item } from '@/gilded-rose'; + +const MAXIMUM_QUALITY = 50 +const MINIMUM_QUALITY = 0 + +const isLessThanMaximum = quality => quality < MAXIMUM_QUALITY +const isOverMinimum = quality => quality > MINIMUM_QUALITY + +const increaseQuality = quality => isLessThanMaximum(quality) ? quality + 1 : quality +const decreaseQuality = quality => isOverMinimum(quality) ? quality - 1 : quality + +export const updateQualityForAgedBrieItem = (item) :Item => { + item.quality = increaseQuality(item.quality) + item.quality = item.sellIn < 0 ? increaseQuality(item.quality) : item.quality + item.sellIn -= 1; + + return item +} + +const increaseQualityForConcert = (item: Item): number => { + let quality = increaseQuality(item.quality); + quality = item.sellIn < 11 ? increaseQuality(quality) : quality; + quality = item.sellIn < 6 ? increaseQuality(quality) : quality; + + return quality +} + +export const updateQualityForBackStageConcert = (item) :Item => { + item.quality = item.sellIn === 0 ? 0 : increaseQualityForConcert(item); + item.sellIn -= 1 + + return item; +} + +export const updateQualityForSulfurasItem = (item) :Item => { + item.quality = 80; + + return item +} + +const updateConjuredQuality = (item):Item => { + item.quality = updateQualityItem(item) + item.quality = updateQualityItem(item) + return item +} + +export const updateQualityForConjuredItem = (item) :Item => { + if (item.sellIn === 5) { + item.quality -= 3 + } else { + item = updateQualityItem(item) + item = updateQualityItem(item) + } + item.sellIn -= 1 + + return item +} + +const updateQualityItem = (item): Item => { + item.quality = decreaseQuality(item.quality); + item.quality = item.sellIn <= 0 ? decreaseQuality(item.quality) : item.quality + + return item +} + +export const updateQualityForNormalItem = (item) :Item => { + item = updateQualityItem(item) + item.sellIn -= 1 + + return item +} \ No newline at end of file diff --git a/TypeScript/test/mocha/gilded-rose.spec.ts b/TypeScript/test/mocha/gilded-rose.spec.ts index 02cd24c5..7f630dc8 100644 --- a/TypeScript/test/mocha/gilded-rose.spec.ts +++ b/TypeScript/test/mocha/gilded-rose.spec.ts @@ -2,9 +2,138 @@ import { expect } from 'chai'; import { Item, GildedRose } from '@/gilded-rose'; describe('Gilded Rose', () => { - it('should foo', () => { - const gildedRose = new GildedRose([new Item('foo', 0, 0)]); - const items = gildedRose.updateQuality(); - expect(items[0].name).to.equal('fixme'); - }); + + it('should add a new item to the app', () => { + const gildedRose = new GildedRose([ new Item('foo', 0, 0) ]); + const added = gildedRose.items[0] + expect(added.name).to.equal('foo'); + expect(added.quality).to.equal(0); + expect(added.sellIn).to.equal(0); + }); }); + +describe('validate normal quality rules', () => { + it('should update quality for sellin of 1 day', () => { + const gildedRose = new GildedRose([ new Item('foo', 1, 1) ]); + const items = gildedRose.updateQuality(); + const added =items[0] + expect(added.quality).to.equal(0); + expect(added.sellIn).to.equal(0); + }); + + it('should update quality 2x as fast for sellin of 0 days', () => { + const gildedRose = new GildedRose([ new Item('foo', 0, 4) ]); + const items = gildedRose.updateQuality(); + const added =items[0] + expect(added.quality).to.equal(2); + expect(added.sellIn).to.equal(-1); + }); + + it('should check quality value not 0', () => { + const gildedRose = new GildedRose([ new Item('foo', 0, 1) ]); + const items = gildedRose.updateQuality(); + const added = items[0] + expect(added.quality).to.equal(0); + expect(added.sellIn).to.equal(-1); + }); +}) + +describe('aged brie quality', () => { + it('Validate Aged Brie quality rules', () => { + const gildedRose = new GildedRose([ new Item('Aged Brie', 1, 1) ]); + const items = gildedRose.updateQuality(); + const added =items[0] + expect(added.quality).to.equal(2); + expect(added.sellIn).to.equal(0); + }); + + it('should check quality not greater than 50', () => { + const gildedRose = new GildedRose([ new Item('Aged Brie', 1, 50) ]); + const items = gildedRose.updateQuality(); + const added =items[0] + expect(added.quality).to.equal(50); + expect(added.sellIn).to.equal(0); + }); + it('should allow quality of aged brie to be incremented up to 50', () => { + const gildedRose = new GildedRose([ new Item('Aged Brie', -10, 10) ]); + const items = gildedRose.updateQuality(); + const added =items[0] + expect(added.quality).to.equal(12); + expect(added.sellIn).to.equal(-11); + }); +}) + +describe('validate sulfuras quality rules', () => { + it('should check quality of sulfuras do not change', () => { + const gildedRose = new GildedRose([ new Item('Sulfuras, Hand of Ragnaros', 1, 1) ]); + const items = gildedRose.updateQuality(); + const added =items[0] + expect(added.quality).to.equal(80); + expect(added.sellIn).to.equal(1); + }); +}) + +describe('validate backstage pass quality rules', () => { + it('should increment quality of backstage passes by 1 when more than 10 days remaining', () => { + const gildedRose = new GildedRose([ new Item('Backstage passes to a TAFKAL80ETC concert', 11, 1) ]); + const items = gildedRose.updateQuality(); + const added =items[0] + expect(added.quality).to.equal(2); + expect(added.sellIn).to.equal(10); + }); + it('should increment quality of backstage passes by 2 when more than 5 days remaining', () => { + const gildedRose = new GildedRose([ new Item('Backstage passes to a TAFKAL80ETC concert', 6, 1) ]); + const items = gildedRose.updateQuality(); + const added =items[0] + expect(added.quality).to.equal(3); + expect(added.sellIn).to.equal(5); + }); + it('should increment quality of backstage passes by 3 when less than 5 days remaining', () => { + const gildedRose = new GildedRose([ new Item('Backstage passes to a TAFKAL80ETC concert', 3, 1) ]); + const items = gildedRose.updateQuality(); + const added =items[0] + expect(added.quality).to.equal(4); + expect(added.sellIn).to.equal(2); + }); + it('should set quality of backstage passes to 0 after concert', () => { + const gildedRose = new GildedRose([ new Item('Backstage passes to a TAFKAL80ETC concert', 0, 10) ]); + const items = gildedRose.updateQuality(); + const added =items[0] + expect(added.quality).to.equal(0); + expect(added.sellIn).to.equal(-1); + }); +}) + +describe('validate conjured items quality rules', () => { + it('should increment quality for conjured sellin 1 day', () => { + const gildedRose = new GildedRose([ new Item('Conjured', 1, 2) ]); + const items = gildedRose.updateQuality(); + const added =items[0] + expect(added.quality).to.equal(0); + expect(added.sellIn).to.equal(0); + }); + + it('should increment conjured quality 4x as fast for sellin 0 days', () => { + const gildedRose = new GildedRose([ new Item('Conjured', 0, 4) ]); + const items = gildedRose.updateQuality(); + const added =items[0] + expect(added.quality).to.equal(0); + expect(added.sellIn).to.equal(-1); + }); + + it('should check conjured item quality not below 0', () => { + const gildedRose = new GildedRose([ new Item('Conjured', 0, 1) ]); + const items = gildedRose.updateQuality(); + const added = items[0] + expect(added.quality).to.equal(0); + expect(added.sellIn).to.equal(-1); + }); + + it('should lower the conjured quality by 3 if exactly 5 days left', () => { + const gildedRose = new GildedRose([ new Item('Conjured', 5, 6 )]) + const items = gildedRose.updateQuality(); + const added = items[0] + expect(added.quality).to.equal(3); + expect(added.sellIn).to.equal(4); + }) +}) \ No newline at end of file