Commit 24efc7bd by 李楚霏

task-9-整理子类函数;消除不需要的参数

parent e3923306
import { Item } from './item' import { Item } from './item'
export class AgedBrie extends Item{ export class AgedBrie extends Item {
constructor(sell_in, quality) { constructor(sell_in, quality) {
super(sell_in, quality) super(sell_in, quality)
this.name = 'Aged Brie' this.name = 'Aged Brie'
} }
isAgedBrie() {
return 'Aged Brie'
}
updateQuality(item) { updateQuality(item) {
if (item.quality < 50) { if (item.quality < 50) {
item.quality = item.quality + 1 item.quality = item.quality + 1
} }
} }
updateQualityAfterExpired(item) {
return
}
isAgedBire() {
return true
}
} }
import { AgedBrie } from "./AgedBrie"
describe('AgedBrieTest', () => {
it('foo', () => {
const agedBrie = new AgedBrie(2, 0);
const string = agedBrie.isAgedBrie();
expect(string).toEqual('Aged Brie');
})
})
\ No newline at end of file
...@@ -7,24 +7,22 @@ export class BackstagePass extends Item{ ...@@ -7,24 +7,22 @@ export class BackstagePass extends Item{
} }
isBackStage() { isBackStage() {
return 'Backstage passes to a TAFKAL80ETC concert' return true
} }
updateQuality(item) { updateQuality(item) {
if (item.name === this.isBackStagePass()) { if (item.quality >= 50) {
if (item.quality < 50) { return
if (item.sell_in < 11) { }
if (item.quality < 50) { if (item.sell_in < 11) {
item.quality = item.quality + 1 if (item.quality < 50) {
} item.quality = item.quality + 1
} }
if (item.sell_in < 6) { }
if (item.quality < 50) { if (item.sell_in < 6) {
item.quality = item.quality + 1; if (item.quality < 50) {
} item.quality = item.quality + 1
}
}
return;
} }
}
} }
updateQualityAfterExpired(item) { updateQualityAfterExpired(item) {
if (item.quality < 50) { if (item.quality < 50) {
......
import { BackstagePass } from "./BackstagePass";
describe('backstagePassTest', () => {
it('foo', () => {
const agedBrie = new BackstagePass(15, 20);
const string = agedBrie.isBackStage();
expect(string).toEqual('Backstage passes to a TAFKAL80ETC concert');
})
})
\ No newline at end of file
...@@ -7,7 +7,13 @@ export class Sulfuras extends Item{ ...@@ -7,7 +7,13 @@ export class Sulfuras extends Item{
} }
isSulfuras() { isSulfuras() {
return 'Sulfuras, Hand of Ragnaros' return true
} }
updateQuality(item) {} updateQuality(item) {}
updateQualityAfterExpired(item) {
return
}
updateSellIn(item) {
return
}
} }
import { Sulfuras } from "./Sulfuras";
describe('SulfurasTest', () => {
it('foo', () => {
const agedBrie = new Sulfuras(-1, 80);
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'
import { AgedBrie } from './AgedBrie'
import { Sulfuras } from './Sulfuras'
import { BackstagePass } from './BackstagePass'
const items = [ const items = [
createNormalItem('+5 Dexterity Vest', 10, 20), new Item('+5 Dexterity Vest', 10, 20),
createAgedBrie(2, 0), new AgedBrie(2, 0),
createNormalItem('Elixir of the Mongoose', 5, 7), new Item('Elixir of the Mongoose', 5, 7),
createSulfuras(0, 80), new Sulfuras(0, 80),
createSulfuras(-1, 80), new Sulfuras(-1, 80),
createBackStagePass(15, 20), new BackstagePass(15, 20),
createBackStagePass(10, 49), new BackstagePass(10, 49),
createBackStagePass(5, 49), new BackstagePass(5, 49),
createBackStagePass(1, 20), new BackstagePass(1, 20),
// this conjured item does not work properly yet // this conjured item does not work properly yet
createNormalItem('Conjured Mana Cake', 3, 6), new Item('Conjured Mana Cake', 3, 6),
] ]
const app = new GildedRose(items) const app = new GildedRose(items)
......
...@@ -7,63 +7,7 @@ export class GildedRose { ...@@ -7,63 +7,7 @@ export class GildedRose {
update_quality() { update_quality() {
for (const item of this.items) { for (const item of this.items) {
item._updateItem(item); item.passOneDay()
} }
} }
// update_quality() {
// for (const item of this.items) {
// if (
// item.name != 'Aged Brie' &&
// item.name != 'Backstage passes to a TAFKAL80ETC concert'
// ) {
// if (item.quality > 0) {
// if (item.name != 'Sulfuras, Hand of Ragnaros') {
// item.quality = item.quality - 1
// }
// }
// } else {
// if (item.quality < 50) {
// item.quality = item.quality + 1
// if (
// item.name == 'Backstage passes to a TAFKAL80ETC concert'
// ) {
// if (item.sell_in < 11) {
// if (item.quality < 50) {
// item.quality = item.quality + 1
// }
// }
// if (item.sell_in < 6) {
// if (item.quality < 50) {
// item.quality = item.quality + 1
// }
// }
// }
// }
// }
// if (item.name != 'Sulfuras, Hand of Ragnaros') {
// item.sell_in = item.sell_in - 1
// }
// if (item.sell_in < 0) {
// if (item.name != 'Aged Brie') {
// if (
// item.name != 'Backstage passes to a TAFKAL80ETC concert'
// ) {
// if (item.quality > 0) {
// if (item.name != 'Sulfuras, Hand of Ragnaros') {
// item.quality = item.quality - 1
// }
// }
// } else {
// item.quality =
// item.quality - item.quality
// }
// } else {
// if (item.quality < 50) {
// item.quality = item.quality + 1
// }
// }
// }
// }
// }
} }
...@@ -9,79 +9,38 @@ export class Item { ...@@ -9,79 +9,38 @@ 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) { passOneDay() {
new BackStagePass( this.updateQuality()
'Backstage passes to a TAFKAL80ETC concert', this.updateSellIn()
sell_in, this.updateQualityAfterExpired()
quality
)
}
createSulfuras(sell_in, quality) {
new Sulfuras(sell_in, quality)
} }
createNormalItem(name, sell_in, quality) { updateQuality() {
new Item(name, sell_in, quality) if (this.quality > 0) {
} this.quality = this.quality - 1
createAgedBrie(sell_in, quality) {
new AgedBrie(sell_in, quality)
}
_updateItem(item) {
this.updateQuality(item)
this.updateSellIn(item)
this.updateQualityAfterExpired(item)
}
updateQuality(item) {
if (item.quality > 0) {
item.quality = item.quality - 1
} }
} }
updateSellIn(item) { updateSellIn() {
if (item.name === this.isSulfuras()) { this.decreaseSellInOneDay()
return
}
this.decreaseSellInOneDay(item)
} }
updateQualityAfterExpired(item) { updateQualityAfterExpired() {
if (item.sell_in >= 0) { if (this.sell_in >= 0) {
return return
} }
if (item.sell_in < 0) { if (this.quality <= 0) {
if (item.name !== this.isAgedBrie()) { this.quality = 0
if (item.quality > 0) { return
if (item.name !== this.isSulfuras()) {
item.quality = item.quality - 1
}
} else {
item.quality = item.quality - item.quality
}
}
} }
this.quality = this.quality - 1
} }
decreaseSellInOneDay(item) { decreaseSellInOneDay() {
item.sell_in = item.sell_in - 1 this.sell_in = this.sell_in - 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