Commit e3923306 by 李楚霏

task-8-if-conditions -- refactor updateQuality

function And refactor updateQualityAfterExpired Fun simply
parent 13c57e5a
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() { isAgedBrie() {
return 'Aged Brie'; return 'Aged Brie'
}
updateQuality(item) {
if (item.quality < 50) {
item.quality = item.quality + 1
}
} }
} }
import { Item } from "./item"; import { Item } from './item'
export class BackstagePass extends Item{ export class BackstagePass extends Item{
constructor(sell_in, quality) { constructor(sell_in, quality) {
super(sell_in, quality); super(sell_in, quality)
this.name = 'Backstage passes to a TAFKAL80ETC concert'; this.name = 'Backstage passes to a TAFKAL80ETC concert'
} }
isBackStage() { isBackStage() {
return 'Backstage passes to a TAFKAL80ETC concert'; return 'Backstage passes to a TAFKAL80ETC concert'
}
updateQuality(item) {
if (item.name === this.isBackStagePass()) {
if (item.quality < 50) {
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;
}
}
}
return;
}
}
updateQualityAfterExpired(item) {
if (item.quality < 50) {
item.quality = item.quality + 1
}
} }
} }
import { Item } from "./item"; import { Item } from './item'
export class Sulfuras extends Item{ export class Sulfuras extends Item{
constructor(sell_in, quality) { constructor(sell_in, quality) {
super(sell_in, quality); super(sell_in, quality)
this.name = 'Sulfuras, Hand of Ragnaros'; this.name = 'Sulfuras, Hand of Ragnaros'
} }
isSulfuras() { isSulfuras() {
return 'Sulfuras, Hand of Ragnaros'; return 'Sulfuras, Hand of Ragnaros'
} }
updateQuality(item) {}
} }
...@@ -10,15 +10,15 @@ export class Item { ...@@ -10,15 +10,15 @@ export class Item {
} }
isAgedBrie() { isAgedBrie() {
return 'Aged Brie'; return 'Aged Brie'
} }
isSulfuras() { isSulfuras() {
return 'Sulfuras, Hand of Ragnaros'; return 'Sulfuras, Hand of Ragnaros'
} }
isBackStagePass() { isBackStagePass() {
return 'Backstage passes to a TAFKAL80ETC concert'; return 'Backstage passes to a TAFKAL80ETC concert'
} }
toString() { toString() {
...@@ -26,84 +26,62 @@ export class Item { ...@@ -26,84 +26,62 @@ export class Item {
} }
createBackStagePass(sell_in, quality) { createBackStagePass(sell_in, quality) {
new BackStagePass('Backstage passes to a TAFKAL80ETC concert', sell_in, quality); new BackStagePass(
'Backstage passes to a TAFKAL80ETC concert',
sell_in,
quality
)
} }
createSulfuras(sell_in, quality) { createSulfuras(sell_in, quality) {
new Sulfuras(sell_in, quality); new Sulfuras(sell_in, quality)
} }
createNormalItem(name, sell_in, quality) { createNormalItem(name, sell_in, quality) {
new Item(name, sell_in, quality); new Item(name, sell_in, quality)
} }
createAgedBrie(sell_in, quality) { createAgedBrie(sell_in, quality) {
new AgedBrie(sell_in, quality); new AgedBrie(sell_in, quality)
} }
_updateItem(item) { _updateItem(item) {
this.updateQualityInOneDay(item); this.updateQuality(item)
this.updateSellInInOneDay(item); this.updateSellIn(item)
this.updateQualityAfterDevaluation(item); this.updateQualityAfterExpired(item)
} }
updateQualityInOneDay(item) { updateQuality(item) {
if (item.name != this.isAgedBrie() && item.name != this.isBackStagePass()) {
if (item.quality > 0) { if (item.quality > 0) {
if (item.name != this.isSulfuras()) {
item.quality = item.quality - 1 item.quality = item.quality - 1
} }
} }
} else {
if (item.quality < 50) {
this.updateQualityAfterExpired(item)
if (item.name == this.isBackStagePass()) {
if (item.sell_in < 11) {
if (item.quality < 50) {
this.updateQualityAfterExpired(item);
}
}
if (item.sell_in < 6) {
if (item.quality < 50) {
this.updateQualityAfterExpired(item);
}
}
}
}
}
}
updateSellInInOneDay(item) { updateSellIn(item) {
if (item.name != this.isSulfuras()) { if (item.name === this.isSulfuras()) {
this.decreaseSellIn(item) return
} }
this.decreaseSellInOneDay(item)
} }
updateQualityAfterDevaluation(item) { updateQualityAfterExpired(item) {
if (item.sell_in >= 0) {
return
}
if (item.sell_in < 0) { if (item.sell_in < 0) {
if (item.name != this.isAgedBrie()) { if (item.name !== this.isAgedBrie()) {
if (item.name != this.isBackStagePass()) {
if (item.quality > 0) { if (item.quality > 0) {
if (item.name != this.isSulfuras()) { if (item.name !== this.isSulfuras()) {
item.quality = item.quality - 1 item.quality = item.quality - 1
} }
} else { } else {
item.quality = item.quality - item.quality item.quality = item.quality - item.quality
} }
} else {
if (item.quality < 50) {
this.updateQualityAfterExpired(item);
}
}
} }
} }
} }
decreaseSellIn(item) { decreaseSellInOneDay(item) {
item.sell_in = item.sell_in - 1 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