Skip to content

Commit

Permalink
Red Green Refactor of BoughShares - it groups shares of the same comp…
Browse files Browse the repository at this point in the history
…any together
  • Loading branch information
michaelszymczak committed Feb 9, 2017
1 parent 1b9ed8b commit 1f8e654
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 20 deletions.
@@ -1,33 +1,51 @@
package com.michaelszymczak.foo.episode01;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.summingInt;

public class BoughtShares extends Value {
private final List<CompanyShares> shares;

public BoughtShares(List<CompanyShares> shares) {
this.shares = ImmutableList.copyOf(shares);
private final Set<CompanyShares> shares;

public static class None extends BoughtShares
{
public None() {
super(ImmutableSet.of());
}
}

public BoughtShares(Set<CompanyShares> shares) {
this.shares = ImmutableSet.copyOf(shares);
}

public int totalPriceOn(StockMarket stockMarket)
{
return shares.stream().mapToInt(share -> share.worthOn(stockMarket)).sum();
}

public BoughtShares withMore(BoughtShares boughtShares)
public BoughtShares withMore(BoughtShares newlyBoughtShares)
{
return new BoughtShares(new ImmutableList.Builder<CompanyShares>()
.addAll(shares)
return new BoughtShares(groupedByCompany(existingWith(newlyBoughtShares)));
}

private ImmutableList<CompanyShares> existingWith(BoughtShares boughtShares) {
return new ImmutableList.Builder<CompanyShares>()
.addAll(this.shares)
.addAll(boughtShares.shares)
.build());
.build();
}

public static class None extends BoughtShares
{
public None() {
super(ImmutableList.of());
}
private static Set<CompanyShares> groupedByCompany(ImmutableList<CompanyShares> shares) {
return shares.stream()
.collect(groupingBy(CompanyShares::getCompany, summingInt(CompanyShares::getQuantity)))
.entrySet().stream()
.map(e -> new CompanyShares(e.getKey(), e.getValue()))
.collect(Collectors.toSet());
}
}
Expand Up @@ -13,4 +13,12 @@ public CompanyShares(Share.Ticker company, int quantity) {
public int worthOn(StockMarket stockMarket) {
return stockMarket.priceOf(company).value() * quantity;
}

public Share.Ticker getCompany() {
return company;
}

public int getQuantity() {
return quantity;
}
}
@@ -1,5 +1,7 @@
package com.michaelszymczak.foo.episode01;

import org.apache.commons.lang3.builder.ToStringStyle;

import static org.apache.commons.lang3.builder.EqualsBuilder.reflectionEquals;
import static org.apache.commons.lang3.builder.HashCodeBuilder.reflectionHashCode;
import static org.apache.commons.lang3.builder.ToStringBuilder.reflectionToString;
Expand All @@ -17,6 +19,6 @@ public int hashCode() {

@Override
public String toString() {
return reflectionToString(this);
return reflectionToString(this, ToStringStyle.SIMPLE_STYLE);
}
}
@@ -0,0 +1,42 @@
package com.michaelszymczak.foo.episode01

import spock.lang.Specification

class BoughtSharesTest extends Specification {
def "should calculate its total price on stock market"() {
given:
def stockMarket = StockMarket.trading([
Share.identifiedBy(Share.ticker("FOO")).andPricePerShare(5),
Share.identifiedBy(Share.ticker("BAR")).andPricePerShare(9)
] as Set)
def boughtShares = new BoughtShares([
new CompanyShares(Share.ticker("FOO"), 1),
new CompanyShares(Share.ticker("BAR"), 3),
] as Set)

expect:
boughtShares.totalPriceOn(stockMarket) == 32
}

def "should group shares of the same company together with previously bought ones"() {
given:
def initiallyBoughtShares = new BoughtShares([
new CompanyShares(Share.ticker("FOO"), 1),
new CompanyShares(Share.ticker("BAR"), 2),
new CompanyShares(Share.ticker("BAZ"), 3),
] as Set)
def laterBoughtShares = new BoughtShares([
new CompanyShares(Share.ticker("FOO"), 4),
new CompanyShares(Share.ticker("BAR"), 5),
new CompanyShares(Share.ticker("QUX"), 6),
] as Set)

expect:
initiallyBoughtShares.withMore(laterBoughtShares) == new BoughtShares([
new CompanyShares(Share.ticker("FOO"), 5),
new CompanyShares(Share.ticker("BAR"), 7),
new CompanyShares(Share.ticker("BAZ"), 3),
new CompanyShares(Share.ticker("QUX"), 6)
] as Set)
}
}
Expand Up @@ -49,11 +49,11 @@ class PortfolioShould extends Specification {
.afterAdding(Funds.ofValue(initialFunds))

when:
def portfolioWithSomeStocks = portfolio.after(new BoughtShares([new CompanyShares(someCompanyTicker(), quantity)]))
def portfolioWithSomeStocks = portfolio.after(new BoughtShares([new CompanyShares(someCompanyTicker(), quantity)] as Set))

then:
portfolioWithSomeStocks.availableFunds() == Funds.ofValue(expectedAvailableFundsAfterTransaction)
portfolioWithSomeStocks.shares() == new BoughtShares([new CompanyShares(someCompanyTicker(), quantity)])
portfolioWithSomeStocks.shares() == new BoughtShares([new CompanyShares(someCompanyTicker(), quantity)] as Set)

where:
initialFunds | pricePerShare | quantity | expectedAvailableFundsAfterTransaction
Expand All @@ -76,7 +76,7 @@ class PortfolioShould extends Specification {
def sharesToBuy = new BoughtShares([
new CompanyShares(Share.ticker("BAZ"), 3),
new CompanyShares(Share.ticker("FOO"), 5)
])
] as Set)


when:
Expand All @@ -96,16 +96,16 @@ class PortfolioShould extends Specification {
Share.identifiedBy(Share.ticker("BAR")).andPricePerShare(2)
] as Set))
.afterAdding(Funds.ofValue(101))
.after(new BoughtShares([new CompanyShares(Share.ticker("FOO"), 1)]))
.after(new BoughtShares([new CompanyShares(Share.ticker("FOO"), 1)] as Set))
when:
def portfolioWithSomeMoreStocks = portfolio.after(new BoughtShares([new CompanyShares(Share.ticker("BAR"), 3)]))
def portfolioWithSomeMoreStocks = portfolio.after(new BoughtShares([new CompanyShares(Share.ticker("BAR"), 3)] as Set))

then:
portfolioWithSomeMoreStocks.availableFunds() == Funds.ofValue(94)
portfolioWithSomeMoreStocks.shares() == new BoughtShares([
new CompanyShares(Share.ticker("FOO"), 1),
new CompanyShares(Share.ticker("BAR"), 3)
])
] as Set)
}

private static Share.Ticker someCompanyTicker() {
Expand Down

0 comments on commit 1f8e654

Please sign in to comment.