170 lines
6.4 KiB
Java
170 lines
6.4 KiB
Java
import java.io.BufferedReader;
|
|
import java.io.InputStreamReader;
|
|
import java.io.IOException;
|
|
|
|
/**
|
|
* Main Class for the Student-Administration.<br />
|
|
* The user types commands and parameters to manipulate or change
|
|
* the Trie. <br />Commands are found with the HELP command
|
|
*/
|
|
final class Shell {
|
|
private Shell() { }
|
|
/**
|
|
* Checks if <code>string</code> consists of only chars.
|
|
* @param string String to check
|
|
* @return <code>true</code>, if <code>string</code> has only chars<br />
|
|
* <code>false</code>, if <code>string</code> has other signs
|
|
*/
|
|
private static boolean isCharString(String string) {
|
|
for (int j = 0; j < string.length(); j++) {
|
|
if (string.charAt(j) - 'a' < 0
|
|
|| string.charAt(j) - 'a' > 25) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Main method for the Student Administration including the Shell.
|
|
* Validation for input.
|
|
* @param args are not handled at the moment
|
|
* @throws IOException
|
|
*/
|
|
public static void main(String[] args) throws IOException {
|
|
Trie t = new Trie();
|
|
boolean runtime = true;
|
|
BufferedReader br = new BufferedReader(
|
|
new InputStreamReader(System.in));
|
|
while (runtime) {
|
|
System.out.print("trie> ");
|
|
String input = br.readLine();
|
|
String[] splited = input.split(" ");
|
|
char ch = splited[0].charAt(0);
|
|
ch = Character.toLowerCase(ch);
|
|
try {
|
|
switch(ch) {
|
|
case 'n' :
|
|
if (splited.length != 1) {
|
|
System.out.println("Error! Wrong Parameter! "
|
|
+ "check HELP");
|
|
break;
|
|
}
|
|
t = new Trie();
|
|
break;
|
|
case 'a' :
|
|
if (splited.length != 3) {
|
|
System.out.println("Error! Wrong Parameter! "
|
|
+ "check HELP");
|
|
break;
|
|
}
|
|
if (!isCharString(splited[1])) {
|
|
System.out.println("Error! Wrong Parameter! "
|
|
+ "check HELP");
|
|
break;
|
|
}
|
|
if (Integer.parseInt(splited[2]) < 0) {
|
|
System.out.println("Error! Wrong Parameter! "
|
|
+ "check HELP");
|
|
break;
|
|
}
|
|
if (!t.add(splited[1], Integer.parseInt(splited[2]))) {
|
|
System.out.println("Error! " + splited[1]
|
|
+ " is already present.");
|
|
}
|
|
break;
|
|
case 'c' :
|
|
if (splited.length != 3) {
|
|
System.out.println("Error! Wrong Parameter! "
|
|
+ "check HELP");
|
|
break;
|
|
}
|
|
if (!isCharString(splited[1])) {
|
|
System.out.println("Error! Wrong Parameter! "
|
|
+ "check HELP");
|
|
break;
|
|
}
|
|
if (Integer.parseInt(splited[2]) < 0) {
|
|
System.out.println("Error! Wrong Parameter! "
|
|
+ "check HELP");
|
|
break;
|
|
}
|
|
if (!t.change(splited[1], Integer.parseInt(splited[2]))) {
|
|
System.out.println("Error! " + splited[1]
|
|
+ " does not yet exist");
|
|
}
|
|
break;
|
|
case 'd' :
|
|
if (splited.length != 2) {
|
|
System.out.println("Error! Wrong Parameter! "
|
|
+ "check HELP");
|
|
break;
|
|
}
|
|
if (!isCharString(splited[1])) {
|
|
System.out.println("Error! Wrong Parameter! "
|
|
+ "check HELP");
|
|
break;
|
|
}
|
|
if (!t.delete(splited[1])) {
|
|
System.out.println("Error! " + splited[1]
|
|
+ " does not yet exist");
|
|
}
|
|
break;
|
|
case 'p' :
|
|
if (splited.length != 2) {
|
|
System.out.println("Error! Wrong Parameter! "
|
|
+ "check HELP");
|
|
break;
|
|
}
|
|
if (!isCharString(splited[1])) {
|
|
System.out.println("Error! Wrong Parameter! "
|
|
+ "check HELP");
|
|
break;
|
|
}
|
|
if (t.points(splited[1]) != null) {
|
|
System.out.println(t.points(splited[1]));
|
|
} else {
|
|
System.out.println("Error! " + splited[1]
|
|
+ " does not yet exist");
|
|
}
|
|
|
|
break;
|
|
case 't' :
|
|
if (splited.length != 1) {
|
|
System.out.println("Error! Wrong Parameter! "
|
|
+ "check HELP");
|
|
break;
|
|
}
|
|
System.out.println(t.toString());
|
|
break;
|
|
case 'h' :
|
|
if (splited.length != 1) {
|
|
System.out.println("Error! Wrong Parameter! "
|
|
+ "check HELP");
|
|
break;
|
|
}
|
|
System.out.println("NEW Creates a new empty Trie ");
|
|
System.out.println("ADD adds <name> with <points>");
|
|
System.out.println("CHANGE change <points> of <name>");
|
|
System.out.println("DELETE deletes <name>");
|
|
System.out.println("POINTS displays points of <name>");
|
|
System.out.println("TRIE displays hole Trie");
|
|
System.out.println("HELP display this help");
|
|
System.out.println("QUIT exit Shell");
|
|
break;
|
|
case 'q' :
|
|
if (splited.length != 1) {
|
|
System.out.println("Error! Wrong Parameter! check HELP");
|
|
break;
|
|
}
|
|
runtime = false;
|
|
break;
|
|
default :
|
|
System.out.println("Error! Wrong command check HELP");
|
|
}
|
|
} catch (NumberFormatException e) {
|
|
System.out.println("Error! Wrong Parameter! check HELP");
|
|
}
|
|
}
|
|
}
|
|
} |