Refactor update quality

This commit is contained in:
Vijayanto 2023-10-05 08:49:12 +05:30
parent 5c9cabfbad
commit 4458127869
4 changed files with 126 additions and 54 deletions

View File

@ -0,0 +1,11 @@
export class Item {
name: string;
sellIn: number;
quality: number;
constructor(name, sellIn, quality) {
this.name = name;
this.sellIn = sellIn;
this.quality = quality;
}
}

View File

@ -0,0 +1,12 @@
export class ItemConstants {
// static readonly properties to create constants
static readonly AGED_BRIE:string = 'Aged Brie';
static readonly BACKSTAGE: string = 'Backstage passes to a TAFKAL80ETC concert';
static readonly SULFURAS: string = 'Sulfuras, Hand of Ragnaros';
static readonly CONJURED: string = 'Conjured';
//The Quality of an item is never more than 50
static readonly MAX_QUALITY: number = 50;
}

View File

@ -1,14 +1,5 @@
export class Item {
name: string;
sellIn: number;
quality: number;
constructor(name, sellIn, quality) {
this.name = name;
this.sellIn = sellIn;
this.quality = quality;
}
}
import { Item } from "./components/Item";
import { ItemConstants } from "./constants/constant";
export class GildedRose {
items: Array<Item>;
@ -17,53 +8,110 @@ export class GildedRose {
this.items = items;
}
updateQuality() {
/**
* Function to update quality
*/
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
}
}
const item = this.items[i];
// Check for specific item types and update quality accordingly
switch (item.name) {
case ItemConstants.AGED_BRIE:
item.sellIn--;
//"Aged Brie" actually increases in Quality the older it gets
item.quality = Math.min(item.quality + 1, ItemConstants.MAX_QUALITY);
break;
case ItemConstants.BACKSTAGE:
item.sellIn--;
// The Quality of an item is never negative
if (item.sellIn < 0) {
item.quality = 0;
} else if (item.sellIn <= 5) {
// by 3 when there are 5 days or less but
item.quality = Math.min(item.quality + 3, ItemConstants.MAX_QUALITY);
} else if (item.sellIn <= 10) {
// Quality increases by 2 when there are 10 days or less
item.quality = Math.min(item.quality + 2, ItemConstants.MAX_QUALITY);
} else {
this.items[i].quality = this.items[i].quality - this.items[i].quality
item.quality = Math.min(item.quality + 1, ItemConstants.MAX_QUALITY);
}
} else {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
break;
case ItemConstants.SULFURAS:
// "Sulfuras", being a legendary item, never has to be sold or decreases in Quality
break;
case ItemConstants.CONJURED:
item.sellIn--;
// "Conjured" items degrade in Quality twice as fast as normal items
item.quality = Math.max(item.quality - 2, 0);
break;
default:
// Normal item
item.sellIn--;
item.quality = Math.max(item.quality - 1, 0);
// The Quality of an item is never negative
if (item.sellIn < 0) {
item.quality = Math.max(item.quality - 1, 0);
}
}
break;
}
}
}
return this.items;
}
// updateQualityOld() {
// for (let i = 0; i < this.items.length; i++) {
// if (this.items[i].name != ItemConstants.AGED_BRIE && this.items[i].name != ItemConstants.BACKSTAGE) {
// if (this.items[i].quality > 0) {
// if (this.items[i].name != ItemConstants.SULFURAS) {
// 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 == ItemConstants.BACKSTAGE) {
// 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 != ItemConstants.SULFURAS) {
// this.items[i].sellIn = this.items[i].sellIn - 1;
// }
// if (this.items[i].sellIn < 0) {
// if (this.items[i].name != ItemConstants.AGED_BRIE) {
// if (this.items[i].name != ItemConstants.BACKSTAGE) {
// if (this.items[i].quality > 0) {
// if (this.items[i].name != ItemConstants.SULFURAS) {
// 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
// }
// }
// }
// }
// return this.items;
// }
}

View File

@ -1,9 +1,10 @@
import { Item, GildedRose } from '@/gilded-rose';
import { Item } from '@/components/Item';
import { 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).toBe('fixme');
expect(items[0].name).toBe('foo');
});
});