Commit f8d48a44 by 李楚霏

task-6-subclassing

parent 85e441bf
export class AgedBrie {
isAgedBrie() {
return 'Aged Brie';
}
isSulfuras() {
return 'Sulfuras, Hand of Ragnaros';
}
isBackStage() {
return 'Backstage passes to a TAFKAL80ETC concert';
}
}
\ No newline at end of file
import { AgedBrie } from "./AgedBrie"
describe('AgedBrieTest', () => {
it('foo', () => {
const agedBrie = new AgedBrie();
const string = agedBrie.isAgedBrie();
expect(string).toEqual('Aged Brie');
})
})
\ No newline at end of file
export class BackstagePass {
isAgedBrie() {
return 'Aged Brie';
}
isSulfuras() {
return 'Sulfuras, Hand of Ragnaros';
}
isBackStage() {
return 'Backstage passes to a TAFKAL80ETC concert';
}
}
\ No newline at end of file
import { BackstagePass } from "./BackstagePass";
describe('backstagePassTest', () => {
it('foo', () => {
const agedBrie = new BackstagePass();
const string = agedBrie.isBackStage();
expect(string).toEqual('Backstage passes to a TAFKAL80ETC concert');
})
})
\ No newline at end of file
export class Sulfuras {
isAgedBrie() {
return 'Aged Brie';
}
isSulfuras() {
return 'Sulfuras, Hand of Ragnaros';
}
isBackStage() {
return 'Backstage passes to a TAFKAL80ETC concert';
}
}
\ No newline at end of file
import { Sulfuras } from "./Sulfuras";
describe('SulfurasTest', () => {
it('foo', () => {
const agedBrie = new Sulfuras();
const string = agedBrie.isSulfuras();
expect(string).toEqual('Sulfuras, Hand of Ragnaros');
})
})
\ No newline at end of file
import { GildedRose } from './gilded-rose' import { GildedRose } from './gilded-rose'
import { Item } from './item' import { Item } from './item'
const items = [ const items = [
new Item('+5 Dexterity Vest', 10, 20), // createNormalItem('+5 Dexterity Vest', 10, 20),
new Item('Aged Brie', 2, 0), // createAgedBrie(2, 0),
new Item('Elixir of the Mongoose', 5, 7), // createNormalItem('Elixir of the Mongoose', 5, 7),
new Item('Sulfuras, Hand of Ragnaros', 0, 80), // createSulfuras(0, 80),
new Item('Sulfuras, Hand of Ragnaros', -1, 80), createSulfuras(-1, 80),
new Item('Backstage passes to a TAFKAL80ETC concert', 15, 20), createBackStagePass(15, 20),
new Item('Backstage passes to a TAFKAL80ETC concert', 10, 49), createBackStagePass(10, 49),
new Item('Backstage passes to a TAFKAL80ETC concert', 5, 49), createBackStagePass(5, 49),
new Item('Backstage passes to a TAFKAL80ETC concert', 1, 20), createBackStagePass(1, 20),
// this conjured item does not work properly yet // this conjured item does not work properly yet
new Item('Conjured Mana Cake', 3, 6), createNormalItem('Conjured Mana Cake', 3, 6),
] ]
const app = new GildedRose(items) const app = new GildedRose(items)
......
const backStage = 'Backstage passes to a TAFKAL80ETC concert'
const Sulfuras = 'Sulfuras, Hand of Ragnaros'
const AgedBrie = 'Aged Brie'
export class Item { export class Item {
name name
sell_in sell_in
...@@ -12,54 +9,89 @@ export class Item { ...@@ -12,54 +9,89 @@ export class Item {
this.quality = quality this.quality = quality
} }
isAgedBrie() {
return 'Aged Brie';
}
isSulfuras() {
return 'Sulfuras, Hand of Ragnaros';
}
isBackStagePass() {
return 'Backstage passes to a TAFKAL80ETC concert';
}
toString() { toString() {
return `${this.name}, ${this.sell_in}, ${this.quality}` return `${this.name}, ${this.sell_in}, ${this.quality}`
} }
createBackStagePass(sell_in, quality) {
new Item('Backstage passes to a TAFKAL80ETC concert', sell_in, quality);
}
createSulfuras(sell_in, quality) {
new Item('Sulfuras, Hand of Ragnaros', sell_in, quality);
}
createNormalItem(name, sell_in, quality) {
new Item(name, sell_in, quality);
}
createAgedBrie(sell_in, quality) {
new Item('Aged Brie', sell_in, quality);
}
_updateItem(item) { _updateItem(item) {
if (item.name != AgedBrie && item.name != backStage) { if (item.name != this.isAgedBrie() && item.name != this.isBackStagePass()) {
if (item.quality > 0) { if (item.quality > 0) {
if (item.name != Sulfuras) { if (item.name != this.isSulfuras()) {
item.quality = item.quality - 1 item.quality = item.quality - 1
} }
} }
} else { } else {
if (item.quality < 50) { if (item.quality < 50) {
item.quality = item.quality + 1 this.updateQualityAfterExpired(item)
if ( if (item.name == this.isBackStagePass()) {
item.name == backStage) {
if (item.sell_in < 11) { if (item.sell_in < 11) {
if (item.quality < 50) { if (item.quality < 50) {
item.quality = item.quality + 1 this.updateQualityAfterExpired(item);
} }
} }
if (item.sell_in < 6) { if (item.sell_in < 6) {
if (item.quality < 50) { if (item.quality < 50) {
item.quality = item.quality + 1 this.updateQualityAfterExpired(item);
} }
} }
} }
} }
} }
if (item.name != Sulfuras) { if (item.name != this.isSulfuras()) {
item.sell_in = item.sell_in - 1 this.decreaseSellIn(item)
} }
if (item.sell_in < 0) { if (item.sell_in < 0) {
if (item.name != AgedBrie) { if (item.name != this.isAgedBrie()) {
if (item.name != backStage) { if (item.name != this.isBackStagePass()) {
if (item.quality > 0) { if (item.quality > 0) {
if(item.name !=Sulfuras) { if (item.name != this.isSulfuras()) {
item.quality = item.quality - 1 item.quality = item.quality - 1
}
} else {
item.quality = item.quality - item.quality
} }
} else { } else {
item.quality = item.quality - item.quality if (item.quality < 50) {
} this.updateQualityAfterExpired(item);
} else { }
if (item.quality < 50) {
item.quality = item.quality + 1
} }
} }
} }
} }
}
decreaseSellIn(item) {
item.sell_in = item.sell_in - 1
}
updateQualityAfterExpired(item) {
item.quality = item.quality + 1
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment