diff --git a/a0/src/com/CS3310/a0/Controller.java b/a0/src/com/CS3310/a0/Controller.java index 81e5d3469441e7c2e740d9c384c6adef4570868b..d68c59886778a135a6535a4c704a6c850f0b1844 100644 --- a/a0/src/com/CS3310/a0/Controller.java +++ b/a0/src/com/CS3310/a0/Controller.java @@ -19,6 +19,7 @@ public class Controller { private char [][] unsortedFoundHolder; private char[] sortedArray; private char[] sortedFoundHolder; + private String userName; private Random random = new Random(); private Scanner reader = new Scanner(System.in); private ComputeTime computeTime = new ComputeTime(); @@ -44,28 +45,29 @@ public class Controller { PrintHelp(); while (runProgram) { - //userInputString = GetUserInput(); -// switch (tempString) { -// case "help": -// PrintHelp(); -// break; -// case "dimensions": -// GetArrayDimensions(); -// CreateArray(); -// break; -// case "sort": -// SortArray(); -// break; -// case "print": -// PrintUnsortedArray(); -// break; -// case "exit": + userInputString = GetUserInput(); + switch (userInputString) { + case "print": + PrintUnsortedArray(); + PrintSortedArray(); + break; + case "dimensions": + GetArrayDimensions(); + CreateArray(); + break; + case "core": + RunCore(); + break; + case "help": + PrintHelp(); + break; + case "exit": runProgram = false; -// break; -// default: -// SearchName(tempString); -// break; -// } + break; + default: + PrintHelp(); + break; + } } } @@ -82,15 +84,17 @@ public class Controller { * Core functionality of program. */ private void RunCore() { - String userInputString = GetUserInput("Enter a name to search: "); + userName = GetUserInput("Enter a name to search: "); CreateArray(); + PrintUnsortedArray(); - SearchNameUnsorted(userInputString); + SearchNameUnsorted(true); - SortArray(); PrintSortedArray(); - SearchNameSorted(userInputString); + SearchNameSorted(true); + + ReplaceName(); } @@ -120,6 +124,8 @@ public class Controller { } } + SortArray(); + // // Get time lapse of process. // System.out.print("Array Creation Time: "); // endDate = new Date(); @@ -152,10 +158,9 @@ public class Controller { /** * Iterate through each char in name and search for value in array. - * @param name Name to search through. */ - private void SearchNameUnsorted(String name) { - int stringLength = name.length(); + private void SearchNameUnsorted(boolean printAll) { + int stringLength = userName.length(); int index = 0; int arrayIndex1; int arrayIndex2; @@ -171,20 +176,22 @@ public class Controller { // Search current string values. while (index < stringLength) { - tempChar = name.charAt(index); - LinearSearch(tempChar); + tempChar = userName.charAt(index); + LinearSearch(tempChar, printAll); index++; } - System.out.println(); + + if (printAll) { + System.out.println(); + } } /** * Iterate through each char in name and search for value in array. - * @param name Name to search through. */ - private void SearchNameSorted(String name) { - int stringLength = name.length(); + private void SearchNameSorted(boolean printAll) { + int stringLength = userName.length(); int index = 0; int arrayIndex1; int arrayIndex2; @@ -203,11 +210,16 @@ public class Controller { // Search current string values. index = 0; while (index < stringLength) { - tempChar = name.charAt(index); + tempChar = userName.charAt(index); returnIndex = BinarySearch(tempChar, 0, ((arrayDim1 * arrayDim2) - 1)); index++; - // Send found values or -1 if not found. - PrintArraySearch(tempChar, returnIndex); + if (printAll) { + // Send found values or -1 if not found. + PrintArraySearch(tempChar, returnIndex); + } + } + if (printAll) { + System.out.println(); } } @@ -216,7 +228,7 @@ public class Controller { * Linearly search through array for given value. * @param searchValue Char to search for. */ - private void LinearSearch(char searchValue) { + private void LinearSearch(char searchValue, boolean printAll) { int index1 = -1; int index2 = -1; int arrayIndex1 = 0; @@ -245,8 +257,10 @@ public class Controller { } arrayIndex1++; } - // Send found values or -1 if not found. - PrintArraySearch(searchValue, index1, index2); + if (printAll) { + // Send found values or -1 if not found. + PrintArraySearch(searchValue, index1, index2); + } } @@ -386,9 +400,69 @@ public class Controller { } + + /** + * Replaces characters in user's name m times, where m is declared by user input. + */ + private void ReplaceName() { + boolean validInput = false; + int index; + int replaceInt = 0; + String userInputString; + + // Loop until user enters a valid integer. + while (!(validInput)) { + userInputString = GetUserInput("Enter number of chars to replace: "); + try { + replaceInt = Integer.parseInt(userInputString); + validInput = true; + } catch(Exception e) { + System.out.println("Invalid integer."); + } + } + + // Replaces characters in original user name. + index = 0; + while (index < replaceInt) { + ReplaceNameChar(); + index++; + + // Search with new name value. + SearchNameUnsorted(true); + SearchNameSorted(true); + } + + System.out.println("New Name: " + userName); + } + + + /** + * Replaces a random character in the user's name. + * Pays attention to which indexes have already been replaced. + */ + private void ReplaceNameChar() { + int randomSpot = 0; + int randomInt; + char[] tempChar; + + // Get string index to replace. + randomSpot = random.nextInt(userName.length()); + + // Get random ascii value and convert to char. + randomInt = random.nextInt(26); + randomInt = randomInt + 97; // Set to valid ascii codes. + tempChar = Character.toChars((char) randomInt); + + // Recreate name. + StringBuilder newName = new StringBuilder(userName); + newName.setCharAt(randomSpot, tempChar[0]); + userName = newName.toString(); + } + //endregion Array Management Methods + //region User Interface Methods /** @@ -396,7 +470,7 @@ public class Controller { * @return String of user's input. */ private String GetUserInput() { - return reader.next().toLowerCase(); + return reader.next().trim().toLowerCase(); } @@ -407,7 +481,7 @@ public class Controller { */ private String GetUserInput(String promptString) { System.out.println(promptString); - return reader.next().toLowerCase(); + return reader.next().trim().toLowerCase(); } @@ -415,12 +489,12 @@ public class Controller { * Print help text for user. */ private void PrintHelp() { - System.out.println("To print current array values, type 'Print'"); - System.out.println("To change array dimensions, type 'Dimensions'"); - System.out.println("To sort current array, type 'Sort'"); - System.out.println("To show help again, type 'Help'"); - System.out.println("To exit program, type 'Exit'"); - System.out.println("Otherwise, type name to search in array"); + System.out.println(); + System.out.println("To print current array values, type 'Print'."); + System.out.println("To change array dimensions, type 'Dimensions'."); + System.out.println("To run core program, type 'Core'."); + System.out.println("To show help again, type 'Help'."); + System.out.println("To exit program, type 'Exit'."); System.out.println(); }