Created
April 11, 2019 09:55
-
-
Save tcelik/536f46104f050bbcb915b65585e3df9e to your computer and use it in GitHub Desktop.
Terminal-Reply
This file contains hidden or 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
package org.csystem.cmdpromptapp.app; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
import java.util.function.Consumer; | |
public class Terminal { | |
public static void main(String[] args) | |
{ | |
// Evaluate find print loop REPL App | |
Terminal terminal = new Terminal(); | |
terminal.run(); | |
} | |
private static final Scanner m_kb = new Scanner(System.in); | |
// Enum 0 ve 1 dememek için parse ederken neyin-nerede olduğu split için aslında. | |
private enum Info { | |
OPERATION, INPUT; | |
} | |
// inner class sadece biz kullanacağız dışarıya açmıyoruz. | |
private class Command { | |
public String Cmd; | |
public Consumer<String> Proc; | |
public Command(String cmd, Consumer<String> consumer) | |
{ | |
Cmd = cmd; | |
Proc = consumer; | |
} | |
} | |
private ArrayList<Command> m_commands; | |
private void init() | |
{ | |
m_commands = new ArrayList<>(); | |
addCommandToTerminal(); | |
} | |
private void addCommandToTerminal() | |
{ | |
//m_commands.add( new Command("reverse", input -> System.out.println(StringUtil.reverseWithStrBuilderWay(input))) ); | |
m_commands.add( new Command("len", input -> System.out.println(input.length())) ); | |
// m_commands.add( new Command("isPrime", NumberUtil::isPrime) ); //kendin sınıf yaz araya gir. | |
m_commands.add( new Command("clear", input -> System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n") ) ); | |
} | |
public Terminal() | |
{ | |
init(); | |
//... | |
} | |
public Command findCommandByName(String cmd) | |
{ | |
for (Command c : m_commands) { | |
if (c.Cmd.equalsIgnoreCase(cmd)) | |
return c; | |
} | |
return null; | |
} | |
public void run() | |
{ | |
// Loop | |
for (;;) { | |
// Read | |
System.out.println("operation?-input?"); | |
String operationInput = m_kb.nextLine(); | |
if (operationInput.equalsIgnoreCase("exit")) | |
return; | |
// Parse | |
String [] parts = operationInput.split("-"); | |
// Evaluate & Find Command From Collection | |
Command c = findCommandByName(parts[Info.OPERATION.ordinal()]); | |
if (c != null) | |
c.Proc.accept(parts[1]); // print | |
else | |
System.out.println("command not found: " + operationInput); //... | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
REPL APP - Command Line Interface App