Refactoring to have a base class

This commit is contained in:
Vijayanto 2023-10-05 09:33:01 +05:30
parent 4458127869
commit 22d55bed7c
6 changed files with 88 additions and 109 deletions

View File

@ -1,11 +1,15 @@
export class Item { export class Item {
name: string; constructor(public name: string, public sellIn: number, public quality: number) {}
sellIn: number;
quality: number; /**
* update quality is a common across all functions, so moving it to Item and extend them
constructor(name, sellIn, quality) { */
this.name = name; updateQuality() {
this.sellIn = sellIn; this.sellIn--;
this.quality = quality; this.quality = Math.max(this.quality - 1, 0);
if (this.sellIn < 0) {
this.quality = Math.max(this.quality - 1, 0);
}
} }
} }

View File

@ -0,0 +1,8 @@
import { Item } from "./Item";
export class AgedBrie extends Item {
updateQuality() {
super.updateQuality();
this.quality = Math.min(this.quality + 1, 50);
}
}

View File

@ -0,0 +1,16 @@
import { Item } from "./Item";
export class BackstagePass extends Item {
updateQuality() {
super.updateQuality();
if (this.sellIn < 0) {
this.quality = 0;
} else if (this.sellIn <= 5) {
this.quality = Math.min(this.quality + 3, 50);
} else if (this.sellIn <= 10) {
this.quality = Math.min(this.quality + 2, 50);
} else {
this.quality = Math.min(this.quality + 1, 50);
}
}
}

View File

@ -0,0 +1,8 @@
import { Item } from "./Item";
export class Conjured extends Item {
updateQuality() {
super.updateQuality();
this.quality = Math.max(this.quality - 2, 0);
}
}

View File

@ -0,0 +1,12 @@
import { ItemConstants } from "@/constants/constant";
import { Item } from "./Item";
export class Sulfuras extends Item {
constructor() {
super(ItemConstants.SULFURAS, 0, 80);
}
updateQuality() {
// Sulfuras quality never changes
}
}

View File

@ -1,6 +1,11 @@
import { Item } from "./components/Item"; import { Item } from "./components/Item";
import { AgedBrie } from "./components/aged-brie";
import { BackstagePass } from "./components/backstagepass";
import { Conjured } from "./components/conjured";
import { Sulfuras } from "./components/sulfuras";
import { ItemConstants } from "./constants/constant"; import { ItemConstants } from "./constants/constant";
export class GildedRose { export class GildedRose {
items: Array<Item>; items: Array<Item>;
@ -8,110 +13,36 @@ export class GildedRose {
this.items = items; this.items = items;
} }
/**
* Function to create item
* @param name
* @param sellIn
* @param quality
* @returns
*/
static createItem(name: string, sellIn: number, quality: number): Item {
switch (name) {
case ItemConstants.AGED_BRIE:
return new AgedBrie(name, sellIn, quality);
case ItemConstants.BACKSTAGE:
return new BackstagePass(name, sellIn, quality);
case ItemConstants.SULFURAS:
return new Sulfuras();
case ItemConstants.CONJURED:
return new Conjured(name, sellIn, quality);
default:
return new Item(name, sellIn, quality);
}
}
/** /**
* Function to update quality * Function to update quality
*/ */
updateQuality(){ updateQuality() {
for (let i = 0; i < this.items.length; i++) { for (const item of this.items) {
const item = this.items[i]; item.updateQuality();
}
// 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 {
item.quality = Math.min(item.quality + 1, ItemConstants.MAX_QUALITY);
}
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; 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;
// }
} }