Refactor: check test, delete previous functions

This commit is contained in:
Gayun00 2023-09-26 00:07:59 +09:00
parent 77c82c95b3
commit b7857cb77b
2 changed files with 12 additions and 74 deletions

View File

@ -1,69 +1,19 @@
import { ITEMS } from "./constants"; import { AgedBrie, Passes, Surfras } from "./itemClasses";
import { Item } from "./itemClasses"; type ItemClasses = AgedBrie[] | Surfras[] | Passes[];
export class GildedRose { export class GildedRose {
items: Array<Item>; items: ItemClasses;
constructor(items = [] as ItemClasses) {
constructor(items = [] as Array<Item>) {
this.items = items; this.items = items;
} }
handleSellIn(item) {
//
if (item.name != ITEMS.SURFRAS) {
item.sellIn -= 1;
}
if (item.sellIn >= 0) return;
if (item.quality >= 50) return;
//
switch (item.name) {
case ITEMS.BRIE:
item.quality = item.quality + 1;
break;
case ITEMS.SURFRAS:
item.quality = 0;
break;
default:
item.quality -= 1;
break;
}
}
handlePassesQuality(item) {
//
if (item.name !== ITEMS.PASSES) return;
if (6 <= item.sellIn && item.sellIn < 11) {
item.quality += 1;
}
if (item.sellIn < 6) {
item.quality += 2;
}
}
handleQuality(item) {
//
switch (item.name) {
case ITEMS.PASSES:
this.handlePassesQuality(item);
break;
case ITEMS.SURFRAS:
item.quality -= 1;
break;
}
if (item.quality >= 50) return;
item.quality += 1;
}
updateQuality() { updateQuality() {
for (const item of this.items) { const updatedItems = this.items.map((item) => {
if (!item.quality) break; item.handleQuality();
this.handleSellIn(item);
this.handleQuality(item);
}
return this.items; item.handleSellIn();
return item;
});
return updatedItems as ItemClasses;
} }
} }

View File

@ -8,14 +8,6 @@ export class Item {
this.sellIn = sellIn; this.sellIn = sellIn;
this.quality = quality; this.quality = quality;
} }
handleQuality() {
//
}
handleSellIn() {
//
}
} }
export class AgedBrie extends Item { export class AgedBrie extends Item {
@ -23,7 +15,7 @@ export class AgedBrie extends Item {
super("Aged Brie", sellIn, quality); super("Aged Brie", sellIn, quality);
} }
handleQuality() { handleQuality() {
this.quality++; if (this.quality >= 50) return;
this.quality++; this.quality++;
} }
@ -38,8 +30,6 @@ export class Passes extends Item {
} }
handleQuality() { handleQuality() {
this.quality--;
if (6 <= this.sellIn && this.sellIn < 11) { if (6 <= this.sellIn && this.sellIn < 11) {
this.quality += 1; this.quality += 1;
} }
@ -60,9 +50,7 @@ export class Surfras extends Item {
super("Sulfuras, Hand of Ragnaros", sellIn, quality); super("Sulfuras, Hand of Ragnaros", sellIn, quality);
} }
handleQuality() { handleQuality() {
this.quality = 0; //
this.quality--;
this.quality++;
} }
handleSellIn() { handleSellIn() {