diff options
| author | Kostya <mail@sartin.in> | 2026-08-14 11:02:33 +0000 |
|---|---|---|
| committer | Kostya <mail@sartin.in> | 2026-08-14 11:02:33 +0000 |
| commit | 98ebc2b0f6fdde1f7502a35ebe067fb95e5ee336 (patch) | |
| tree | 9704fcc7cc1d17a3743928c0219728887a65862b /service/src/main/java/market/guess | |
| parent | db4317bbe0ff5709fa54ade5bec112186ca9aca3 (diff) | |
| download | guess-market-98ebc2b0f6fdde1f7502a35ebe067fb95e5ee336.tar.gz guess-market-98ebc2b0f6fdde1f7502a35ebe067fb95e5ee336.tar.xz guess-market-98ebc2b0f6fdde1f7502a35ebe067fb95e5ee336.zip | |
Wire buyShares and menu commands, replace factory interfaces with DI
Implement LocalGuessMarketContext.buyShares/loadEvents via a new
TradingOperation and Loader, backed by in-memory repositories. Replace
the ConsoleInputProviderFactory/ConsoleOutputProviderFactory pattern
with picocontainer-injected providers, and drive the menu from a list
of MenuCommand implementations instead of a hardcoded printout.
Diffstat (limited to 'service/src/main/java/market/guess')
8 files changed, 116 insertions, 5 deletions
diff --git a/service/src/main/java/market/guess/service/LocalGuessMarketContext.java b/service/src/main/java/market/guess/service/LocalGuessMarketContext.java index 67e263f..16ee1b8 100644 --- a/service/src/main/java/market/guess/service/LocalGuessMarketContext.java +++ b/service/src/main/java/market/guess/service/LocalGuessMarketContext.java | |||
| @@ -10,19 +10,33 @@ import market.guess.api.LoadResultDTO; | |||
| 10 | import market.guess.api.PurchaseReceiptDTO; | 10 | import market.guess.api.PurchaseReceiptDTO; |
| 11 | import market.guess.exception.GuessMarketException; | 11 | import market.guess.exception.GuessMarketException; |
| 12 | import market.guess.service.domain.Event; | 12 | import market.guess.service.domain.Event; |
| 13 | import market.guess.service.infrastructure.provider.Loader; | ||
| 13 | import market.guess.service.repository.EventRepository; | 14 | import market.guess.service.repository.EventRepository; |
| 15 | import market.guess.service.repository.UserRepository; | ||
| 14 | 16 | ||
| 15 | public final class LocalGuessMarketContext implements GuessMarketContext { | 17 | public final class LocalGuessMarketContext implements GuessMarketContext { |
| 16 | private final EventRepository eventRepository; | 18 | private final EventRepository eventRepository; |
| 19 | private final UserRepository userRepository; | ||
| 17 | 20 | ||
| 18 | public LocalGuessMarketContext(EventRepository eventRepository) { | 21 | private final TradingOperation tradingOperation; |
| 22 | |||
| 23 | private final Loader provider; | ||
| 24 | |||
| 25 | public LocalGuessMarketContext( | ||
| 26 | Loader provider, | ||
| 27 | EventRepository eventRepository, | ||
| 28 | UserRepository userRepository, | ||
| 29 | TradingOperation tradingOperation) { | ||
| 19 | super(); | 30 | super(); |
| 31 | this.provider = provider; | ||
| 20 | this.eventRepository = eventRepository; | 32 | this.eventRepository = eventRepository; |
| 33 | this.userRepository = userRepository; | ||
| 34 | this.tradingOperation = tradingOperation; | ||
| 21 | } | 35 | } |
| 22 | 36 | ||
| 23 | @Override | 37 | @Override |
| 24 | public LoadResultDTO loadEvents(Path path) { | 38 | public LoadResultDTO loadEvents(Path path) { |
| 25 | throw new UnsupportedOperationException("Unimplemented method 'loadEvents'"); | 39 | return provider.load(path); |
| 26 | } | 40 | } |
| 27 | 41 | ||
| 28 | @Override | 42 | @Override |
| @@ -42,15 +56,19 @@ public final class LocalGuessMarketContext implements GuessMarketContext { | |||
| 42 | 56 | ||
| 43 | @Override | 57 | @Override |
| 44 | public PurchaseReceiptDTO buyShares( | 58 | public PurchaseReceiptDTO buyShares( |
| 45 | String userName, String eventKey, String optionKey, long quantity) | 59 | String userName, String eventKey, String optionKey, int quantity) |
| 46 | throws GuessMarketException { | 60 | throws GuessMarketException { |
| 47 | throw new UnsupportedOperationException("Unimplemented method 'buyShares'"); | 61 | var event = eventRepository.getEvent(eventKey); |
| 62 | var option = event.getOption(optionKey); | ||
| 63 | var user = userRepository.getUser(userName); | ||
| 64 | var trade = tradingOperation.buy(event, user, option, quantity); | ||
| 65 | return new PurchaseReceiptDTO(); | ||
| 48 | } | 66 | } |
| 49 | 67 | ||
| 50 | @Override | 68 | @Override |
| 51 | public EventDetailDTO closeEvent(String userName, String eventKey, String winningOptionKey) | 69 | public EventDetailDTO closeEvent(String userName, String eventKey, String winningOptionKey) |
| 52 | throws GuessMarketException { | 70 | throws GuessMarketException { |
| 53 | throw new UnsupportedOperationException("Unimplemented method 'closeEvent'"); | 71 | throw new UnsupportedOperationException("Unimplemented method 'account'"); |
| 54 | } | 72 | } |
| 55 | 73 | ||
| 56 | @Override | 74 | @Override |
diff --git a/service/src/main/java/market/guess/service/TradingOperation.java b/service/src/main/java/market/guess/service/TradingOperation.java new file mode 100644 index 0000000..c8153c2 --- /dev/null +++ b/service/src/main/java/market/guess/service/TradingOperation.java | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | package market.guess.service; | ||
| 2 | |||
| 3 | import market.guess.service.domain.Event; | ||
| 4 | import market.guess.service.domain.Option; | ||
| 5 | import market.guess.service.domain.Settlement; | ||
| 6 | import market.guess.service.domain.Trade; | ||
| 7 | import market.guess.service.domain.User; | ||
| 8 | |||
| 9 | public interface TradingOperation { | ||
| 10 | Trade buy(Event event, User user, Option Option, int quantity); | ||
| 11 | |||
| 12 | Settlement settle(Event event, String winningOption); | ||
| 13 | } | ||
diff --git a/service/src/main/java/market/guess/service/TradingOperationImpl.java b/service/src/main/java/market/guess/service/TradingOperationImpl.java new file mode 100644 index 0000000..f6c9aa1 --- /dev/null +++ b/service/src/main/java/market/guess/service/TradingOperationImpl.java | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | package market.guess.service; | ||
| 2 | |||
| 3 | import market.guess.service.domain.Event; | ||
| 4 | import market.guess.service.domain.Option; | ||
| 5 | import market.guess.service.domain.Settlement; | ||
| 6 | import market.guess.service.domain.Trade; | ||
| 7 | import market.guess.service.domain.User; | ||
| 8 | |||
| 9 | public final class TradingOperationImpl implements TradingOperation { | ||
| 10 | |||
| 11 | @Override | ||
| 12 | public Trade buy(Event event, User user, Option Option, int quantity) { | ||
| 13 | // TODO Auto-generated method stub | ||
| 14 | throw new UnsupportedOperationException("Unimplemented method 'buy'"); | ||
| 15 | } | ||
| 16 | |||
| 17 | @Override | ||
| 18 | public Settlement settle(Event event, String winningOption) { | ||
| 19 | // TODO Auto-generated method stub | ||
| 20 | throw new UnsupportedOperationException("Unimplemented method 'settle'"); | ||
| 21 | } | ||
| 22 | } | ||
diff --git a/service/src/main/java/market/guess/service/domain/Event.java b/service/src/main/java/market/guess/service/domain/Event.java index 2d7ac39..cef0840 100644 --- a/service/src/main/java/market/guess/service/domain/Event.java +++ b/service/src/main/java/market/guess/service/domain/Event.java | |||
| @@ -165,4 +165,11 @@ public final class Event { | |||
| 165 | getOptionsNames(), | 165 | getOptionsNames(), |
| 166 | account.getBalance().toPlainString()); | 166 | account.getBalance().toPlainString()); |
| 167 | } | 167 | } |
| 168 | |||
| 169 | public Option getOption(String optionKey) { | ||
| 170 | return options.stream() | ||
| 171 | .filter(option -> option.getKey().equalsIgnoreCase(optionKey)) | ||
| 172 | .findFirst() | ||
| 173 | .orElseThrow(() -> new IllegalArgumentException("No such option.")); | ||
| 174 | } | ||
| 168 | } | 175 | } |
diff --git a/service/src/main/java/market/guess/service/domain/Settlement.java b/service/src/main/java/market/guess/service/domain/Settlement.java new file mode 100644 index 0000000..cfc61f5 --- /dev/null +++ b/service/src/main/java/market/guess/service/domain/Settlement.java | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | package market.guess.service.domain; | ||
| 2 | |||
| 3 | import java.math.BigDecimal; | ||
| 4 | |||
| 5 | public record Settlement( | ||
| 6 | String winningOption, BigDecimal commission, BigDecimal paidOutToHolders) {} | ||
diff --git a/service/src/main/java/market/guess/service/infrastructure/provider/XMLLoader.java b/service/src/main/java/market/guess/service/infrastructure/provider/XMLLoader.java new file mode 100644 index 0000000..443cd87 --- /dev/null +++ b/service/src/main/java/market/guess/service/infrastructure/provider/XMLLoader.java | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | package market.guess.service.infrastructure.provider; | ||
| 2 | |||
| 3 | import java.nio.file.Path; | ||
| 4 | import market.guess.api.LoadResultDTO; | ||
| 5 | |||
| 6 | public final class XMLLoader implements Loader { | ||
| 7 | |||
| 8 | @Override | ||
| 9 | public LoadResultDTO load(Path path) { | ||
| 10 | // TODO Auto-generated method stub | ||
| 11 | throw new UnsupportedOperationException("Unimplemented method 'load'"); | ||
| 12 | } | ||
| 13 | } | ||
diff --git a/service/src/main/java/market/guess/service/repository/InMemoryEventRepository.java b/service/src/main/java/market/guess/service/repository/InMemoryEventRepository.java new file mode 100644 index 0000000..dad1055 --- /dev/null +++ b/service/src/main/java/market/guess/service/repository/InMemoryEventRepository.java | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | package market.guess.service.repository; | ||
| 2 | |||
| 3 | import java.util.List; | ||
| 4 | import market.guess.service.domain.Event; | ||
| 5 | |||
| 6 | public final class InMemoryEventRepository implements EventRepository { | ||
| 7 | |||
| 8 | @Override | ||
| 9 | public Event getEvent(String id) { | ||
| 10 | // TODO Auto-generated method stub | ||
| 11 | throw new UnsupportedOperationException("Unimplemented method 'getEvent'"); | ||
| 12 | } | ||
| 13 | |||
| 14 | @Override | ||
| 15 | public List<Event> getAllEvents() { | ||
| 16 | // TODO Auto-generated method stub | ||
| 17 | throw new UnsupportedOperationException("Unimplemented method 'getAllEvents'"); | ||
| 18 | } | ||
| 19 | } | ||
diff --git a/service/src/main/java/market/guess/service/repository/InMemoryUserRepository.java b/service/src/main/java/market/guess/service/repository/InMemoryUserRepository.java new file mode 100644 index 0000000..e71c94c --- /dev/null +++ b/service/src/main/java/market/guess/service/repository/InMemoryUserRepository.java | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | package market.guess.service.repository; | ||
| 2 | |||
| 3 | |||
| 4 | import market.guess.service.domain.User; | ||
| 5 | |||
| 6 | public final class InMemoryUserRepository implements UserRepository { | ||
| 7 | |||
| 8 | @Override | ||
| 9 | public User getUser(String name) { | ||
| 10 | // TODO Auto-generated method stub | ||
| 11 | throw new UnsupportedOperationException("Unimplemented method 'getUser'"); | ||
| 12 | } | ||
| 13 | } | ||