|
| 1 | +package org.csystem.app.samples.commandpromptapp; |
| 2 | + |
| 3 | +import org.csystem.util.StringUtil; |
| 4 | + |
| 5 | +import java.util.Scanner; |
| 6 | + |
| 7 | +public class CommandPrompt { |
| 8 | + private static final String [] COMMAND_STRINGS = {"length", "reverse", "upper", "lower", "change", "clear"}; |
| 9 | + private String m_prompt; |
| 10 | + private final Scanner m_kb; |
| 11 | + |
| 12 | + private static String getCommandByText(String text) |
| 13 | + { |
| 14 | + if (text.length() < 3) |
| 15 | + return ""; |
| 16 | + |
| 17 | + for (String s : COMMAND_STRINGS) |
| 18 | + if (s.startsWith(text)) |
| 19 | + return s; |
| 20 | + |
| 21 | + return ""; |
| 22 | + } |
| 23 | + |
| 24 | + private void lengthProc(String [] commandsStr) |
| 25 | + { |
| 26 | + if (commandsStr.length != 2) { |
| 27 | + System.out.println("length bir tane argüman almalıdır"); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + System.out.println(commandsStr[1].length()); |
| 32 | + } |
| 33 | + |
| 34 | + private void reverseProc(String [] commandsStr) |
| 35 | + { |
| 36 | + if (commandsStr.length != 2) { |
| 37 | + System.out.println("revere bir tane argüman almalıdır"); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + System.out.println(StringUtil.reverse(commandsStr[1])); |
| 42 | + } |
| 43 | + |
| 44 | + private void upperProc(String [] commandsStr) |
| 45 | + { |
| 46 | + if (commandsStr.length != 2) { |
| 47 | + System.out.println("upper bir tane argüman almalıdır"); |
| 48 | + return; |
| 49 | + } |
| 50 | + System.out.println(commandsStr[1].toUpperCase()); |
| 51 | + } |
| 52 | + |
| 53 | + private void lowerProc(String [] commandsStr) |
| 54 | + { |
| 55 | + if (commandsStr.length != 2) { |
| 56 | + System.out.println("lower bir tane argüman almalıdır"); |
| 57 | + return; |
| 58 | + } |
| 59 | + System.out.println(commandsStr[1].toLowerCase()); |
| 60 | + } |
| 61 | + |
| 62 | + private void changeProc(String [] commandsStr) |
| 63 | + { |
| 64 | + if (commandsStr.length != 2) { |
| 65 | + System.out.println("change bir tane argüman almalıdır"); |
| 66 | + return; |
| 67 | + } |
| 68 | + m_prompt = commandsStr[1]; |
| 69 | + } |
| 70 | + |
| 71 | + private void clearProc(String [] commandsStr) |
| 72 | + { |
| 73 | + for (int i = 0; i < 30; ++i) |
| 74 | + System.out.println(); |
| 75 | + } |
| 76 | + |
| 77 | + private void doWorkForCommand(String [] commandInfoStr) |
| 78 | + { |
| 79 | + switch (commandInfoStr[0]) { |
| 80 | + case "length": |
| 81 | + lengthProc(commandInfoStr); |
| 82 | + break; |
| 83 | + case "reverse": |
| 84 | + reverseProc(commandInfoStr); |
| 85 | + break; |
| 86 | + case "upper": |
| 87 | + upperProc(commandInfoStr); |
| 88 | + break; |
| 89 | + case "lower": |
| 90 | + lowerProc(commandInfoStr); |
| 91 | + break; |
| 92 | + case "change": |
| 93 | + changeProc(commandInfoStr); |
| 94 | + break; |
| 95 | + case "clear": |
| 96 | + clearProc(commandInfoStr); |
| 97 | + break; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private void parseCommand(String [] commandInfoStr) |
| 102 | + { |
| 103 | + String cmd = getCommandByText(commandInfoStr[0]); |
| 104 | + |
| 105 | + if (!cmd.isEmpty()) { |
| 106 | + commandInfoStr[0] = cmd; |
| 107 | + doWorkForCommand(commandInfoStr); |
| 108 | + } |
| 109 | + else |
| 110 | + System.out.println("Geçersiz komut"); |
| 111 | + } |
| 112 | + |
| 113 | + public CommandPrompt(String p) |
| 114 | + { |
| 115 | + m_prompt = p; |
| 116 | + m_kb = new Scanner(System.in); |
| 117 | + } |
| 118 | + |
| 119 | + public void run() |
| 120 | + { |
| 121 | + System.out.println("C ve Sistem Programcıları Derneği"); |
| 122 | + System.out.println("Homework-013 sorusuna ilişkin bir iskelet"); |
| 123 | + |
| 124 | + for (;;) { |
| 125 | + System.out.print(m_prompt + ">"); |
| 126 | + String cmd = m_kb.nextLine().trim(); |
| 127 | + |
| 128 | + if (cmd.equals("quit")) |
| 129 | + break; |
| 130 | + |
| 131 | + parseCommand(cmd.split("[ \t]+")); |
| 132 | + } |
| 133 | + |
| 134 | + System.out.println("C ve Sistem Programcıları Derneği"); |
| 135 | + System.out.println("Tekrar yapıyor musunuz?"); |
| 136 | + } |
| 137 | +} |
0 commit comments