Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Red Green Refactor of BoughShares - it groups shares of the same comp…
…any together
- Loading branch information
1 parent
1b9ed8b
commit 1f8e654
Showing
5 changed files
with
90 additions
and
20 deletions.
There are no files selected for viewing
44 changes: 31 additions & 13 deletions
44
tdd/src/main/java/com/michaelszymczak/foo/episode01/BoughtShares.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
tdd/src/test/groovy/com/michaelszymczak/foo/episode01/BoughtSharesTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters