gpt4 book ai didi

java - 观察者模式小鬼

转载 作者:行者123 更新时间:2023-12-01 15:04:39 25 4
gpt4 key购买 nike

我正在做观察者模式作为作业,但我没有通过测试。我已经堆积了好一阵子了。如果您可以看一下我的代码并给我一些建议,我哪里错了以及我没有按预期做的事情。干杯。这是代码。

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

最佳答案

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

  • BankManager 中,您永远不会更改投资组合,因此这就是您第一次失败的原因。
  • StockBroker 中,您将 portfolio 始终设置为 SB_INCREMENT,因此这可能是您第二次失败的原因。

因此,为了使其发挥作用,您必须在价格发生变化时调整投资组合,或者根据当前的实现调整expectedValue

关于java - 观察者模式小鬼,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13138964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com