diff options
Diffstat (limited to 'ui-console/src/main')
8 files changed, 71 insertions, 31 deletions
diff --git a/ui-console/src/main/java/market/guess/ui/console/App.java b/ui-console/src/main/java/market/guess/ui/console/App.java index 330498c..a628a36 100644 --- a/ui-console/src/main/java/market/guess/ui/console/App.java +++ b/ui-console/src/main/java/market/guess/ui/console/App.java | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | package market.guess.ui.console; | 1 | package market.guess.ui.console; |
| 2 | 2 | ||
| 3 | import market.guess.api.CatalogContext; | ||
| 3 | import market.guess.api.GuessMarketContext; | 4 | import market.guess.api.GuessMarketContext; |
| 4 | import market.guess.service.LocalGuessMarketContext; | 5 | import market.guess.service.LocalGuessMarketContext; |
| 5 | import market.guess.service.TradingOperation; | 6 | import market.guess.service.catalog.LocalCatalogContext; |
| 6 | import market.guess.service.TradingOperationImpl; | ||
| 7 | import market.guess.service.infrastructure.MarketContext; | 7 | import market.guess.service.infrastructure.MarketContext; |
| 8 | import market.guess.service.infrastructure.mapper.v1.EventMapperV1; | 8 | import market.guess.service.infrastructure.mapper.v1.EventMapperV1; |
| 9 | import market.guess.service.infrastructure.provider.Loader; | 9 | import market.guess.service.infrastructure.provider.Loader; |
| @@ -31,10 +31,10 @@ public class App { | |||
| 31 | pico.addComponent(EventRepository.class, InMemoryEventRepository.class); | 31 | pico.addComponent(EventRepository.class, InMemoryEventRepository.class); |
| 32 | pico.addComponent(UserRepository.class, InMemoryUserRepository.class); | 32 | pico.addComponent(UserRepository.class, InMemoryUserRepository.class); |
| 33 | pico.addComponent(MarketContext.class); | 33 | pico.addComponent(MarketContext.class); |
| 34 | pico.addComponent(TradingOperation.class, TradingOperationImpl.class); | ||
| 35 | pico.addComponent(EventMapperV1.class); | 34 | pico.addComponent(EventMapperV1.class); |
| 36 | pico.addComponent(Loader.class, XMLLoaderV1.class); | 35 | pico.addComponent(Loader.class, XMLLoaderV1.class); |
| 37 | pico.addComponent(GuessMarketContext.class, LocalGuessMarketContext.class); | 36 | pico.addComponent(GuessMarketContext.class, LocalGuessMarketContext.class); |
| 37 | pico.addComponent(CatalogContext.class, LocalCatalogContext.class); | ||
| 38 | 38 | ||
| 39 | pico.addComponent(LoadFileMenuCommand.class); | 39 | pico.addComponent(LoadFileMenuCommand.class); |
| 40 | pico.addComponent(ListEventsMenuCommand.class); | 40 | pico.addComponent(ListEventsMenuCommand.class); |
diff --git a/ui-console/src/main/java/market/guess/ui/console/command/BuyMenuCommand.java b/ui-console/src/main/java/market/guess/ui/console/command/BuyMenuCommand.java index df40aa8..87da340 100644 --- a/ui-console/src/main/java/market/guess/ui/console/command/BuyMenuCommand.java +++ b/ui-console/src/main/java/market/guess/ui/console/command/BuyMenuCommand.java | |||
| @@ -1,8 +1,8 @@ | |||
| 1 | package market.guess.ui.console.command; | 1 | package market.guess.ui.console.command; |
| 2 | 2 | ||
| 3 | import market.guess.api.CatalogContext; | 3 | import market.guess.api.CatalogContext; |
| 4 | import market.guess.api.EventStatus; | ||
| 5 | import market.guess.api.GuessMarketContext; | 4 | import market.guess.api.GuessMarketContext; |
| 5 | import market.guess.model.event.EventStatus; | ||
| 6 | import market.guess.ui.console.io.InputProcessor; | 6 | import market.guess.ui.console.io.InputProcessor; |
| 7 | 7 | ||
| 8 | public final class BuyMenuCommand implements MenuCommand { | 8 | public final class BuyMenuCommand implements MenuCommand { |
| @@ -59,14 +59,16 @@ public final class BuyMenuCommand implements MenuCommand { | |||
| 59 | io.readSelect( | 59 | io.readSelect( |
| 60 | "Choose a market: ", | 60 | "Choose a market: ", |
| 61 | event.state().markets(), | 61 | event.state().markets(), |
| 62 | o -> "%s [value %d, %d shares helds]".formatted(o.name(), o.price(), o.volume())); | 62 | o -> "%s [value %s, %s shares helds]".formatted(o.name(), o.price(), o.volume())); |
| 63 | 63 | ||
| 64 | var amount = io.readInt("How many shares? ", 1, Integer.MAX_VALUE); | 64 | var amount = io.readInt("How many shares? ", 1, Integer.MAX_VALUE); |
| 65 | 65 | ||
| 66 | var receipt = context.buyShares("", event.summary().key(), market.key(), amount); | 66 | var buyResult = context.buyShares("Tester", event.summary().key(), market.key(), amount); |
| 67 | if (receipt.isSuccess()) return; | 67 | if (buyResult.isSuccess()) { |
| 68 | 68 | io.printReceipt(buyResult.getData()); | |
| 69 | return; | ||
| 70 | } | ||
| 69 | io.newLine(); | 71 | io.newLine(); |
| 70 | io.println("Purchase declined: %s".formatted(receipt.getMessage())); | 72 | io.println("Purchase declined: %s".formatted(buyResult.getMessage())); |
| 71 | } | 73 | } |
| 72 | } | 74 | } |
diff --git a/ui-console/src/main/java/market/guess/ui/console/command/EventDetailsMenuCommand.java b/ui-console/src/main/java/market/guess/ui/console/command/EventDetailsMenuCommand.java index ab60a1f..881acfa 100644 --- a/ui-console/src/main/java/market/guess/ui/console/command/EventDetailsMenuCommand.java +++ b/ui-console/src/main/java/market/guess/ui/console/command/EventDetailsMenuCommand.java | |||
| @@ -1,6 +1,18 @@ | |||
| 1 | package market.guess.ui.console.command; | 1 | package market.guess.ui.console.command; |
| 2 | 2 | ||
| 3 | import market.guess.api.CatalogContext; | ||
| 4 | import market.guess.model.event.EventStatus; | ||
| 5 | import market.guess.ui.console.io.InputProcessor; | ||
| 6 | |||
| 3 | public final class EventDetailsMenuCommand implements MenuCommand { | 7 | public final class EventDetailsMenuCommand implements MenuCommand { |
| 8 | private final CatalogContext catalog; | ||
| 9 | private final InputProcessor io; | ||
| 10 | |||
| 11 | public EventDetailsMenuCommand(CatalogContext catalog, InputProcessor io) { | ||
| 12 | super(); | ||
| 13 | this.catalog = catalog; | ||
| 14 | this.io = io; | ||
| 15 | } | ||
| 4 | 16 | ||
| 5 | @Override | 17 | @Override |
| 6 | public int getIndex() { | 18 | public int getIndex() { |
| @@ -14,7 +26,30 @@ public final class EventDetailsMenuCommand implements MenuCommand { | |||
| 14 | 26 | ||
| 15 | @Override | 27 | @Override |
| 16 | public void execute() { | 28 | public void execute() { |
| 17 | // TODO Auto-generated method stub | 29 | var result = catalog.getAllEvents(); |
| 18 | throw new UnsupportedOperationException("Unimplemented method 'execute'"); | 30 | |
| 31 | if (!result.isSuccess()) { | ||
| 32 | io.println("Failed to list events"); | ||
| 33 | return; | ||
| 34 | } | ||
| 35 | |||
| 36 | var events = result.getData(); | ||
| 37 | var active = events.stream().filter(event -> event.status() == EventStatus.ACTIVE).toList(); | ||
| 38 | |||
| 39 | if (active.isEmpty()) { | ||
| 40 | io.println("There are currently no active events to trade on."); | ||
| 41 | return; | ||
| 42 | } | ||
| 43 | |||
| 44 | var pick = io.readSelect("Pick an event: ", active, event -> "%s".formatted(event.name())); | ||
| 45 | |||
| 46 | var result2 = catalog.getEvent(pick.key()); | ||
| 47 | var event = result2.getData(); | ||
| 48 | if (event == null) { | ||
| 49 | io.println("That event is no longer available."); | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | |||
| 53 | io.printState(event.state()); | ||
| 19 | } | 54 | } |
| 20 | } | 55 | } |
diff --git a/ui-console/src/main/java/market/guess/ui/console/command/ListEventsMenuCommand.java b/ui-console/src/main/java/market/guess/ui/console/command/ListEventsMenuCommand.java index a97ab9f..755bf37 100644 --- a/ui-console/src/main/java/market/guess/ui/console/command/ListEventsMenuCommand.java +++ b/ui-console/src/main/java/market/guess/ui/console/command/ListEventsMenuCommand.java | |||
| @@ -29,6 +29,7 @@ public final class ListEventsMenuCommand implements MenuCommand { | |||
| 29 | var result = catalog.getAllEvents(); | 29 | var result = catalog.getAllEvents(); |
| 30 | 30 | ||
| 31 | if (!result.isSuccess()) { | 31 | if (!result.isSuccess()) { |
| 32 | io.println("Failed to list events due to %s.".formatted(result.getMessage())); | ||
| 32 | return; | 33 | return; |
| 33 | } | 34 | } |
| 34 | var events = result.getData(); | 35 | var events = result.getData(); |
diff --git a/ui-console/src/main/java/market/guess/ui/console/command/LoadFileMenuCommand.java b/ui-console/src/main/java/market/guess/ui/console/command/LoadFileMenuCommand.java index 065ac6e..46312e4 100644 --- a/ui-console/src/main/java/market/guess/ui/console/command/LoadFileMenuCommand.java +++ b/ui-console/src/main/java/market/guess/ui/console/command/LoadFileMenuCommand.java | |||
| @@ -25,8 +25,8 @@ public final class LoadFileMenuCommand implements MenuCommand { | |||
| 25 | 25 | ||
| 26 | @Override | 26 | @Override |
| 27 | public void execute() { | 27 | public void execute() { |
| 28 | // var path = io.readPath("Pleae enter the full file path to the XML file: "); | 28 | // var path = io.readPath("Pleae enter the full file path to the XML file: "); |
| 29 | // var result = context.loadEvents(Path.of(path)); | 29 | // var result = context.loadEvents(Path.of(path)); |
| 30 | var result = | 30 | var result = |
| 31 | catalog.loadEvents( | 31 | catalog.loadEvents( |
| 32 | Path.of("/home/hex-dev/projects/guess-market/test-data/ex-1/multiple.xml")); | 32 | Path.of("/home/hex-dev/projects/guess-market/test-data/ex-1/multiple.xml")); |
| @@ -38,11 +38,6 @@ public final class LoadFileMenuCommand implements MenuCommand { | |||
| 38 | return; | 38 | return; |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | io.println( | 41 | io.println("The file was not loaded, %s ".formatted(result.getMessage())); |
| 42 | "The file was not loaded. %d problem(s) were found: " | ||
| 43 | .formatted(result.getData().issues().size())); | ||
| 44 | for (var issue : result.getData().issues()) { | ||
| 45 | io.println(" - %s: %s".formatted(issue.location(), issue.humanizedMessage())); | ||
| 46 | } | ||
| 47 | } | 42 | } |
| 48 | } | 43 | } |
diff --git a/ui-console/src/main/java/market/guess/ui/console/command/LoadMarketStateMenuCommand.java b/ui-console/src/main/java/market/guess/ui/console/command/LoadMarketStateMenuCommand.java index defb402..26d00c1 100644 --- a/ui-console/src/main/java/market/guess/ui/console/command/LoadMarketStateMenuCommand.java +++ b/ui-console/src/main/java/market/guess/ui/console/command/LoadMarketStateMenuCommand.java | |||
| @@ -1,16 +1,16 @@ | |||
| 1 | package market.guess.ui.console.command; | 1 | package market.guess.ui.console.command; |
| 2 | 2 | ||
| 3 | import java.nio.file.Path; | 3 | import java.nio.file.Path; |
| 4 | import market.guess.api.GuessMarketContext; | 4 | import market.guess.api.CatalogContext; |
| 5 | import market.guess.ui.console.io.InputProcessor; | 5 | import market.guess.ui.console.io.InputProcessor; |
| 6 | 6 | ||
| 7 | public final class LoadMarketStateMenuCommand implements MenuCommand { | 7 | public final class LoadMarketStateMenuCommand implements MenuCommand { |
| 8 | private final GuessMarketContext context; | 8 | private final CatalogContext catalog; |
| 9 | private final InputProcessor io; | 9 | private final InputProcessor io; |
| 10 | 10 | ||
| 11 | public LoadMarketStateMenuCommand(GuessMarketContext context, InputProcessor io) { | 11 | public LoadMarketStateMenuCommand(CatalogContext catalog, InputProcessor io) { |
| 12 | super(); | 12 | super(); |
| 13 | this.context = context; | 13 | this.catalog = catalog; |
| 14 | this.io = io; | 14 | this.io = io; |
| 15 | } | 15 | } |
| 16 | 16 | ||
| @@ -27,7 +27,7 @@ public final class LoadMarketStateMenuCommand implements MenuCommand { | |||
| 27 | @Override | 27 | @Override |
| 28 | public void execute() { | 28 | public void execute() { |
| 29 | var path = io.readPath("Which state file to load? "); | 29 | var path = io.readPath("Which state file to load? "); |
| 30 | var result = context.restoreState(Path.of(path)); | 30 | var result = catalog.restoreState(Path.of(path)); |
| 31 | 31 | ||
| 32 | if (result.isSuccess()) { | 32 | if (result.isSuccess()) { |
| 33 | io.println("Restored %d event(s) from %s.".formatted(result.getData().eventsLoaded(), path)); | 33 | io.println("Restored %d event(s) from %s.".formatted(result.getData().eventsLoaded(), path)); |
diff --git a/ui-console/src/main/java/market/guess/ui/console/command/SaveMarketStateMenuCommand.java b/ui-console/src/main/java/market/guess/ui/console/command/SaveMarketStateMenuCommand.java index 27dfdde..77400d2 100644 --- a/ui-console/src/main/java/market/guess/ui/console/command/SaveMarketStateMenuCommand.java +++ b/ui-console/src/main/java/market/guess/ui/console/command/SaveMarketStateMenuCommand.java | |||
| @@ -1,16 +1,16 @@ | |||
| 1 | package market.guess.ui.console.command; | 1 | package market.guess.ui.console.command; |
| 2 | 2 | ||
| 3 | import java.nio.file.Path; | 3 | import java.nio.file.Path; |
| 4 | import market.guess.api.GuessMarketContext; | 4 | import market.guess.api.CatalogContext; |
| 5 | import market.guess.ui.console.io.InputProcessor; | 5 | import market.guess.ui.console.io.InputProcessor; |
| 6 | 6 | ||
| 7 | public final class SaveMarketStateMenuCommand implements MenuCommand { | 7 | public final class SaveMarketStateMenuCommand implements MenuCommand { |
| 8 | private final GuessMarketContext context; | 8 | private final CatalogContext catalog; |
| 9 | private final InputProcessor io; | 9 | private final InputProcessor io; |
| 10 | 10 | ||
| 11 | public SaveMarketStateMenuCommand(GuessMarketContext context, InputProcessor io) { | 11 | public SaveMarketStateMenuCommand(CatalogContext catalog, InputProcessor io) { |
| 12 | super(); | 12 | super(); |
| 13 | this.context = context; | 13 | this.catalog = catalog; |
| 14 | this.io = io; | 14 | this.io = io; |
| 15 | } | 15 | } |
| 16 | 16 | ||
| @@ -29,7 +29,7 @@ public final class SaveMarketStateMenuCommand implements MenuCommand { | |||
| 29 | var path = io.readPath("Save to: "); | 29 | var path = io.readPath("Save to: "); |
| 30 | 30 | ||
| 31 | try { | 31 | try { |
| 32 | context.saveState(Path.of(path)); | 32 | catalog.saveState(Path.of(path)); |
| 33 | io.println("Saved current state to %s.".formatted(path)); | 33 | io.println("Saved current state to %s.".formatted(path)); |
| 34 | } catch (Exception e) { | 34 | } catch (Exception e) { |
| 35 | io.println("Failed to save: %s".formatted(e.getMessage())); | 35 | io.println("Failed to save: %s".formatted(e.getMessage())); |
diff --git a/ui-console/src/main/java/market/guess/ui/console/io/InputProcessor.java b/ui-console/src/main/java/market/guess/ui/console/io/InputProcessor.java index 912ca25..d644a9b 100644 --- a/ui-console/src/main/java/market/guess/ui/console/io/InputProcessor.java +++ b/ui-console/src/main/java/market/guess/ui/console/io/InputProcessor.java | |||
| @@ -4,6 +4,7 @@ import java.util.List; | |||
| 4 | import java.util.Scanner; | 4 | import java.util.Scanner; |
| 5 | import java.util.function.Function; | 5 | import java.util.function.Function; |
| 6 | import market.guess.api.MarketStateDTO; | 6 | import market.guess.api.MarketStateDTO; |
| 7 | import market.guess.api.PurchaseReceiptDTO; | ||
| 7 | 8 | ||
| 8 | public final class InputProcessor { | 9 | public final class InputProcessor { |
| 9 | private final Scanner scanner = new Scanner(System.in); | 10 | private final Scanner scanner = new Scanner(System.in); |
| @@ -32,6 +33,8 @@ public final class InputProcessor { | |||
| 32 | .formatted( | 33 | .formatted( |
| 33 | padAround("Option", 18), space(10), padLeft("Value", 8), padRight("Shares", 8))); | 34 | padAround("Option", 18), space(10), padLeft("Value", 8), padRight("Shares", 8))); |
| 34 | 35 | ||
| 36 | println("-".repeat(46)); | ||
| 37 | |||
| 35 | for (var market : state.markets()) { | 38 | for (var market : state.markets()) { |
| 36 | println( | 39 | println( |
| 37 | "%s|%s%s|%s" | 40 | "%s|%s%s|%s" |
| @@ -43,8 +46,8 @@ public final class InputProcessor { | |||
| 43 | } | 46 | } |
| 44 | newLine(); | 47 | newLine(); |
| 45 | 48 | ||
| 46 | print("Event ledger balance: ".formatted(state.accountBalance())); | 49 | println("Event ledger balance: ".formatted(state.accountBalance())); |
| 47 | print("Commission collected: ".formatted(state.commissionCollected())); | 50 | println("Commission collected: ".formatted(state.commissionCollected())); |
| 48 | } | 51 | } |
| 49 | 52 | ||
| 50 | private static String space(int width) { | 53 | private static String space(int width) { |
| @@ -136,4 +139,8 @@ public final class InputProcessor { | |||
| 136 | private String nextLine() { | 139 | private String nextLine() { |
| 137 | return scanner.hasNextLine() ? scanner.nextLine() : null; | 140 | return scanner.hasNextLine() ? scanner.nextLine() : null; |
| 138 | } | 141 | } |
| 142 | |||
| 143 | public void printReceipt(PurchaseReceiptDTO receipt) { | ||
| 144 | println(receipt); | ||
| 145 | } | ||
| 139 | } | 146 | } |