2012-10-30 192 views
0

我正在做一個觀察員模式作爲功課,但我沒有通過測試。 我已經堆放了很長一段時間。如果你可以看看我的代碼,並給我一個建議,我錯了,我沒有按照想象的去做。乾杯。這是代碼。觀察者模式imp

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

你的哪個測試失敗?你有一些測試運行的輸出嗎? –

+0

由於代碼正在失敗,testChangePrice2:失敗;預計<500>,但爲stockroker和testChangePrice3:<0>:失敗;預計<0>但銀行經理<100>。預期的是印刷和實際應該是什麼。 –

回答

1

開關assertEquals(..., exptedValue);assertEquals(exptedValue, ...);。這不會改變您的失敗,但會遵循javadoc在Class Assert並修復報告的輸出。

  • BankManager,你永遠不會改變portfolio,所以這是你第一次失敗的原因。
  • StockBroker中,您將portfolio始終設置爲SB_INCREMENT,因此這可能是您第二次失敗的原因。

所以爲了達到此目的,您必須調整portfolio,如果價格更改或將expectedValue s調整爲您當前的實施。

+0

謝謝你,但正如你所說,它不改變faulures –

+0

我仍然不知道我需要如何改變投資組合,以通過測試... –

+0

我也不知道。由於這是一項家庭作業,因此必須有一些描述或要求,即在設定價格時投資組合應如何變化。沒有人能夠猜測價格變化與產生的投資組合之間的關係。 –