November 2024
Intermediate to advanced
300 pages
7h 12m
English
Madhu’s second batch of requirements for you is a batch of one: track the number of shares owned for a given symbol. A happy path test case:
| | @Test |
| | void returnsSharesGivenSymbol() { |
| | portfolio.purchase("AAPL", 42); |
| | |
| | assertEquals(42, portfolio.sharesOf("AAPL")); |
| | } |
The tests to any point in time represent the set of assumptions you make. Currently, you are assuming there will only ever be a single purchase. As such, you can track the shares purchased using a single discrete field:
| | public class Portfolio { |
| | private Set symbols = new HashSet<String>(); |
| » | private int shares; |
| | |
| | // ... |
Read now
Unlock full access