Refactor: use items constants

This commit is contained in:
Gayun00 2023-09-23 23:11:10 +09:00
parent a56e6acd5a
commit 99177b74a0
5 changed files with 32 additions and 25 deletions

View File

@ -9,4 +9,3 @@ test/**/*.js.map
coverage coverage
.nyc_output .nyc_output
.yarn .yarn
.DS_Store

View File

@ -0,0 +1,5 @@
export const ITEMS = {
BRIE: "Aged Brie",
SURFRAS: "Sulfuras, Hand of Ragnaros",
PASSES: "Backstage passes to a TAFKAL80ETC concert",
};

View File

@ -1,3 +1,5 @@
import { ITEMS } from "./constants";
export class Item { export class Item {
name: string; name: string;
sellIn: number; sellIn: number;
@ -19,46 +21,50 @@ export class GildedRose {
updateQuality() { updateQuality() {
for (let i = 0; i < this.items.length; i++) { 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 != ITEMS.BRIE &&
this.items[i].name != ITEMS.PASSES
) {
if (this.items[i].quality > 0) { if (this.items[i].quality > 0) {
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { if (this.items[i].name != ITEMS.SURFRAS) {
this.items[i].quality = this.items[i].quality - 1 this.items[i].quality = this.items[i].quality - 1;
} }
} }
} else { } else {
if (this.items[i].quality < 50) { if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1 this.items[i].quality = this.items[i].quality + 1;
if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') { if (this.items[i].name == ITEMS.PASSES) {
if (this.items[i].sellIn < 11) { if (this.items[i].sellIn < 11) {
if (this.items[i].quality < 50) { if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1 this.items[i].quality = this.items[i].quality + 1;
} }
} }
if (this.items[i].sellIn < 6) { if (this.items[i].sellIn < 6) {
if (this.items[i].quality < 50) { if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1 this.items[i].quality = this.items[i].quality + 1;
} }
} }
} }
} }
} }
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { if (this.items[i].name != ITEMS.SURFRAS) {
this.items[i].sellIn = this.items[i].sellIn - 1; this.items[i].sellIn = this.items[i].sellIn - 1;
} }
if (this.items[i].sellIn < 0) { if (this.items[i].sellIn < 0) {
if (this.items[i].name != 'Aged Brie') { if (this.items[i].name != ITEMS.BRIE) {
if (this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') { if (this.items[i].name != ITEMS.PASSES) {
if (this.items[i].quality > 0) { if (this.items[i].quality > 0) {
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { if (this.items[i].name != ITEMS.SURFRAS) {
this.items[i].quality = this.items[i].quality - 1 this.items[i].quality = this.items[i].quality - 1;
} }
} }
} else { } else {
this.items[i].quality = this.items[i].quality - this.items[i].quality this.items[i].quality =
this.items[i].quality - this.items[i].quality;
} }
} else { } else {
if (this.items[i].quality < 50) { if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1 this.items[i].quality = this.items[i].quality + 1;
} }
} }
} }

View File

@ -1,4 +1,4 @@
import { Item, GildedRose } from '../app/gilded-rose'; import { Item, GildedRose } from "../app/gilded-rose";
const items = [ const items = [
new Item("+5 Dexterity Vest", 10, 20), // new Item("+5 Dexterity Vest", 10, 20), //
@ -10,22 +10,21 @@ const items = [
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49), new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49),
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49), new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49),
// this conjured item does not work properly yet // this conjured item does not work properly yet
new Item("Conjured Mana Cake", 3, 6)]; // new Item("Conjured Mana Cake", 3, 6)
];
const gildedRose = new GildedRose(items); const gildedRose = new GildedRose(items);
let days: number = 2; let days: number = 2;
if (process.argv.length > 2) { if (process.argv.length > 2) {
days = +process.argv[2]; days = +process.argv[2];
} }
for (let i = 0; i < days; i++) { for (let i = 0; i < days; i++) {
console.log("-------- day " + i + " --------"); console.log("-------- day " + i + " --------");
console.log("name, sellIn, quality"); console.log("name, sellIn, quality");
items.forEach(element => { items.forEach((element) => {
console.log(element.name + ' ' + element.sellIn + ' ' + element.quality); console.log(element.name + " " + element.sellIn + " " + element.quality);
}); });
console.log(); console.log();
gildedRose.updateQuality(); gildedRose.updateQuality();

View File

@ -77,6 +77,4 @@ describe("Backstage passes 테스트", () => {
expect(items[0].sellIn).toBe(4); expect(items[0].sellIn).toBe(4);
expect(items[0].quality).toBe(6); expect(items[0].quality).toBe(6);
}); });
// TODO: 콘서트 종료 후 quality 0으로 변경 테스트
}); });