-
Notifications
You must be signed in to change notification settings - Fork 5
use pattern to handle a commands #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: shtramak
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package juja.sqlcmd.controller; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import juja.sqlcmd.DatabaseManager; | ||
| import juja.sqlcmd.controller.command.Command; | ||
| import juja.sqlcmd.controller.command.CommandType; | ||
| import juja.sqlcmd.controller.command.Connect; | ||
| import juja.sqlcmd.controller.command.Exit; | ||
| import juja.sqlcmd.controller.command.UnsupportedCommand; | ||
| import juja.sqlcmd.view.View; | ||
|
|
||
| public class CommandHandler { | ||
| private View view; | ||
| private Map<CommandType, Command> registeredCommands = new HashMap<>(); | ||
| private CommandType[] allCommandTypes; | ||
|
|
||
| public CommandHandler(DatabaseManager databaseManager, View view) { | ||
| this.view = view; | ||
| register(CommandType.CONNECT, new Connect(databaseManager, view)); | ||
| register(CommandType.EXIT, new Exit(databaseManager, view)); | ||
| allCommandTypes = CommandType.values(); | ||
| } | ||
|
|
||
| public void handleCommand(String strCommand) { | ||
| if (strCommand.toLowerCase().equals("help")) { | ||
| view.write("List of available commands to execute:"); | ||
| for (CommandType commandType : allCommandTypes) { | ||
| view.write(commandType.getDescription()); | ||
| } | ||
| } else { | ||
| Command command = getCommandByName(strCommand); | ||
| command.execute(); | ||
| } | ||
| } | ||
|
|
||
| private Command getCommandByName(String commandName) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А я не могу понять как отрабатывает connect, если как enum это одно слово connect, а как команда он 4 слова |
||
| for (CommandType commandType : allCommandTypes) { | ||
| if (commandName.toLowerCase().equals(commandType.getName())) { | ||
| return registeredCommands.get(commandType); | ||
| } | ||
| } | ||
| return new UnsupportedCommand(view); | ||
| } | ||
|
|
||
| private void register(CommandType commandType, Command command) { | ||
| registeredCommands.put(commandType, command); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,20 @@ public MainController(View view, DatabaseManager databaseManager) { | |
| this.databaseManager = databaseManager; | ||
| } | ||
|
|
||
| public void run(){ | ||
|
|
||
| public void run() { | ||
| CommandHandler handler = new CommandHandler(databaseManager, view); | ||
| view.write("Welcome to SqlCmd application!\n" + | ||
| "Please enter help to see a list of commands"); | ||
| view.write("-----"); | ||
| while (true) { | ||
| String command = view.read(); | ||
| handler.handleCommand(command); | ||
| if (command.toLowerCase().equals("exit")) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А зачем этот if, если есть команда exit?) |
||
| view.write("Bye!"); | ||
| break; | ||
| } | ||
| view.write("-----"); | ||
| view.write("Please enter next command"); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package juja.sqlcmd.controller.command; | ||
|
|
||
| public interface Command { | ||
| void execute(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package juja.sqlcmd.controller.command; | ||
|
|
||
| public enum CommandType { | ||
|
|
||
| CONNECT("connect", "\tconnect" | ||
| + System.lineSeparator() | ||
| + "\t\tconnection with database. Require to enter credentials"), | ||
|
|
||
| EXIT("exit", "\texit" | ||
| + System.lineSeparator() | ||
| + "\t\tto exit from this session"); | ||
|
|
||
| private String name; | ||
| private String description; | ||
|
|
||
| CommandType(String name, String description) { | ||
| this.name = name; | ||
| this.description = description; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public String getDescription() { | ||
| return description; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package juja.sqlcmd.controller.command; | ||
|
|
||
| import juja.sqlcmd.DatabaseManager; | ||
| import juja.sqlcmd.view.View; | ||
|
|
||
| public class Connect implements Command { | ||
| private DatabaseManager databaseManager; | ||
| private View view; | ||
|
|
||
| public Connect(DatabaseManager databaseManager, View view) { | ||
| this.databaseManager = databaseManager; | ||
| this.view = view; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute() { | ||
| if (databaseManager.hasConnection()) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я поэтому сделал 2 метода с коннектом и без, чтобы не городить if else в каждой команде) |
||
| view.write("Connection already exists. To close current connection use Disconnect or Exit command"); | ||
| } else { | ||
| view.write("Please enter required data in format:\n" + | ||
| "databaseName|login|password|"); | ||
| String command = view.read(); | ||
| if (isCommandValid(command)) { | ||
| String[] splittedCommand = command.split("\\|"); | ||
| String dbName = splittedCommand[0]; | ||
| String login = splittedCommand[1]; | ||
| String password = splittedCommand[2]; | ||
| if (databaseManager.connect(dbName, login, password)) { | ||
| view.write("Successful connection to database " + dbName); | ||
| return; | ||
| } | ||
| } | ||
| view.write("Unsuccessful connection to database. Check if entered data is correct"); | ||
| } | ||
| } | ||
|
|
||
| private boolean isCommandValid(String command) { | ||
| String[] splittedCommand = command.split("\\|"); | ||
| return splittedCommand.length == 3; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package juja.sqlcmd.controller.command; | ||
|
|
||
| import java.sql.SQLException; | ||
|
|
||
| import juja.sqlcmd.DatabaseManager; | ||
| import juja.sqlcmd.view.View; | ||
|
|
||
| public final class Exit implements Command { | ||
| private DatabaseManager databaseManager; | ||
| private View view; | ||
|
|
||
| public Exit(DatabaseManager databaseManager, View view) { | ||
| this.databaseManager = databaseManager; | ||
| this.view = view; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute() { | ||
| if (databaseManager.hasConnection()) { | ||
| try { | ||
| databaseManager.close(); | ||
| view.write("Connection is closed."); | ||
| } catch (SQLException e) { | ||
| view.write("Cannot close current connection! Reason: " + e.getMessage()); | ||
| } | ||
| } else { | ||
| view.write("There's no connection to close."); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package juja.sqlcmd.controller.command; | ||
|
|
||
| import juja.sqlcmd.view.View; | ||
|
|
||
| public class UnsupportedCommand implements Command { | ||
| private View view; | ||
|
|
||
| public UnsupportedCommand(View view) { | ||
| this.view = view; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute() { | ||
| view.write("Entered command is not supported. Enter 'help' to see a list of available commands"); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Потом лучше, конечно, это в команду help вытащить)