Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. package org.csystem.cmdpromptapp.app;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5. import java.util.function.Consumer;
  6.  
  7.  
  8. public class Terminal {
  9. public static void main(String[] args)
  10. {
  11. // Evaluate find print loop REPL App
  12. Terminal terminal = new Terminal();
  13. terminal.run();
  14. }
  15. private static final Scanner m_kb = new Scanner(System.in);
  16.  
  17. // Enum 0 ve 1 dememek için parse ederken neyin-nerede olduğu split için aslında.
  18. private enum Info {
  19. OPERATION, INPUT;
  20. }
  21.  
  22.  
  23. // inner class sadece biz kullanacağız dışarıya açmıyoruz.
  24. private class Command {
  25. public String Cmd;
  26. public Consumer<String> Proc;
  27.  
  28. public Command(String cmd, Consumer<String> consumer)
  29. {
  30. Cmd = cmd;
  31. Proc = consumer;
  32. }
  33. }
  34.  
  35. private ArrayList<Command> m_commands;
  36.  
  37. private void init()
  38. {
  39. m_commands = new ArrayList<>();
  40. addCommandToTerminal();
  41. }
  42.  
  43. private void addCommandToTerminal()
  44. {
  45. //m_commands.add( new Command("reverse", input -> System.out.println(StringUtil.reverseWithStrBuilderWay(input))) );
  46. m_commands.add( new Command("len", input -> System.out.println(input.length())) );
  47. // m_commands.add( new Command("isPrime", NumberUtil::isPrime) ); //kendin sınıf yaz araya gir.
  48. m_commands.add( new Command("clear", input -> System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n") ) );
  49. }
  50.  
  51. public Terminal()
  52. {
  53. init();
  54. //...
  55. }
  56.  
  57. public Command findCommandByName(String cmd)
  58. {
  59. for (Command c : m_commands) {
  60. if (c.Cmd.equalsIgnoreCase(cmd))
  61. return c;
  62. }
  63.  
  64. return null;
  65. }
  66.  
  67.  
  68. public void run()
  69. {
  70.  
  71. // Loop
  72. for (;;) {
  73. // Read
  74. System.out.println("operation?-input?");
  75. String operationInput = m_kb.nextLine();
  76.  
  77. if (operationInput.equalsIgnoreCase("exit"))
  78. return;
  79.  
  80. // Parse
  81. String [] parts = operationInput.split("-");
  82.  
  83. // Evaluate & Find Command From Collection
  84. Command c = findCommandByName(parts[Info.OPERATION.ordinal()]);
  85.  
  86. if (c != null)
  87. c.Proc.accept(parts[1]); // print
  88. else
  89. System.out.println("command not found: " + operationInput); //...
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement