From 70a6cfa04f847bdc7ecd2e0c6868cdee5051465a Mon Sep 17 00:00:00 2001 From: Stefan19K Date: Tue, 3 Dec 2024 17:52:17 +0200 Subject: [PATCH 1/3] requested changes --- app/gilded-rose-refactor.ts | 63 +++++++++++++++++++++++++++++++++++ app/gilded-rose.ts | 2 +- test/jest/gilded-rose.spec.ts | 52 +++++++++++++++++++++++++++-- 3 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 app/gilded-rose-refactor.ts diff --git a/app/gilded-rose-refactor.ts b/app/gilded-rose-refactor.ts new file mode 100644 index 0000000..5133b01 --- /dev/null +++ b/app/gilded-rose-refactor.ts @@ -0,0 +1,63 @@ +export class Item { + name: string; + sellIn: number; + quality: number; + + constructor(name, sellIn, quality) { + this.name = name; + this.sellIn = sellIn; + this.quality = quality; + } +} + +export class GildedRose { + items: Array; + + constructor(items = [] as Array) { + 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].name != 'Sulfuras, Hand of Ragnaros') { + if (this.items[i].quality > 0) { + this.items[i].quality = this.items[i].quality - 1 + } + } + } else { + if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') { + if (this.items[i].sellIn < 6) { + this.items[i].quality += 3 + } else if (this.items[i].sellIn < 11) { + this.items[i].quality += 2 + } + } else { + this.items[i].quality += 1 + } + + if (this.items[i].quality > 50) { + this.items[i].quality = 50 + } + } + + 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 == 'Backstage passes to a TAFKAL80ETC concert') { + this.items[i].quality = 0 + } + + if (this.items[i].name =='Aged Brie') { + if (this.items[i].quality < 50) { + this.items[i].quality = this.items[i].quality + 1 + } + } + } + } + + return this.items; + } +} \ No newline at end of file diff --git a/app/gilded-rose.ts b/app/gilded-rose.ts index ee55134..d053b3f 100644 --- a/app/gilded-rose.ts +++ b/app/gilded-rose.ts @@ -16,7 +16,7 @@ export class GildedRose { constructor(items = [] as Array) { 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') { diff --git a/test/jest/gilded-rose.spec.ts b/test/jest/gilded-rose.spec.ts index 613639f..1401c31 100644 --- a/test/jest/gilded-rose.spec.ts +++ b/test/jest/gilded-rose.spec.ts @@ -1,9 +1,9 @@ -import { Item, GildedRose } from '@/gilded-rose'; +import { Item, GildedRose } from '@/gilded-rose-refactor'; describe('Gilded Rose', () => { it('should foo', () => { // Arrange - const gildedRose = new GildedRose([new Item('foo', 0, 0)]); + const gildedRose = new GildedRose([new Item('bar', 0, 0)]); // Act const items = gildedRose.updateQuality(); @@ -14,7 +14,7 @@ describe('Gilded Rose', () => { it('sword quality drops by 1', () => { // Arrange - const gildedRose = new GildedRose([new Item('Sword', 1, 1)]); + const gildedRose = new GildedRose([new Item('Sword', 1, 2)]); // Act const items = gildedRose.updateQuality(); @@ -22,4 +22,50 @@ describe('Gilded Rose', () => { // Assert expect(items[0].quality).toBe(1); }) + + it('quality goes 0 on name Backstage passes to a TAFKAL80ETC concert and saleIn < 0', () => { + // Arrange + const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', -1, 2)]); + + // Act + const items = gildedRose.updateQuality(); + + // Assert + expect(items[0].quality).toBe(0); + }) + + it('sellIn decrease if name != Sulfuras, Hand of Ragnaros', () => { + // Arrange + const gildedRose = new GildedRose([new Item('Aged Brie', 1, 2), new Item('Sulfuras, Hand of Ragnaros', 1, 2)]); + + // Act + const items = gildedRose.updateQuality(); + + // Assert + expect(items[0].sellIn).toBe(0); + expect(items[1].sellIn).toBe(1); + }) + + it('quality > 50', () => { + // Arrange + const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 5, 48), new Item('Backstage passes to a TAFKAL80ETC concert', 7, 48)]); + + // Act + const items = gildedRose.updateQuality(); + + // Assert + expect(items[0].quality).toBe(50); + expect(items[1].quality).toBe(50); + }) + + it('foo name', () => { + // Arrange + const gildedRose = new GildedRose([new Item('foo', 5, 50)]); + + // Act + const items = gildedRose.updateQuality(); + + // Assert + expect(items[0].quality).toBe(49); + }) }); From c47db8e634f9925bb8c6156ba40be9652c1223bf Mon Sep 17 00:00:00 2001 From: Stefan19K Date: Tue, 3 Dec 2024 21:31:43 +0200 Subject: [PATCH 2/3] more structured refactoring --- app/gilded-rose-refactor.ts | 65 +++++++++++++++++++---------------- test/jest/gilded-rose.spec.ts | 28 ++++++++++----- 2 files changed, 55 insertions(+), 38 deletions(-) diff --git a/app/gilded-rose-refactor.ts b/app/gilded-rose-refactor.ts index 5133b01..b7d06f7 100644 --- a/app/gilded-rose-refactor.ts +++ b/app/gilded-rose-refactor.ts @@ -18,46 +18,51 @@ export class GildedRose { } 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].name != 'Sulfuras, Hand of Ragnaros') { - if (this.items[i].quality > 0) { - this.items[i].quality = this.items[i].quality - 1 - } - } - } else { - if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') { - if (this.items[i].sellIn < 6) { - this.items[i].quality += 3 - } else if (this.items[i].sellIn < 11) { - this.items[i].quality += 2 - } - } else { + for (let i = 0; i < this.items.length; i++) + { + if (this.items[i].name == 'Aged Brie') + { + this.items[i].sellIn -= 1 + + if (this.items[i].sellIn < 0) + this.items[i].quality += 2 + else this.items[i].quality += 1 - } - if (this.items[i].quality > 50) { + if (this.items[i].quality > 50) this.items[i].quality = 50 - } } - if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { - this.items[i].sellIn = this.items[i].sellIn - 1; + if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') + { + this.items[i].sellIn -= 1 + + if (this.items[i].sellIn < 0) + this.items[i].quality = 0 + else + if (this.items[i].quality < 50) + { + if (this.items[i].sellIn < 6) + this.items[i].quality += 3 + else if (this.items[i].sellIn < 11) + this.items[i].quality += 2 + + if (this.items[i].quality > 50) + this.items[i].quality = 50 + } } - if (this.items[i].sellIn < 0) { - if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') { - this.items[i].quality = 0 - } + if (this.items[i].name != 'Aged Brie' && + this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert' && + this.items[i].name != 'Sulfuras, Hand of Ragnaros') + { + this.items[i].sellIn -= 1 - if (this.items[i].name =='Aged Brie') { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1 - } - } + if (this.items[i].quality > 0) + this.items[i].quality -= 1 } } return this.items; } -} \ No newline at end of file +} diff --git a/test/jest/gilded-rose.spec.ts b/test/jest/gilded-rose.spec.ts index 1401c31..91de958 100644 --- a/test/jest/gilded-rose.spec.ts +++ b/test/jest/gilded-rose.spec.ts @@ -23,9 +23,9 @@ describe('Gilded Rose', () => { expect(items[0].quality).toBe(1); }) - it('quality goes 0 on name Backstage passes to a TAFKAL80ETC concert and saleIn < 0', () => { + it('quality goes 0 on name \'Backstage passes to a TAFKAL80ETC concert\' and sellIn <= 0', () => { // Arrange - const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', -1, 2)]); + const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 0, -4)]); // Act const items = gildedRose.updateQuality(); @@ -34,21 +34,21 @@ describe('Gilded Rose', () => { expect(items[0].quality).toBe(0); }) - it('sellIn decrease if name != Sulfuras, Hand of Ragnaros', () => { + it('name == \'Aged Brie\'', () => { // Arrange - const gildedRose = new GildedRose([new Item('Aged Brie', 1, 2), new Item('Sulfuras, Hand of Ragnaros', 1, 2)]); + const gildedRose = new GildedRose([new Item('Aged Brie', 1, 49), new Item('Aged Brie', 0, 49)]); // Act const items = gildedRose.updateQuality(); // Assert - expect(items[0].sellIn).toBe(0); - expect(items[1].sellIn).toBe(1); + expect(items[0].quality).toBe(50); + expect(items[1].quality).toBe(50); }) - it('quality > 50', () => { + it('name == \'Backstage passes to a TAFKAL80ETC concert\'', () => { // Arrange - const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 5, 48), new Item('Backstage passes to a TAFKAL80ETC concert', 7, 48)]); + const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 5, 48), new Item('Backstage passes to a TAFKAL80ETC concert', 7, 49)]); // Act const items = gildedRose.updateQuality(); @@ -58,6 +58,18 @@ describe('Gilded Rose', () => { expect(items[1].quality).toBe(50); }) + it('name == \'Sulfuras, Hand of Ragnaros\'', () => { + // Arrange + const gildedRose = new GildedRose([new Item('Sulfuras, Hand of Ragnaros', 5, 48)]); + + // Act + const items = gildedRose.updateQuality(); + + // Assert + expect(items[0].sellIn).toBe(5); + expect(items[0].quality).toBe(48); + }) + it('foo name', () => { // Arrange const gildedRose = new GildedRose([new Item('foo', 5, 50)]); From 12ef7328ae573b49c566f305e36c6909fe797236 Mon Sep 17 00:00:00 2001 From: Stefan19K Date: Tue, 3 Dec 2024 22:02:45 +0200 Subject: [PATCH 3/3] fix quantity calculation bug --- app/gilded-rose-refactor.ts | 27 ++++++++++++++------------- test/jest/gilded-rose.spec.ts | 10 +++++++--- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/app/gilded-rose-refactor.ts b/app/gilded-rose-refactor.ts index b7d06f7..05efe3a 100644 --- a/app/gilded-rose-refactor.ts +++ b/app/gilded-rose-refactor.ts @@ -23,14 +23,13 @@ export class GildedRose { if (this.items[i].name == 'Aged Brie') { this.items[i].sellIn -= 1 + + if (this.items[i].quality < 50) + this.items[i].quality += 1 - if (this.items[i].sellIn < 0) - this.items[i].quality += 2 - else + if (this.items[i].sellIn < 0 && + this.items[i].quality < 50) this.items[i].quality += 1 - - if (this.items[i].quality > 50) - this.items[i].quality = 50 } if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') @@ -42,13 +41,15 @@ export class GildedRose { else if (this.items[i].quality < 50) { - if (this.items[i].sellIn < 6) - this.items[i].quality += 3 - else if (this.items[i].sellIn < 11) - this.items[i].quality += 2 - - if (this.items[i].quality > 50) - this.items[i].quality = 50 + this.items[i].quality += 1 + + if (this.items[i].sellIn < 11 && + this.items[i].quality < 50) + this.items[i].quality += 1 + + if (this.items[i].sellIn < 6 && + this.items[i].quality < 50) + this.items[i].quality += 1 } } diff --git a/test/jest/gilded-rose.spec.ts b/test/jest/gilded-rose.spec.ts index 91de958..a84ae0d 100644 --- a/test/jest/gilded-rose.spec.ts +++ b/test/jest/gilded-rose.spec.ts @@ -36,7 +36,7 @@ describe('Gilded Rose', () => { it('name == \'Aged Brie\'', () => { // Arrange - const gildedRose = new GildedRose([new Item('Aged Brie', 1, 49), new Item('Aged Brie', 0, 49)]); + const gildedRose = new GildedRose([new Item('Aged Brie', 1, 49), new Item('Aged Brie', 0, 48), new Item('Aged Brie', 0, 70)]); // Act const items = gildedRose.updateQuality(); @@ -44,18 +44,22 @@ describe('Gilded Rose', () => { // Assert expect(items[0].quality).toBe(50); expect(items[1].quality).toBe(50); + expect(items[2].quality).toBe(70); }) it('name == \'Backstage passes to a TAFKAL80ETC concert\'', () => { // Arrange - const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 5, 48), new Item('Backstage passes to a TAFKAL80ETC concert', 7, 49)]); + const gildedRose = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 5, 47), + new Item('Backstage passes to a TAFKAL80ETC concert', 7, 45), + new Item('Backstage passes to a TAFKAL80ETC concert', 7, 70)]); // Act const items = gildedRose.updateQuality(); // Assert expect(items[0].quality).toBe(50); - expect(items[1].quality).toBe(50); + expect(items[1].quality).toBe(47); + expect(items[2].quality).toBe(70); }) it('name == \'Sulfuras, Hand of Ragnaros\'', () => {