2012-10-30 5 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 : Failed; <500>이되었지만 stockBroker 및 testChangePrice3에 대해 <0>이었습니다. 실패했습니다. <0>이 예상되었지만 bankManager의 경우 <100>이었습니다. 예상되는 것은 인쇄되고 실제는 예상되는 것입니다. –

답변

1

테스트 클래스를 assertEquals(exptedValue, ...);로 전환 assertEquals(..., exptedValue);입니다. 이것은 실패를 바꾸지 않지만, javadoc을 따라 Class Assert으로보고 된 출력을 수정합니다.

  • BankManager에는 절대로 portfolio을 변경하지 않으므로이 오류가 첫 번째 오류의 원인입니다.
  • StockBroker에서 portfolio은 항상 SB_INCREMENT으로 설정되므로 두 번째 실패의 원인 일 수 있습니다.

따라서 작동하려면 가격이 변경된 경우 portfolio을 조정하거나 expectedValue을 현재 구현에 맞게 조정해야합니다.

+0

그 점에 대해 고마워요.하지만 당신이 말했듯이 그것들은 돌발 물을 바꾸지 않습니다. –

+0

나는 시험을 통과하기 위해 포트폴리오를 어떻게 바꿀 필요가 있는지 아직도 모르겠다. ... –

+0

나도 몰라. 이것이 숙제이기 때문에 가격을 정할 때 포트폴리오가 어떻게 변화해야하는지에 대한 설명이나 요구 사항이 있어야합니다. 아무도 변화하는 가격과 결과 포트폴리오 간의 관계를 짐작할 수 없습니다. –