Repository navigation
Expand file tree
/
Copy pathmainUI.java
More file actions
122 lines (86 loc) · 4.13 KB
/
Copy pathmainUI.java
File metadata and controls
122 lines (86 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import java.io.*;
import java.util.*;
public class mainUI {
// Retrieve data to the end of the line as an argument for a method call
// Include two special kinds of arguments:
// "null" asks us to return no string
// "empty" asks us to return an empty string
private static String getEndingString(Scanner userInput ) {
String userArgument = null;
userArgument = userInput.nextLine();
userArgument = userArgument.trim();
// Include a "hack" to provide null and empty strings for testing
if (userArgument.equalsIgnoreCase("empty")) {
userArgument = "";
} else if (userArgument.equalsIgnoreCase("null")) {
userArgument = null;
}
return userArgument;
}
// Main program to process user commands.
// This method is not robust. When it asks for a command, it expects all arguments to be there.
// It is a quickly-done test harness rather than a full solution for an assignment.
public static void main(String[] args) {
// Command options
String addCommand = "add";
String findCommand = "find";
String resetCommand = "reset";
String printCommand = "print";
String quitCommand = "quit";
// Define variables to manage user input
String userCommand = "";
String userArgument = "";
Scanner userInput = new Scanner( System.in );
// Define the recommender that we will be testing.
SearchTree theTree = new SearchTree();
// Define variables to catch the return values of the methods
boolean booleanOutcome;
Integer findOutcome;
// Let the user know how to use this interface
System.out.println("Commands available:");
System.out.println(" " + addCommand + " <string to the end of line>");
System.out.println(" " + findCommand + " <string to the end of line>");
System.out.println(" " + resetCommand );
System.out.println(" " + printCommand );
System.out.println(" " + quitCommand);
// Process the user input until they provide the command "quit"
do {
// Find out what the user wants to do
userCommand = userInput.next();
/* Do what the user asked for. */
if (userCommand.equalsIgnoreCase(addCommand)) {
// Get the parameters.
userArgument = getEndingString( userInput );
// Call the method
booleanOutcome = theTree.add( userArgument );
System.out.println(userCommand + " \""+userArgument+"\" outcome " + booleanOutcome );
} else if (userCommand.equalsIgnoreCase(findCommand)) {
// Get the parameters.
userArgument = getEndingString( userInput );
// Call the method
findOutcome = theTree.find( userArgument );
System.out.println(userCommand + " \""+userArgument+"\" outcome " + findOutcome );
} else if (userCommand.equalsIgnoreCase(resetCommand)) {
// Clear the rest of the line text.
userArgument = getEndingString( userInput );
// Call the method
theTree.reset( );
System.out.println(userCommand + " \""+userArgument+"\"" );
} else if (userCommand.equalsIgnoreCase(printCommand)) {
// Clear the rest of the line text.
userArgument = getEndingString( userInput );
// Call the method
String treeStructure;
treeStructure = theTree.printTree( );
System.out.println(userCommand + " \""+userArgument+"\" outcome " );
System.out.println(treeStructure);
} else if (userCommand.equalsIgnoreCase(quitCommand)) {
System.out.println ( userCommand );
} else {
System.out.println ("Bad command: " + userCommand);
}
} while (!userCommand.equalsIgnoreCase("quit"));
// The user is done so close the stream of user input before ending.
userInput.close();
}
}