首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >观察者模式输入

观察者模式输入
EN

Stack Overflow用户
提问于 2012-10-30 20:26:46
回答 1查看 179关注 0票数 0

我正在做一个观察者模式作为家庭作业,但我没有通过测试。我已经被堆积了很长时间了。如果你可以看一下我的代码,给我一个建议,我哪里错了,我没有按照预期做什么。干杯。下面是代码。

代码语言:javascript
复制
public class Share 
{
    /**@param poundsAndPences stores the monetary unit for the share.
     * @unique a  instance of the Share class responsible for the observer pattern*/
    private double poundsAndPences = 1.00;
    ArrayList<ShareWatcher> list = new ArrayList<ShareWatcher>();

    public boolean addShareWatcher(ShareWatcher sw)
    {
            list.add(sw); 
            if (list.contains(sw))
            {
                return true;
            }
        return false;
    }

    public boolean removeShareWatcher(ShareWatcher sw)
    {
        if(list.contains(sw))
        {
             list.remove(sw);
            return true;
        }
        else
        {
          return false;  
        } 


    }

    /** Share(double poundsAndPences) private constructor.
     * 1-st pre-requisite for the multiple pattern 
     * takes and double value and initialized the local
     * variable with the one that have been passed
     * @param poundsAndPences sets the local variable with the current value*/
     Share()
    {
//        this.poundsAndPences = poundsAndPences;
//          changeState();
//        System.out.println("test: " + list);
    }

    /**getPoundsAndPences() is a getter method to. 
     * @return the poundsAndPences
     */
    public double getPrice() 
    {
        return poundsAndPences;
    }

    /**setPoundsAndPences(int poundsAndPences) is a mutator method.
     * @param poundsAndPences set the poundsAndPences passed to the
     * methods to the local ones 
     */
    public void setPrice(double poundsAndPences) 
    {
        this.poundsAndPences = poundsAndPences;
        changeState();
        updateShareWatcher();
    }

    public void changeState()
    {
        poundsAndPences = getPrice() ;
    }

    public void updateShareWatcher()
    {
//       System.out.println("list: " + list);
        int counter = 0;
        for(ShareWatcher sw: list)
        {
//            System.out.println("list test: "+ counter++  + "  %%% " + sw);
            sw.updatePrice(poundsAndPences);
//            System.out.println(list.toString());
        }
    }
}

这就是界面

代码语言:javascript
复制
public interface ShareWatcher 
{
    void updatePrice(double price);

}

public class BankManager implements ShareWatcher
{
     int portfolio = 0;
    /**
     * Buy value for bank manager.
     */
     static double BM_BUY = 1.00;

    /**
     * Sell value for bank manager.
     */
     static double BM_SELL = 4.00;

    /**
     * Increment value for bank manager.
     */
     static int BM_INCREMENT = 100;


    public BankManager(double BM_BUY, double BM_SELL, int BM_INCREMENT)
    {
        this.BM_BUY = BM_BUY;
        this.BM_SELL = BM_SELL;
        this.BM_INCREMENT = BM_INCREMENT;

        portfolio = 0;

//        updatePrice(portfolio);
    }


    public  int getPortfolio()
    {
        return portfolio;
    }

    public  void setPortfolio(int portfolio)
    {
        this.portfolio = portfolio;
//        updatePrice(portfolio);
    }

    public void  updatePrice(double price)
    {
        if(price < 1.00)
        {
            BM_BUY = price;
            System.out.println("BankManager buy shares at: " + BM_BUY);

        }

        if(price > 4.00)
        {
            BM_SELL = price;
            System.out.println("BankManager sell shares at:" + BM_SELL);
        }
//        portfolio = price;
//        System.out.println("Update BankManager");
//        System.out.println("New value is: " + portfolio);
    }
}


public class StockBroker implements ShareWatcher
{
      int portfolio = 1;
     /**
     * Buy value for stock broker.
     */
     static double SB_BUY = 2.00;

    /**
     * Sell value for stock broker.
     */
     static double SB_SELL = 3.00;

    /**
     * Increment value for stock broker.
     */
     static int SB_INCREMENT = 500;

    StockBroker(double SB_BUY, double SB_SELL, int SB_INCREMENT)
    {
//        this.price = portfolio;
//        updatePrice(portfolio);
        this.SB_BUY = SB_BUY;
        this.SB_SELL = SB_SELL;
        this.SB_INCREMENT = SB_INCREMENT;
        portfolio = 0;

//        updatePrice(portfolio);
    }
    public  int getPortfolio() 
    {
        return portfolio ;
    }

    public  void setPortfolio(int portfolio) 
    {
        this.portfolio = portfolio;
    }

    public void updatePrice(double price)
    {
//        StockBroker sb = new StockBroker(SB_BUY, SB_SELL, SB_INCREMENT);

        if(price < 2.00)
        {
           SB_BUY = price;
            System.out.println("StockBroker buy shares at: " + SB_BUY);
        }

        if(price > 3.00)
        {
            SB_SELL= price;
            System.out.println("StockBroker sell shares at:" + SB_SELL);
        }
        portfolio = SB_INCREMENT;
//        System.out.println("Update StockBroker");
//        System.out.println("New value is: " + portfolio);
    }

}

这是测试类

代码语言:javascript
复制
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Ignore;

/** A set of unit tests that check the solution to the SILVER task.
 *
 */
public class ShareTest {

/**
     * Arbitrary stock price value for testing. 
     */
    final static double PRICE1 = 4.01;
    /**
     * Arbitrary stock price value for testing. 
     */
    final static double PRICE2 = 0.99;
    /**
     * Arbitrary stock price value for testing. 
     */
    final static double PRICE3 = 2.12;
    /**
     * Arbitrary stock price value for testing. 
     */
    final static double PRICE4 = 1.89;
    /**
     * Arbitrary stock price value for testing. 
     */
    final static double PRICE5 = 1.83;
    /**
     * Arbitrary stock price value for testing. 
     */
    final static double PRICE6 = 2.78;
    /**
     * Arbitrary stock price value for testing. 
     */
    final static double PRICE7 = 14.12;
    /**
     * Arbitrary stock price value for testing. 
     */
    final static double PRICE8 = 6.99;

    /**
     * Buy value for bank manager.
     */
    final static double BM_BUY = 1.00;

    /**
     * Sell value for bank manager.
     */
    final static double BM_SELL = 4.00;

    /**
     * Increment value for bank manager.
     */
    final static int BM_INCREMENT = 100;

    /**
     * Buy value for stock broker.
     */
    final static double SB_BUY = 2.00;

    /**
     * Sell value for stock broker.
     */
    final static double SB_SELL = 3.00;

    /**
     * Increment value for stock broker.
     */
    final static int SB_INCREMENT = 500;
  public ShareTest(){
  }

  @Test
    public void testChangePrice1() {
        final Share share = new Share();
        final BankManager bankManager = new BankManager(BM_BUY, BM_SELL, BM_INCREMENT);
        final StockBroker stockBroker = new StockBroker(SB_BUY, SB_SELL, SB_INCREMENT);
        assertTrue(share.addShareWatcher(bankManager));
        assertTrue(share.addShareWatcher(stockBroker));
        share.setPrice(PRICE5);
        final int expectedValue1 = 0;
//        System.out.println("*****BankManager " + bankManager.getPortfolio());
        assertEquals(bankManager.getPortfolio(), expectedValue1);
        final int expectedValue2 = 500;
        System.out.println("*****StockBroker " + stockBroker.getPortfolio());
        assertEquals(stockBroker.getPortfolio(), expectedValue2);
    }

    /**
     * Test of changePrice method, of class Share. A similar test to above. More 
     * changes this time.
     */
//    @Ignore
    @Test
    public void testChangePrice2() {
        final Share share = new Share();
        final BankManager bankManager = new BankManager(BM_BUY, BM_SELL, BM_INCREMENT);
        final StockBroker stockBroker = new StockBroker(SB_BUY, SB_SELL, SB_INCREMENT);
        assertTrue(share.addShareWatcher(bankManager));
        assertTrue(share.addShareWatcher(stockBroker));
        share.setPrice(PRICE3);
        share.setPrice(PRICE6);
        share.setPrice(PRICE8);
        final int expectedValue1 = 0;
        assertEquals(bankManager.getPortfolio(), expectedValue1);
        final int expectedValue2 = 0;
        assertEquals(stockBroker.getPortfolio(), expectedValue2);
    }


    /**
     * Test of changePrice method, of class Share. A similar test to above. More
     * changes this time.
     */
//    @Ignore
    @Test
    public void testChangePrice3() {
        final Share share = new Share();
        final BankManager bankManager = new BankManager(BM_BUY, BM_SELL, BM_INCREMENT);
        final StockBroker stockBroker = new StockBroker(SB_BUY, SB_SELL, SB_INCREMENT);
        assertTrue(share.addShareWatcher(bankManager));
        assertTrue(share.addShareWatcher(stockBroker));
        share.setPrice(PRICE1);
        share.setPrice(PRICE4);
        share.setPrice(PRICE7);
        share.setPrice(PRICE2);
        final int expectedValue1 = 100;
        assertEquals(bankManager.getPortfolio(), expectedValue1);
        final int expectedValue2 = 500;
        assertEquals(stockBroker.getPortfolio(), expectedValue2);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2012-10-30 21:00:06

assertEquals(..., exptedValue);切换到assertEquals(exptedValue, ...);。这不会改变您的失败,但会遵循Class Assert上的javadoc并修复报告的输出。

  • BankManager中,您从不更改portfolio,因此这是您第一次失败的原因。
  • StockBroker中,您将portfolio always设置为SB_INCREMENT,因此这可能是第二次失败的原因。

因此,为了使其起作用,如果价格发生变化,您必须调整portfolio,或者将expectedValue调整为您当前的实现。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13138964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档