mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
Refactoring to have a base class
This commit is contained in:
parent
4458127869
commit
22d55bed7c
@ -1,11 +1,15 @@
|
||||
export class Item {
|
||||
name: string;
|
||||
sellIn: number;
|
||||
quality: number;
|
||||
|
||||
constructor(name, sellIn, quality) {
|
||||
this.name = name;
|
||||
this.sellIn = sellIn;
|
||||
this.quality = quality;
|
||||
constructor(public name: string, public sellIn: number, public quality: number) {}
|
||||
|
||||
/**
|
||||
* update quality is a common across all functions, so moving it to Item and extend them
|
||||
*/
|
||||
updateQuality() {
|
||||
this.sellIn--;
|
||||
this.quality = Math.max(this.quality - 1, 0);
|
||||
|
||||
if (this.sellIn < 0) {
|
||||
this.quality = Math.max(this.quality - 1, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
8
TypeScript/app/components/aged-brie.ts
Normal file
8
TypeScript/app/components/aged-brie.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
16
TypeScript/app/components/backstagepass.ts
Normal file
16
TypeScript/app/components/backstagepass.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
8
TypeScript/app/components/conjured.ts
Normal file
8
TypeScript/app/components/conjured.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
12
TypeScript/app/components/sulfuras.ts
Normal file
12
TypeScript/app/components/sulfuras.ts
Normal 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
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,11 @@
|
||||
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";
|
||||
|
||||
|
||||
export class GildedRose {
|
||||
items: Array<Item>;
|
||||
|
||||
@ -8,110 +13,36 @@ export class GildedRose {
|
||||
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
|
||||
*/
|
||||
updateQuality(){
|
||||
for (let i = 0; i < this.items.length; i++) {
|
||||
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 {
|
||||
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;
|
||||
}
|
||||
}
|
||||
updateQuality() {
|
||||
for (const item of this.items) {
|
||||
item.updateQuality();
|
||||
}
|
||||
|
||||
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;
|
||||
// }
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user