Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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); //...
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement