-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/M3-12(Criação de Menu) ✨ Adicionar interface de linha de comando…
… para aplicativo - **Adicionar Interface de Linha de Comando para a aplicação** - Foi adicionada uma Interface de Linha de Comando (CLI) para a aplicação com uma classe Menu para exibir e lidar com diferentes escolhas do usuário. - **Criação do QuestionHandler Enum** - Criado um enum QuestionHandler para processar diferentes tipos de entrada do usuário. - **Melhorar a usabilidade** - Essas adições permitirão que a aplicação seja usada de forma interativa a partir da linha de comando, melhorando a usabilidade.
- Loading branch information
1 parent
c013a68
commit 1d7884b
Showing
5 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
src/main/java/diegosneves/github/hexagonal/adapters/cli/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package diegosneves.github.hexagonal.adapters.cli; | ||
|
||
import diegosneves.github.hexagonal.adapters.cli.controller.Menu; | ||
|
||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
Menu menu = new Menu(); | ||
menu.displayMenu(); | ||
} | ||
|
||
} |
104 changes: 104 additions & 0 deletions
104
src/main/java/diegosneves/github/hexagonal/adapters/cli/controller/Menu.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package diegosneves.github.hexagonal.adapters.cli.controller; | ||
|
||
import diegosneves.github.hexagonal.adapters.cli.enums.QuestionHandler; | ||
|
||
import java.util.Scanner; | ||
|
||
/** | ||
* A classe Menu representa um menu com opções e funcionalidades para exibir e interagir com o menu. | ||
* Cada opção do menu corresponde a uma funcionalidade diferente. | ||
* <pre>{@code | ||
* As opções disponíveis são: | ||
* 1 - Obter um produto pelo ID | ||
* 2 - Criar um novo produto | ||
* 3 - Ativar um produto pelo ID | ||
* 4 - Desativar um produto pelo ID | ||
* 5 - Sair | ||
* }</pre> | ||
* <p> | ||
* O usuário pode interagir com o menu inserindo o número correspondente à opção desejada. | ||
* Se a opção escolhida for válida, será executada a funcionalidade correspondente. | ||
* Caso contrário, será exibida uma mensagem de erro ("Opção Inválida!!") | ||
* e o menu será apresentado novamente para que o usuário possa tentar novamente.. | ||
* <p> | ||
* <b>Vale ressaltar que esta é apenas uma classe de exemplo para fins didáticos e, portanto, não segue necessariamente as melhores práticas de desenvolvimento de software.</b> | ||
* | ||
* @author diegoneves | ||
*/ | ||
public class Menu { | ||
|
||
private static final String MENU_MESSAGE = "Escolha uma opção%s:"; | ||
private static final String SINGLE_OPTION = " [Existe apenas a opção 1]"; | ||
private static final String TOTAL_OPTIONS = " [Entre 1 - %d]"; | ||
private static final String NEW_LINE = "\n"; | ||
private static final String MENU_DIVIDER = "="; | ||
private static final String MENU_OPTION_SEPARATOR = " - "; | ||
private static final int MENU_DIVIDER_SIZE = 50; | ||
|
||
public void displayMenu() { | ||
boolean isOn = Boolean.TRUE; | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
while (isOn) { | ||
int option = this.optionBuilder(scanner, | ||
"Obter Produto por ID", | ||
"Criar um produto", | ||
"Ativar Produto por ID", | ||
"Desativar Produto por ID", | ||
"Sair"); | ||
switch (option) { | ||
case 1: | ||
System.out.println("Obter Produto por ID"); | ||
String id = (String) QuestionHandler.STRING.response(scanner, "Informe o ID desejado"); | ||
System.out.println(id); | ||
break; | ||
case 2: | ||
System.out.println("Criar um produto"); | ||
String productName = (String) QuestionHandler.STRING.response(scanner, "Informe o nome do Produto"); | ||
Double productPrice = (Double) QuestionHandler.DOUBLE.response(scanner, "Informe o preço do Produto"); | ||
System.out.println(productName); | ||
System.out.println(productPrice); | ||
break; | ||
case 3: | ||
System.out.println("Ativar Produto por ID"); | ||
String ativarId = (String) QuestionHandler.STRING.response(scanner, "Informe o ID do Produto para ativação"); | ||
System.out.println(ativarId); | ||
break; | ||
case 4: | ||
System.out.println("Desativar Produto por ID"); | ||
String desativaId = (String) QuestionHandler.STRING.response(scanner, "Informe o ID do Produto para desativação"); | ||
System.out.println(desativaId); | ||
break; | ||
case 5: | ||
isOn = Boolean.FALSE; | ||
break; | ||
default: | ||
System.out.println("Opção Inválida!!"); | ||
break; | ||
} | ||
|
||
} | ||
scanner.close(); | ||
} | ||
|
||
private int optionBuilder(Scanner scanner, String... options) { | ||
int option = 0; | ||
StringBuilder sb = new StringBuilder(MENU_DIVIDER.repeat(MENU_DIVIDER_SIZE)).append(NEW_LINE); | ||
for (int i = 0; i < options.length; i++) { | ||
if (i == options.length -1) { | ||
sb.append(i + 1).append(MENU_OPTION_SEPARATOR).append(options[i]).append(NEW_LINE).append(MENU_DIVIDER.repeat(MENU_DIVIDER_SIZE)); | ||
} else { | ||
sb.append(i + 1).append(MENU_OPTION_SEPARATOR).append(options[i]).append(NEW_LINE); | ||
} | ||
} | ||
System.out.println(sb); | ||
System.out.printf(MENU_MESSAGE, options.length == 1 ? SINGLE_OPTION : String.format(TOTAL_OPTIONS, options.length)); | ||
try { | ||
option = scanner.nextInt(); | ||
} catch (Exception ignored) { | ||
return 0; | ||
} | ||
return option; | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/diegosneves/github/hexagonal/adapters/cli/enums/QuestionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package diegosneves.github.hexagonal.adapters.cli.enums; | ||
|
||
import java.util.Scanner; | ||
|
||
public enum QuestionHandler{ | ||
|
||
STRING { | ||
@Override | ||
public Object response(Scanner scanner, String question) { | ||
String value = ""; | ||
System.out.printf(QUESTION_FORMAT, question); | ||
value = scanner.next(); | ||
return value; | ||
} | ||
}, | ||
DOUBLE { | ||
@Override | ||
public Object response(Scanner scanner, String question) { | ||
Double value = 0.0; | ||
System.out.printf(QUESTION_FORMAT, question); | ||
try { | ||
value = scanner.nextDouble(); | ||
} catch (Exception ignored) { | ||
System.out.println("Double invalido"); | ||
value = (Double) this.response(scanner, question); | ||
} | ||
return value; | ||
} | ||
}, | ||
INTEGER { | ||
@Override | ||
public Object response(Scanner scanner, String question) { | ||
Integer value = 0; | ||
System.out.printf(QUESTION_FORMAT, question); | ||
try { | ||
value = scanner.nextInt(); | ||
} catch (Exception ignored) { | ||
System.out.println("Integer invalido"); | ||
value = (Integer) this.response(scanner,question); | ||
} | ||
return value; | ||
} | ||
}; | ||
|
||
private static final String QUESTION_FORMAT = "-> %s?\n- "; | ||
|
||
public abstract Object response(Scanner scanner, String question); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters