diff options
| author | Kostya <mail@sartin.in> | 2026-08-14 12:10:52 +0000 |
|---|---|---|
| committer | Kostya <mail@sartin.in> | 2026-08-14 12:10:52 +0000 |
| commit | f62600050e22532a3fe297b86eb22ca72d786698 (patch) | |
| tree | d8e93c3ed1cbc6f871589d318f698d42abe90a1b /ui-console/src/main/java/market/guess/ui | |
| parent | 98ebc2b0f6fdde1f7502a35ebe067fb95e5ee336 (diff) | |
| download | guess-market-f62600050e22532a3fe297b86eb22ca72d786698.tar.gz guess-market-f62600050e22532a3fe297b86eb22ca72d786698.tar.xz guess-market-f62600050e22532a3fe297b86eb22ca72d786698.zip | |
Move IO providers into io package, add InputProcessor, implement menu loop and file load command
Wires InputProcessor into the console menu's selection loop and the
load-file command's prompt handling, replacing the unimplemented stub.
Diffstat (limited to 'ui-console/src/main/java/market/guess/ui')
8 files changed, 115 insertions, 23 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 ccffc06..148e928 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 | |||
| @@ -11,6 +11,11 @@ import market.guess.service.repository.InMemoryEventRepository; | |||
| 11 | import market.guess.service.repository.InMemoryUserRepository; | 11 | import market.guess.service.repository.InMemoryUserRepository; |
| 12 | import market.guess.service.repository.UserRepository; | 12 | import market.guess.service.repository.UserRepository; |
| 13 | import market.guess.ui.console.command.LoadFileMenuCommand; | 13 | import market.guess.ui.console.command.LoadFileMenuCommand; |
| 14 | import market.guess.ui.console.io.ConsoleInputProvider; | ||
| 15 | import market.guess.ui.console.io.ConsoleOutputProvider; | ||
| 16 | import market.guess.ui.console.io.InputProcessor; | ||
| 17 | import market.guess.ui.console.io.InputProvider; | ||
| 18 | import market.guess.ui.console.io.OutputProvider; | ||
| 14 | import org.picocontainer.DefaultPicoContainer; | 19 | import org.picocontainer.DefaultPicoContainer; |
| 15 | import org.picocontainer.behaviors.Caching; | 20 | import org.picocontainer.behaviors.Caching; |
| 16 | 21 | ||
| @@ -29,6 +34,7 @@ public class App { | |||
| 29 | 34 | ||
| 30 | pico.addComponent(InputProvider.class, ConsoleInputProvider.class); | 35 | pico.addComponent(InputProvider.class, ConsoleInputProvider.class); |
| 31 | pico.addComponent(OutputProvider.class, ConsoleOutputProvider.class); | 36 | pico.addComponent(OutputProvider.class, ConsoleOutputProvider.class); |
| 37 | pico.addComponent(InputProcessor.class); | ||
| 32 | pico.addComponent(Menu.class); | 38 | pico.addComponent(Menu.class); |
| 33 | 39 | ||
| 34 | pico.start(); | 40 | pico.start(); |
diff --git a/ui-console/src/main/java/market/guess/ui/console/Menu.java b/ui-console/src/main/java/market/guess/ui/console/Menu.java index 1f3f2be..0521e5f 100644 --- a/ui-console/src/main/java/market/guess/ui/console/Menu.java +++ b/ui-console/src/main/java/market/guess/ui/console/Menu.java | |||
| @@ -2,23 +2,39 @@ package market.guess.ui.console; | |||
| 2 | 2 | ||
| 3 | import java.util.List; | 3 | import java.util.List; |
| 4 | import market.guess.ui.console.command.MenuCommand; | 4 | import market.guess.ui.console.command.MenuCommand; |
| 5 | import market.guess.ui.console.io.InputProcessor; | ||
| 6 | import market.guess.ui.console.io.OutputProvider; | ||
| 5 | import org.picocontainer.Startable; | 7 | import org.picocontainer.Startable; |
| 6 | 8 | ||
| 7 | public final class Menu implements Startable { | 9 | public final class Menu implements Startable { |
| 8 | private final OutputProvider out; | 10 | private final OutputProvider out; |
| 9 | private final InputProvider in; | 11 | private final InputProcessor in; |
| 10 | private final List<MenuCommand> commands; | 12 | private final List<MenuCommand> commands; |
| 11 | 13 | ||
| 12 | public Menu( | 14 | public Menu(List<MenuCommand> commands, OutputProvider out, InputProcessor in) { |
| 13 | List<MenuCommand> commands, OutputProvider outputProvider, InputProvider inputProvider) { | ||
| 14 | this.commands = commands; | 15 | this.commands = commands; |
| 15 | this.out = outputProvider; | 16 | this.out = out; |
| 16 | this.in = inputProvider; | 17 | this.in = in; |
| 17 | } | 18 | } |
| 18 | 19 | ||
| 19 | @Override | 20 | @Override |
| 20 | public void start() { | 21 | public void start() { |
| 21 | printMenu(); | 22 | out.splashScreen(); |
| 23 | |||
| 24 | while (true) { | ||
| 25 | printMenu(); | ||
| 26 | |||
| 27 | var selection = in.readInt("Choose a command: ", 1, commands.size()); | ||
| 28 | var commandOptional = commands.stream().filter(c -> c.getIndex() == selection).findFirst(); | ||
| 29 | |||
| 30 | if (commandOptional.isEmpty()) { | ||
| 31 | out.println("There is no command %d.".formatted(selection)); | ||
| 32 | continue; | ||
| 33 | } | ||
| 34 | var command = commandOptional.get(); | ||
| 35 | |||
| 36 | command.execute(); | ||
| 37 | } | ||
| 22 | } | 38 | } |
| 23 | 39 | ||
| 24 | @Override | 40 | @Override |
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 eddc68f..5a91fe0 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 | |||
| @@ -1,15 +1,19 @@ | |||
| 1 | package market.guess.ui.console.command; | 1 | package market.guess.ui.console.command; |
| 2 | 2 | ||
| 3 | import java.nio.file.Path; | ||
| 3 | import market.guess.api.GuessMarketContext; | 4 | import market.guess.api.GuessMarketContext; |
| 4 | import market.guess.ui.console.OutputProvider; | 5 | import market.guess.ui.console.io.InputProcessor; |
| 6 | import market.guess.ui.console.io.OutputProvider; | ||
| 5 | 7 | ||
| 6 | public final class LoadFileMenuCommand implements MenuCommand { | 8 | public final class LoadFileMenuCommand implements MenuCommand { |
| 7 | private final GuessMarketContext context; | 9 | private final GuessMarketContext context; |
| 8 | private final OutputProvider out; | 10 | private final OutputProvider out; |
| 11 | private final InputProcessor in; | ||
| 9 | 12 | ||
| 10 | public LoadFileMenuCommand(GuessMarketContext context, OutputProvider out) { | 13 | public LoadFileMenuCommand(GuessMarketContext context, InputProcessor in, OutputProvider out) { |
| 11 | this.context = context; | 14 | this.context = context; |
| 12 | this.out = out; | 15 | this.out = out; |
| 16 | this.in = in; | ||
| 13 | } | 17 | } |
| 14 | 18 | ||
| 15 | @Override | 19 | @Override |
| @@ -29,7 +33,12 @@ public final class LoadFileMenuCommand implements MenuCommand { | |||
| 29 | 33 | ||
| 30 | @Override | 34 | @Override |
| 31 | public void execute() { | 35 | public void execute() { |
| 32 | // TODO Auto-generated method stub | 36 | var path = in.readPath("Pleae enter the full file path to the XML file: "); |
| 33 | throw new UnsupportedOperationException("Unimplemented method 'execute'"); | 37 | var result = context.loadEvents(Path.of(path)); |
| 38 | |||
| 39 | if (result.success()) { | ||
| 40 | out.println("Loaded %d event(s) from %s.".formatted(result.eventsLoaded(), result.source())); | ||
| 41 | return; | ||
| 42 | } | ||
| 34 | } | 43 | } |
| 35 | } | 44 | } |
diff --git a/ui-console/src/main/java/market/guess/ui/console/ConsoleInputProvider.java b/ui-console/src/main/java/market/guess/ui/console/io/ConsoleInputProvider.java index 38b02a9..5c93f71 100644 --- a/ui-console/src/main/java/market/guess/ui/console/ConsoleInputProvider.java +++ b/ui-console/src/main/java/market/guess/ui/console/io/ConsoleInputProvider.java | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | package market.guess.ui.console; | 1 | package market.guess.ui.console.io; |
| 2 | 2 | ||
| 3 | import java.util.Scanner; | 3 | import java.util.Scanner; |
| 4 | 4 | ||
| @@ -6,16 +6,12 @@ public class ConsoleInputProvider implements InputProvider { | |||
| 6 | private final Scanner scanner = new Scanner(System.in); | 6 | private final Scanner scanner = new Scanner(System.in); |
| 7 | 7 | ||
| 8 | @Override | 8 | @Override |
| 9 | public int nextInt() { | 9 | public void close() throws Exception { |
| 10 | while (!scanner.hasNextInt()) { | 10 | scanner.close(); |
| 11 | scanner.next(); | ||
| 12 | } | ||
| 13 | |||
| 14 | return scanner.nextInt(); | ||
| 15 | } | 11 | } |
| 16 | 12 | ||
| 17 | @Override | 13 | @Override |
| 18 | public void close() throws Exception { | 14 | public String nextLine() { |
| 19 | scanner.close(); | 15 | return scanner.hasNextLine() ? scanner.nextLine() : null; |
| 20 | } | 16 | } |
| 21 | } | 17 | } |
diff --git a/ui-console/src/main/java/market/guess/ui/console/ConsoleOutputProvider.java b/ui-console/src/main/java/market/guess/ui/console/io/ConsoleOutputProvider.java index 3009d95..6fa7595 100644 --- a/ui-console/src/main/java/market/guess/ui/console/ConsoleOutputProvider.java +++ b/ui-console/src/main/java/market/guess/ui/console/io/ConsoleOutputProvider.java | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | package market.guess.ui.console; | 1 | package market.guess.ui.console.io; |
| 2 | 2 | ||
| 3 | public class ConsoleOutputProvider implements OutputProvider { | 3 | public class ConsoleOutputProvider implements OutputProvider { |
| 4 | 4 | ||
| @@ -16,4 +16,9 @@ public class ConsoleOutputProvider implements OutputProvider { | |||
| 16 | public void newLine() { | 16 | public void newLine() { |
| 17 | IO.println(""); | 17 | IO.println(""); |
| 18 | } | 18 | } |
| 19 | |||
| 20 | @Override | ||
| 21 | public void splashScreen() { | ||
| 22 | IO.println("<<-- GUESS MARKET -->>"); | ||
| 23 | } | ||
| 19 | } | 24 | } |
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 new file mode 100644 index 0000000..9f03acb --- /dev/null +++ b/ui-console/src/main/java/market/guess/ui/console/io/InputProcessor.java | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | package market.guess.ui.console.io; | ||
| 2 | |||
| 3 | public final class InputProcessor { | ||
| 4 | private final OutputProvider out; | ||
| 5 | private final InputProvider in; | ||
| 6 | |||
| 7 | public InputProcessor(InputProvider in, OutputProvider out) { | ||
| 8 | super(); | ||
| 9 | this.in = in; | ||
| 10 | this.out = out; | ||
| 11 | } | ||
| 12 | |||
| 13 | public String readLine(String prompt) { | ||
| 14 | out.print(prompt); | ||
| 15 | var line = in.nextLine(); | ||
| 16 | |||
| 17 | return line.trim(); | ||
| 18 | } | ||
| 19 | |||
| 20 | public int readInt(String prompt, int min, int max) { | ||
| 21 | while (true) { | ||
| 22 | var raw = in.nextLine(); | ||
| 23 | if (raw.isBlank()) { | ||
| 24 | out.println("Please enter a number between %d and %d.".formatted(min, max)); | ||
| 25 | continue; | ||
| 26 | } | ||
| 27 | try { | ||
| 28 | var value = Integer.parseInt(raw); | ||
| 29 | |||
| 30 | if (value > max || value < min) { | ||
| 31 | out.println( | ||
| 32 | "%d is out of range, please enter a number between %d and %d." | ||
| 33 | .formatted(value, min, max)); | ||
| 34 | continue; | ||
| 35 | } | ||
| 36 | return value; | ||
| 37 | } catch (NumberFormatException e) { | ||
| 38 | out.println( | ||
| 39 | "%s in not a number, please enter a number between %d and %d." | ||
| 40 | .formatted(raw, min, max)); | ||
| 41 | continue; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | public String readPath(String prompt) { | ||
| 47 | while (true) { | ||
| 48 | var raw = readLine(prompt); | ||
| 49 | |||
| 50 | if (raw.isBlank()) { | ||
| 51 | out.println("Please enter a file path."); | ||
| 52 | continue; | ||
| 53 | } | ||
| 54 | |||
| 55 | return raw; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
diff --git a/ui-console/src/main/java/market/guess/ui/console/InputProvider.java b/ui-console/src/main/java/market/guess/ui/console/io/InputProvider.java index 3579b4a..9404bb7 100644 --- a/ui-console/src/main/java/market/guess/ui/console/InputProvider.java +++ b/ui-console/src/main/java/market/guess/ui/console/io/InputProvider.java | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | package market.guess.ui.console; | 1 | package market.guess.ui.console.io; |
| 2 | 2 | ||
| 3 | public interface InputProvider extends AutoCloseable { | 3 | public interface InputProvider extends AutoCloseable { |
| 4 | int nextInt(); | 4 | String nextLine(); |
| 5 | } | 5 | } |
diff --git a/ui-console/src/main/java/market/guess/ui/console/OutputProvider.java b/ui-console/src/main/java/market/guess/ui/console/io/OutputProvider.java index e49bec5..e419351 100644 --- a/ui-console/src/main/java/market/guess/ui/console/OutputProvider.java +++ b/ui-console/src/main/java/market/guess/ui/console/io/OutputProvider.java | |||
| @@ -1,9 +1,11 @@ | |||
| 1 | package market.guess.ui.console; | 1 | package market.guess.ui.console.io; |
| 2 | 2 | ||
| 3 | public interface OutputProvider { | 3 | public interface OutputProvider { |
| 4 | void print(Object object); | 4 | void print(Object object); |
| 5 | 5 | ||
| 6 | void newLine(); | 6 | void newLine(); |
| 7 | 7 | ||
| 8 | void splashScreen(); | ||
| 9 | |||
| 8 | void println(Object object); | 10 | void println(Object object); |
| 9 | } | 11 | } |