TDD练习之Gilded Rose镶金玫瑰

Gilded Rose镶金玫瑰练习

TDD练习题目网址.
项目原型及题目github.
CSDN下载.

题目

Gilded Rose 需求描述

欢迎来到镶金玫瑰(Gilded Rose)团队。如你所知,我们是主城(暴风城)中的一个小旅店,店主非常友好,名叫Allison。我们也售卖最好的物品。不幸的是,物品品质会随着销售期限的接近而不断下降。
我们有一个系统来更新库存信息。系统是由一个火车王(魔兽世界中导致团灭的猪队友)Leeroy所开发的,他已经不在这了。
你的任务是添加新功能,这样我们就可以售卖新的物品。

先介绍一下我们的系统:

- 每种物品都具备一个销售期限`SellIn`,表示我们要在多少天之前把物品卖出去
- 每种的物品都具备品质值`Quality`,表示物品的品质
- 每天结束时,系统会降低每种物品的这两个数值

很简单吧?这还有些更有意思的:

- 一旦销售期限过期,品质`Quality`会以双倍速度加速下降
- 物品的品质`Quality`永远不会为负值
- "Aged Brie"(陈年布利奶酪)的品质`Quality`会随着时间推移而提高
- 物品的品质`Quality`永远不会超过50
- 传奇物品"Sulfuras"(萨弗拉斯—炎魔拉格纳罗斯之手)永不过期,也不会降低品质`Quality`
- "Backstage passes"(后台通行证)与"Aged Brie"(陈年布利奶酪)类似,其品质`Quality`会随着时间推移而提高;当还剩10天或更少的时候,品质`Quality`每天提高2;当还剩5天或更少的时候,品质`Quality`每天提高3;但一旦过期,品质就会降为0

我们最近签约了一个召唤物品供应商。这需要对我们的系统进行升级:

- "Conjured"(召唤物品)的品质`Quality`下降速度比正常物品快一倍

请随意对**UpdateQuality()**函数进行修改和添加新代码,只要系统还能正常工作。然而,不要修改Item类或其属性,因为那属于角落里的地精,他会非常愤怒地爆你头,因为他不相信代码共享所有制(如果你愿意,你可以将UpdateQuality方法和Items属性改为静态的,我们会掩护你的)。

再次澄清,每种物品的品质不会超过50,然而"Sulfuras"(萨弗拉斯—炎魔拉格纳罗斯之手)是一个传奇物品,因此它的品质是80且永远不变。

项目原型代码

在main中有Item.java和GildedRose.java。
在test中有GildedRoseTest.java和TexttestFixture.java

下面展示一些 GildedRose.java

package com.gildedrose;

class GildedRose {
    Item[] items;

    public GildedRose(Item[] items) {
        this.items = items;
    }

    public void updateQuality() {
        for (int i = 0; i < items.length; i++) {
            if (!items[i].name.equals("Aged Brie")
                    && !items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
                if (items[i].quality > 0) {
                    if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
                        items[i].quality = items[i].quality - 1;
                    }
                }
            } else {
                if (items[i].quality < 50) {
                    items[i].quality = items[i].quality + 1;

                    if (items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
                        if (items[i].sellIn < 11) {
                            if (items[i].quality < 50) {
                                items[i].quality = items[i].quality + 1;
                            }
                        }

                        if (items[i].sellIn < 6) {
                            if (items[i].quality < 50) {
                                items[i].quality = items[i].quality + 1;
                            }
                        }
                    }
                }
            }

            if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
                items[i].sellIn = items[i].sellIn - 1;
            }

            if (items[i].sellIn < 0) {
                if (!items[i].name.equals("Aged Brie")) {
                    if (!items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
                        if (items[i].quality > 0) {
                            if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
                                items[i].quality = items[i].quality - 1;
                            }
                        }
                    } else {
                        items[i].quality = items[i].quality - items[i].quality;
                    }
                } else {
                    if (items[i].quality < 50) {
                        items[i].quality = items[i].quality + 1;
                    }
                }
            }
        }
    }
}

下面展示一些 Item.java

package com.gildedrose;

public class Item {

    public String name;

    public int sellIn;

    public int quality;

    public Item(String name, int sellIn, int quality) {
        this.name = name;
        this.sellIn = sellIn;
        this.quality = quality;
    }

   @Override
   public String toString() {
        return this.name + ", " + this.sellIn + ", " + this.quality;
    }
}

下面展示一些 GildedRoseTest.java

package com.gildedrose;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class GildedRoseTest {

    @Test
    void foo() {
        Item[] items = new Item[] { new Item("foo", 0, 0) };
        GildedRose app = new GildedRose(items);
        app.updateQuality();
        assertEquals("fixme", app.items[0].name);
    }

}

下面展示一些 TexttestFixture.java

package com.gildedrose;

public class TexttestFixture {
    public static void main(String[] args) {
        System.out.println("OMGHAI!");

        Item[] items = new Item[] {
                new Item("+5 Dexterity Vest", 10, 20), //
                new Item("Aged Brie", 2, 0), //
                new Item("Elixir of the Mongoose", 5, 7), //
                new Item("Sulfuras, Hand of Ragnaros", 0, 80), //
                new Item("Sulfuras, Hand of Ragnaros", -1, 80),
                new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
                new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49),
                new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49),
                // this conjured item does not work properly yet
                new Item("Conjured Mana Cake", 3, 6) };

        GildedRose app = new GildedRose(items);

        int days = 2;
        if (args.length > 0) {
            days = Integer.parseInt(args[0]) + 1;
        }

        for (int i = 0; i < days; i++) {
            System.out.println("-------- day " + i + " --------");
            System.out.println("name, sellIn, quality");
            for (Item item : items) {
                System.out.println(item);
            }
            System.out.println();
            app.updateQuality();
        }
    }

}

重构后的代码

完整项目请见下载链接。
重构后项目下载.
下面展示部分代码 GildedRose.java

package gildedrose;

public class GildedRose {
    Item[] items;

    public GildedRose(Item[] items) {
        this.items = items;
    }

    public void updateQuality() {
        for (Item item : items) {
            item.doUpdateQuality();
        }
    }

}

Item.java

package gildedrose;

import gildedrose.items.AgedBrieItem;
import gildedrose.items.BackstagePasses;
import gildedrose.items.Conjured;
import gildedrose.items.Sulfuras;

public class Item {

    public String name;

    public int sellIn;

    public int quality;

    protected Item(String name, int sellIn, int quality) {
        this.name = name;
        this.sellIn = sellIn;
        this.quality = quality;
    }

    public static Item createItem(String name, int sellIn, int quality) {
        switch (name){
            case "Aged Brie":
                return new AgedBrieItem(sellIn,quality);
            case "Backstage passes to a TAFKAL80ETC concert":
                return new BackstagePasses(sellIn,quality);
            case "Sulfuras, Hand of Ragnaros":
                return new Sulfuras(sellIn,quality);
            case "Conjured Mana Cake":
                return new Conjured(sellIn,quality);
            default:
        }
        return new Item(name, sellIn, quality);
    }

    @Override
    public String toString() {
        return this.name + ", " + this.sellIn + ", " + this.quality;
    }

    protected void doUpdateQuality() {
        if (quality > 0) {
            quality = quality - 1;
        }

        sellIn = sellIn - 1;

        if (sellIn < 0) {
            if (quality > 0) {
                quality = quality - 1;
            }
        }
    }
}

1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值