Vwap { public Vwap addTrade(Trade t) { double volume = this.volume + t.quantity; double vwap = (this.volume * this.vwap + t.nominal) / volume; return new Vwap(t.code, vwap, volume); } Vwap(String code);  Vwap(String code, double vwap, double volume); Vwap addTrade(Trade t); static Vwap fromJson(String input); String toJson(); @Override String toString(); final String code; final double vwap; final double volume; }
@Test public void should_match_trade_volume_and_nominal_when_only_one_trade() { Vwap vwap = new Vwap("IBM", 0, 0); Vwap vwap2 = vwap.addTrade(new Trade("IBM", 10, 420)); assertThat(vwap2.volume).isEqualTo(10); assertThat(vwap2.vwap).isEqualTo(42); }
@Test public void should_compute_average_when_adding_a_trade() { Vwap vwap = new Vwap("IBM", 700, 10); Vwap vwap2 = vwap.addTrade(new Trade("IBM", 20, 15200)); assertThat(vwap2.volume).isEqualTo(30); assertThat(vwap2.vwap).isEqualTo(740); }