0

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

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());
        }
    }
}

这是界面

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);
    }

}

这是测试类

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);
    }
}
4

1 回答 1

1

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

  • BankManager中,你永远不会改变portfolio,所以这就是你第一次失败的原因。
  • StockBroker中,您portfolio始终设置为SB_INCREMENT,因此这可能是您第二次失败的原因。

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

于 2012-10-30T13:00:05.990 回答