From ce17580d99fdb131006940b2e1b8b9e46ea71bdb Mon Sep 17 00:00:00 2001 From: michaelbarton Date: Tue, 22 Oct 2019 13:38:45 -0400 Subject: [PATCH 1/6] Initial Commit --- src/Keyboard.java | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 src/Keyboard.java diff --git a/src/Keyboard.java b/src/Keyboard.java new file mode 100644 index 0000000..3ebf37c --- /dev/null +++ b/src/Keyboard.java @@ -0,0 +1,2 @@ +public abstract class Keyboard { +} From 0d29eaa147cc060511d834f75f8c80b589c1e9c0 Mon Sep 17 00:00:00 2001 From: michaelbarton Date: Wed, 23 Oct 2019 16:29:39 -0400 Subject: [PATCH 2/6] Some initial design planning/brainstorming --- src/Keyboard.java | 28 ++++++++++++++++++++++++- src/KeyboardScripter.java | 11 ++++++++++ src/plan.txt | 43 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/KeyboardScripter.java create mode 100644 src/plan.txt diff --git a/src/Keyboard.java b/src/Keyboard.java index 3ebf37c..d6d3982 100644 --- a/src/Keyboard.java +++ b/src/Keyboard.java @@ -1,2 +1,28 @@ -public abstract class Keyboard { +/** + * We use an abstract class for Keyboard for future possibilities of different keyboard layouts + * that a user wants to script input for + */ +public class Keyboard { + + /** + * The keyboard mapping to use for scripting the lines from input files + */ + private char[][] keyboardLayout; + + /** + * Constructor for this class, sets value of keyboard + * @param keyboardLayout - the keyboard mapping to be used for this instance of Keyboard + */ + public Keyboard(char[][] keyboardLayout) { + this.keyboardLayout = keyboardLayout; + } + + /** + * Takes the input and determines the script necessary to obtain that input + * @param input - a String to convert into a script + * @return a String with the script representing how to obtain the input for this particular keyboard + */ + public String scriptLine(String input) { + return input; + } } diff --git a/src/KeyboardScripter.java b/src/KeyboardScripter.java new file mode 100644 index 0000000..deb208f --- /dev/null +++ b/src/KeyboardScripter.java @@ -0,0 +1,11 @@ +/** + * The driver class for this program, takes file input, reads it + * and outputs scripted version of each line + */ +public class KeyboardScripter { + + public static void main(String[] args) { + + } + +} diff --git a/src/plan.txt b/src/plan.txt new file mode 100644 index 0000000..3dccb0b --- /dev/null +++ b/src/plan.txt @@ -0,0 +1,43 @@ +1. Have a Keyboard class that will accept a keyboard as a double array of chars (can be any kind of keyboard) + a. Make this keyboard layout come from a file like how the input will +2. Create a class called KeyboardScripter that will act as the driver to read in the file, create the Keyboard object + with the appropriate keyboard and output the scripted result. + +Keyboard components: + Attributes: + private char[][] keyboard + + Functions: + public Keyboard() + public String scriptLine(String input) + * any helper functions for scriptLine() + +Steps to implement this: +1. Create classes and skeletons - done +2. Implement Driver.java + a. Create Keyboard object + b. Get file input, read through each line + c. Pass each line to keyboard.scriptLine() + d. Print out the result +3. Implement scriptLine() in Keyboard.java + - Initial idea is to have starting index be (0,0). + - Then, find the destination index (i, j). + - Perform arithmetic to find vertical and horizontal distances and use that to convert to 'U', 'D', 'L', 'R' + and just append a '#' on the end. + - Then, repeat starting index being the (i, j) you were just at. + + - Note: you must first test if we are converting a letter or a space. If it's a space, just do 'S'. + - Note: if a letter, make sure we can do both upper and lower case letters. + + - Forseeable helper functions: + Point doubleIndexOf(Char c) + String convertToScript(Point origin, Point destination) + + Point will be a very small object containing an i and j pair of coordinates representing a location + in the double array of chars + Attributes: + int i, int j + Functions: + Point(i, j) + int getI() + int getJ() \ No newline at end of file From cd3a27d2a2884eaeaecd431b93f63a5dabdad335 Mon Sep 17 00:00:00 2001 From: michaelbarton Date: Thu, 24 Oct 2019 17:18:16 -0400 Subject: [PATCH 3/6] Implemented most of the driver (KeyboardScripter.java), will do some testing next --- src/KeyboardScripter.java | 83 ++++++++++++++++++++++++++++++++++++++ src/alphabeticalLayout.txt | 6 +++ src/plan.txt | 2 +- src/testInput.txt | 4 ++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/alphabeticalLayout.txt create mode 100644 src/testInput.txt diff --git a/src/KeyboardScripter.java b/src/KeyboardScripter.java index deb208f..0e70209 100644 --- a/src/KeyboardScripter.java +++ b/src/KeyboardScripter.java @@ -1,3 +1,9 @@ +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.util.ArrayList; +import java.util.Scanner; + /** * The driver class for this program, takes file input, reads it * and outputs scripted version of each line @@ -5,7 +11,84 @@ public class KeyboardScripter { public static void main(String[] args) { + String rootPath = "/Users/michaelbarton/IdeaProjects/OnScreenKeyboard/src/"; + String alphabeticalFileName = "alphabeticalLayout.txt"; + String qwertyFileName = "qwertyLayout.txt"; + + Scanner sc = new Scanner(System.in); + String keyboardType, scriptFileName; + + char[][] keyboardLayout; + Keyboard keyboard; + + System.out.println("Welcome to KeyboardScripter!"); + + do { + System.out.print("Enter which kind of keyboard you'd like to use (\'alphabetical\' or \'qwerty\'): "); + keyboardType = sc.nextLine(); + } while (!keyboardType.equals("alphabetical") && !keyboardType.equals("qwerty")); + + if(keyboardType.equals("alphabetical")) { + keyboardLayout = createKeyboardLayout(rootPath + alphabeticalFileName); + } else { + keyboardLayout = createKeyboardLayout(rootPath + qwertyFileName); + } + + keyboard = new Keyboard(keyboardLayout); + + System.out.print("\nEnter the file name that you would like to script: "); + scriptFileName = sc.nextLine(); + + try { + BufferedReader br = new BufferedReader(new FileReader(new File(rootPath + scriptFileName))); + + String str; + while ((str = br.readLine()) != null) + System.out.println(keyboard.scriptLine(str)); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + private static char[][] createKeyboardLayout(String fileName) { + ArrayList> table = new ArrayList<>(); + + try { + BufferedReader br = new BufferedReader(new FileReader(new File(fileName))); + + String str; + while ((str = br.readLine()) != null) + table.add(parseCharacters(str)); + + } catch (Exception e) { + e.printStackTrace(); + } + + int rows = table.size(); + int columns = table.get(0).size(); + + char[][] layout = new char[rows][columns]; + + for(int i = 0; i < rows; i++) { + for(int j = 0; j < columns; j++) { + layout[i][j] = table.get(i).get(j); + } + } + + return layout; + } + + private static ArrayList parseCharacters(String str) { + ArrayList characters = new ArrayList<>(); + + String[] splitString = str.split(" "); + + for(String s : splitString) { + characters.add(s.charAt(0)); + } + return characters; } } diff --git a/src/alphabeticalLayout.txt b/src/alphabeticalLayout.txt new file mode 100644 index 0000000..da60cc4 --- /dev/null +++ b/src/alphabeticalLayout.txt @@ -0,0 +1,6 @@ +A B C D E F +G H I J K L +M N O P Q R +S T U V W X +Y Z 1 2 3 4 +5 6 7 8 9 0 \ No newline at end of file diff --git a/src/plan.txt b/src/plan.txt index 3dccb0b..98dc6cf 100644 --- a/src/plan.txt +++ b/src/plan.txt @@ -15,7 +15,7 @@ Keyboard components: Steps to implement this: 1. Create classes and skeletons - done 2. Implement Driver.java - a. Create Keyboard object + a. Create Keyboard object from file b. Get file input, read through each line c. Pass each line to keyboard.scriptLine() d. Print out the result diff --git a/src/testInput.txt b/src/testInput.txt new file mode 100644 index 0000000..f004e00 --- /dev/null +++ b/src/testInput.txt @@ -0,0 +1,4 @@ +Testing this input +This should be on a new line +Here's another one +I'm about to eat some dinner \ No newline at end of file From 4ad418231b4b49a378a4c1e43b68d49109da58ad Mon Sep 17 00:00:00 2001 From: michaelbarton Date: Thu, 24 Oct 2019 22:38:01 -0400 Subject: [PATCH 4/6] Manually tested and documented KeyboardScripter.java completely --- src/KeyboardScripter.java | 41 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/KeyboardScripter.java b/src/KeyboardScripter.java index 0e70209..00fc0f3 100644 --- a/src/KeyboardScripter.java +++ b/src/KeyboardScripter.java @@ -11,16 +11,19 @@ public class KeyboardScripter { public static void main(String[] args) { + + // Strings to be used to access files String rootPath = "/Users/michaelbarton/IdeaProjects/OnScreenKeyboard/src/"; String alphabeticalFileName = "alphabeticalLayout.txt"; String qwertyFileName = "qwertyLayout.txt"; + // Other attributes needed for main Scanner sc = new Scanner(System.in); String keyboardType, scriptFileName; - char[][] keyboardLayout; Keyboard keyboard; + // Print welcome message and determine keyboard type System.out.println("Welcome to KeyboardScripter!"); do { @@ -28,6 +31,7 @@ public static void main(String[] args) { keyboardType = sc.nextLine(); } while (!keyboardType.equals("alphabetical") && !keyboardType.equals("qwerty")); + // Create Keyboard based on keyboard type inputted from user if(keyboardType.equals("alphabetical")) { keyboardLayout = createKeyboardLayout(rootPath + alphabeticalFileName); } else { @@ -36,9 +40,14 @@ public static void main(String[] args) { keyboard = new Keyboard(keyboardLayout); + // For testing only + // print2DArray(keyboardLayout); + + // Obtain file name to read through and script System.out.print("\nEnter the file name that you would like to script: "); scriptFileName = sc.nextLine(); + // Open file, read through and script each line try { BufferedReader br = new BufferedReader(new FileReader(new File(rootPath + scriptFileName))); @@ -51,9 +60,17 @@ public static void main(String[] args) { } + /** + * Reads in the keyboard layout from the file and converts it into a double array of characters + * @param fileName - name of the file to read from + * @return double array of characters + */ private static char[][] createKeyboardLayout(String fileName) { + + // Must use a list as we don't know the size yet ArrayList> table = new ArrayList<>(); + // Read the lines into "table" try { BufferedReader br = new BufferedReader(new FileReader(new File(fileName))); @@ -65,11 +82,12 @@ private static char[][] createKeyboardLayout(String fileName) { e.printStackTrace(); } + // Create the double char array based on dimensions of "table" int rows = table.size(); int columns = table.get(0).size(); - char[][] layout = new char[rows][columns]; + // Extract out all characters from table into the double char array "layout" for(int i = 0; i < rows; i++) { for(int j = 0; j < columns; j++) { layout[i][j] = table.get(i).get(j); @@ -79,6 +97,11 @@ private static char[][] createKeyboardLayout(String fileName) { return layout; } + /** + * Splits up a line of characters into a list based on a space regex + * @param str - the line of characters to split up, each token should be a string of length 1 + * @return an ArrayList of characters representing a split version of str + */ private static ArrayList parseCharacters(String str) { ArrayList characters = new ArrayList<>(); @@ -91,4 +114,18 @@ private static ArrayList parseCharacters(String str) { return characters; } + /** + * Prints out any char[][] array + * Only use this for testing purposes + * @param array - the double array to print out + */ + private static void print2DArray(char[][] array) { + for(char[] row : array) { + for (char element : row) { + System.out.print(element + " "); + } + System.out.println(); + } + } + } From 6de432919f7e56bc6702bbd2f517ec47ace75b8b Mon Sep 17 00:00:00 2001 From: michaelbarton Date: Fri, 25 Oct 2019 17:40:03 -0400 Subject: [PATCH 5/6] Implemented Keyboard.java and manually tested --- src/Keyboard.java | 108 +++++++++++++++++- src/KeyboardScripter.java | 6 +- src/Point.java | 26 +++++ src/{ => inputFiles}/testInput.txt | 2 +- .../alphabeticalLayout.txt | 0 src/keyboardLayouts/qwertyLayout.txt | 3 + 6 files changed, 139 insertions(+), 6 deletions(-) create mode 100644 src/Point.java rename src/{ => inputFiles}/testInput.txt (80%) rename src/{ => keyboardLayouts}/alphabeticalLayout.txt (100%) create mode 100644 src/keyboardLayouts/qwertyLayout.txt diff --git a/src/Keyboard.java b/src/Keyboard.java index d6d3982..76ff8fc 100644 --- a/src/Keyboard.java +++ b/src/Keyboard.java @@ -22,7 +22,111 @@ public Keyboard(char[][] keyboardLayout) { * @param input - a String to convert into a script * @return a String with the script representing how to obtain the input for this particular keyboard */ - public String scriptLine(String input) { - return input; + String scriptLine(String input) { + String result = ""; + String uppercase = input.toUpperCase(); + + Point initial = new Point(); + Point destination = new Point(); + + for(int i = 0; i < uppercase.length(); i++) { + char letter = uppercase.charAt(i); + + if(letter == ' ') + result += 'S'; + else if(keyboardContains(letter)) { + result += scriptLetter(letter, initial, destination); + } else { + result += 'E'; + } + + if(i < uppercase.length() - 1) + result += ","; + } + + return result; + } + + /** + * + * @param letterToScript + * @return + */ + private String scriptLetter(char letterToScript, Point initialPoint, Point destinationPoint) { + String script = ""; + + setDestinationPoint(letterToScript, destinationPoint); + + int verticalDisplacement = destinationPoint.getI() - initialPoint.getI(); + int horizontalDisplacement = destinationPoint.getJ() - initialPoint.getJ(); + + if(verticalDisplacement < 0) { + verticalDisplacement *= -1; + script += addLetters(verticalDisplacement, 'U'); + } else { + script += addLetters(verticalDisplacement, 'D'); + } + + if(horizontalDisplacement < 0) { + horizontalDisplacement *= -1; + script += addLetters(horizontalDisplacement, 'L'); + } else { + script += addLetters(horizontalDisplacement, 'R'); + } + + script += "#"; + + initialPoint.setI(destinationPoint.getI()); + initialPoint.setJ(destinationPoint.getJ()); + + return script; + } + + /** + * + * @param numTimes + * @param toAdd + * @return + */ + private String addLetters(int numTimes, char toAdd) { + String builder = ""; + + for(int i = 0; i < numTimes; i++) + builder += toAdd + ","; + + return builder; + } + + /** + * + * @param letterToFind + * @param pointToSet + * @return + */ + private void setDestinationPoint(char letterToFind, Point pointToSet) { + for(int i = 0; i < keyboardLayout.length; i++) { + for(int j = 0; j < keyboardLayout[0].length; j++) { + if(letterToFind == keyboardLayout[i][j]) { + pointToSet.setI(i); + pointToSet.setJ(j); + } + } + } + } + + /** + * + * @param letterToCheck + * @return + */ + private boolean keyboardContains(char letterToCheck) { + for(char[] row : keyboardLayout) { + for(char c : row) { + if(c == letterToCheck) + return true; + } + } + + return false; } } diff --git a/src/KeyboardScripter.java b/src/KeyboardScripter.java index 00fc0f3..c5324a7 100644 --- a/src/KeyboardScripter.java +++ b/src/KeyboardScripter.java @@ -14,8 +14,8 @@ public static void main(String[] args) { // Strings to be used to access files String rootPath = "/Users/michaelbarton/IdeaProjects/OnScreenKeyboard/src/"; - String alphabeticalFileName = "alphabeticalLayout.txt"; - String qwertyFileName = "qwertyLayout.txt"; + String alphabeticalFileName = "keyboardLayouts/alphabeticalLayout.txt"; + String qwertyFileName = "keyboardLayouts/qwertyLayout.txt"; // Other attributes needed for main Scanner sc = new Scanner(System.in); @@ -41,7 +41,7 @@ public static void main(String[] args) { keyboard = new Keyboard(keyboardLayout); // For testing only - // print2DArray(keyboardLayout); + print2DArray(keyboardLayout); // Obtain file name to read through and script System.out.print("\nEnter the file name that you would like to script: "); diff --git a/src/Point.java b/src/Point.java new file mode 100644 index 0000000..ec0d750 --- /dev/null +++ b/src/Point.java @@ -0,0 +1,26 @@ +public class Point { + + private int i; + private int j; + + public Point() { + i = 0; + j = 0; + } + + public int getI() { + return i; + } + + public int getJ() { + return j; + } + + public void setI(int i) { + this.i = i; + } + + public void setJ(int j) { + this.j = j; + } +} diff --git a/src/testInput.txt b/src/inputFiles/testInput.txt similarity index 80% rename from src/testInput.txt rename to src/inputFiles/testInput.txt index f004e00..780dd06 100644 --- a/src/testInput.txt +++ b/src/inputFiles/testInput.txt @@ -1,4 +1,4 @@ -Testing this input +IT Crowd This should be on a new line Here's another one I'm about to eat some dinner \ No newline at end of file diff --git a/src/alphabeticalLayout.txt b/src/keyboardLayouts/alphabeticalLayout.txt similarity index 100% rename from src/alphabeticalLayout.txt rename to src/keyboardLayouts/alphabeticalLayout.txt diff --git a/src/keyboardLayouts/qwertyLayout.txt b/src/keyboardLayouts/qwertyLayout.txt new file mode 100644 index 0000000..d75e77f --- /dev/null +++ b/src/keyboardLayouts/qwertyLayout.txt @@ -0,0 +1,3 @@ +Q W E R T Y U I O P 0 1 +A S D F G H J K L 2 3 4 +Z X C V B N M 5 6 7 8 9 \ No newline at end of file From 16073367eaaf76c750c47186d9c79a39376884cd Mon Sep 17 00:00:00 2001 From: michaelbarton Date: Fri, 25 Oct 2019 19:21:08 -0400 Subject: [PATCH 6/6] Final commit, added unit tests and cleaned everything up --- src/Keyboard.java | 37 +++++--- src/KeyboardScripter.java | 26 +++--- src/KeyboardTester.java | 160 +++++++++++++++++++++++++++++++++++ src/Point.java | 17 +++- src/inputFiles/testInput.txt | 10 ++- src/plan.txt | 5 +- 6 files changed, 222 insertions(+), 33 deletions(-) create mode 100644 src/KeyboardTester.java diff --git a/src/Keyboard.java b/src/Keyboard.java index 76ff8fc..a84cbbd 100644 --- a/src/Keyboard.java +++ b/src/Keyboard.java @@ -23,20 +23,26 @@ public Keyboard(char[][] keyboardLayout) { * @return a String with the script representing how to obtain the input for this particular keyboard */ String scriptLine(String input) { + // Initializes the builder string and converts input to uppercase to match the keyboard layouts String result = ""; String uppercase = input.toUpperCase(); + // The Point objects we will use to store locations and calculate the paths Point initial = new Point(); Point destination = new Point(); + // Iterate through each character in the string, performing the correct conversions on each for(int i = 0; i < uppercase.length(); i++) { char letter = uppercase.charAt(i); if(letter == ' ') + // A space result += 'S'; else if(keyboardContains(letter)) { + // A character inside the keyboard result += scriptLetter(letter, initial, destination); } else { + // Not a character inside the keyboard - error result += 'E'; } @@ -48,18 +54,21 @@ else if(keyboardContains(letter)) { } /** - * - * @param letterToScript - * @return + * Takes letterToScript and converts it to a sequence of U, D, L and R's, ending with a # + * @param letterToScript - the letter to convert + * @return a string formatted as explained above */ private String scriptLetter(char letterToScript, Point initialPoint, Point destinationPoint) { String script = ""; + // Sets the correct location of the destination of the letter we are now searching for setDestinationPoint(letterToScript, destinationPoint); + // Calculate the displacement (the path) int verticalDisplacement = destinationPoint.getI() - initialPoint.getI(); int horizontalDisplacement = destinationPoint.getJ() - initialPoint.getJ(); + // Based on value of displacement, add appropriate letters to the string we are building if(verticalDisplacement < 0) { verticalDisplacement *= -1; script += addLetters(verticalDisplacement, 'U'); @@ -76,6 +85,7 @@ private String scriptLetter(char letterToScript, Point initialPoint, Point desti script += "#"; + // Current destination point becomes initial point for the next letter (represents the cursor) initialPoint.setI(destinationPoint.getI()); initialPoint.setJ(destinationPoint.getJ()); @@ -83,10 +93,10 @@ private String scriptLetter(char letterToScript, Point initialPoint, Point desti } /** - * - * @param numTimes - * @param toAdd - * @return + * Builds a string consisting of toAdd repeated numTimes times with commas in following each time + * @param numTimes - the number of times to add the character + * @param toAdd - the character to add to the string we are building + * @return a String formatted as explained above */ private String addLetters(int numTimes, char toAdd) { String builder = ""; @@ -98,10 +108,9 @@ private String addLetters(int numTimes, char toAdd) { } /** - * - * @param letterToFind - * @param pointToSet - * @return + * Searches for letterToFind in keyboardLayout and sets the location of it in pointToSet + * @param letterToFind - the letter whose location we are searching for + * @param pointToSet - the Point whose location we will set to match letterToFind, represents the destination */ private void setDestinationPoint(char letterToFind, Point pointToSet) { for(int i = 0; i < keyboardLayout.length; i++) { @@ -115,9 +124,9 @@ private void setDestinationPoint(char letterToFind, Point pointToSet) { } /** - * - * @param letterToCheck - * @return + * Checks if keyboardLayout contains letterToCheck + * @param letterToCheck - the letter desired in the keyboard + * @return true if keyboardLayout contains letterToCheck, false otherwise */ private boolean keyboardContains(char letterToCheck) { for(char[] row : keyboardLayout) { diff --git a/src/KeyboardScripter.java b/src/KeyboardScripter.java index c5324a7..876dbad 100644 --- a/src/KeyboardScripter.java +++ b/src/KeyboardScripter.java @@ -14,8 +14,10 @@ public static void main(String[] args) { // Strings to be used to access files String rootPath = "/Users/michaelbarton/IdeaProjects/OnScreenKeyboard/src/"; - String alphabeticalFileName = "keyboardLayouts/alphabeticalLayout.txt"; - String qwertyFileName = "keyboardLayouts/qwertyLayout.txt"; + String keyboardLayoutsPath = "keyboardLayouts/"; + String inputFilesPath = "inputFiles/"; + String alphabeticalFileName = "alphabeticalLayout.txt"; + String qwertyFileName = "qwertyLayout.txt"; // Other attributes needed for main Scanner sc = new Scanner(System.in); @@ -33,27 +35,32 @@ public static void main(String[] args) { // Create Keyboard based on keyboard type inputted from user if(keyboardType.equals("alphabetical")) { - keyboardLayout = createKeyboardLayout(rootPath + alphabeticalFileName); + keyboardLayout = createKeyboardLayout(rootPath + keyboardLayoutsPath + alphabeticalFileName); } else { - keyboardLayout = createKeyboardLayout(rootPath + qwertyFileName); + keyboardLayout = createKeyboardLayout(rootPath + keyboardLayoutsPath + qwertyFileName); } keyboard = new Keyboard(keyboardLayout); - // For testing only + System.out.println("\nHere's what your keyboard looks like!"); print2DArray(keyboardLayout); // Obtain file name to read through and script System.out.print("\nEnter the file name that you would like to script: "); scriptFileName = sc.nextLine(); + System.out.println("\nResults:\n"); + // Open file, read through and script each line try { - BufferedReader br = new BufferedReader(new FileReader(new File(rootPath + scriptFileName))); + BufferedReader br = new BufferedReader(new FileReader(new File(rootPath + inputFilesPath + scriptFileName))); String str; - while ((str = br.readLine()) != null) - System.out.println(keyboard.scriptLine(str)); + while ((str = br.readLine()) != null) { + System.out.println("\"" + str + "\" scripts to: "); + System.out.println(keyboard.scriptLine(str) + "\n"); + } + } catch (Exception e) { e.printStackTrace(); } @@ -65,7 +72,7 @@ public static void main(String[] args) { * @param fileName - name of the file to read from * @return double array of characters */ - private static char[][] createKeyboardLayout(String fileName) { + static char[][] createKeyboardLayout(String fileName) { // Must use a list as we don't know the size yet ArrayList> table = new ArrayList<>(); @@ -116,7 +123,6 @@ private static ArrayList parseCharacters(String str) { /** * Prints out any char[][] array - * Only use this for testing purposes * @param array - the double array to print out */ private static void print2DArray(char[][] array) { diff --git a/src/KeyboardTester.java b/src/KeyboardTester.java new file mode 100644 index 0000000..c6f7757 --- /dev/null +++ b/src/KeyboardTester.java @@ -0,0 +1,160 @@ +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class KeyboardTester { + + private static Keyboard alphabeticalKeyboard; + private static Keyboard qwertyKeyboard; + + @BeforeAll + static void setUpKeyboards() { + String rootPath = "/Users/michaelbarton/IdeaProjects/OnScreenKeyboard/src/keyboardLayouts/"; + String alphabeticalFileName = "alphabeticalLayout.txt"; + String qwertyFileName = "qwertyLayout.txt"; + + // Create both Keyboards + char[][] alphabeticalKeyboardLayout = KeyboardScripter.createKeyboardLayout(rootPath + + alphabeticalFileName); + char[][] qwertyKeyboardLayout = KeyboardScripter.createKeyboardLayout(rootPath + qwertyFileName); + + alphabeticalKeyboard = new Keyboard(alphabeticalKeyboardLayout); + qwertyKeyboard = new Keyboard(qwertyKeyboardLayout); + } + + /** + * All Alphabetical keyboard tests + */ + @Test + void emptyLineAlphabetical() { + assertEquals("", alphabeticalKeyboard.scriptLine("")); + } + + @Test + void selectAndDoneAlphabetical() { + assertEquals("#", alphabeticalKeyboard.scriptLine("A")); + } + + @Test + void bottomRightAlphabetical() { + assertEquals("D,D,D,D,D,R,R,R,R,R,#", alphabeticalKeyboard.scriptLine("0")); + } + + @Test + void singleErrorAlphabetical() { + assertEquals("E", alphabeticalKeyboard.scriptLine("+")); + } + + @Test + void singleSpaceAlphabetical() { + assertEquals("S", alphabeticalKeyboard.scriptLine(" ")); + } + + @Test + void allLettersAlphabetical() { + assertEquals("D,D,#,U,R,R,#,U,#,D,L,#,U,L,#,R,R,R,R,#,D,R,#", + alphabeticalKeyboard.scriptLine("Michael")); + } + + @Test + void lettersAndSpacesAlphabetical() { + assertEquals("D,R,#,U,R,R,R,#,D,D,R,#,U,U,L,#,S,D,D,D,#,U,U,U,#,S,D,L,L,L,L,#,D,R,R,#,S,D,L,L,#,R,#" + + ",U,U,U,R,R,R,#,#,D,R,#,U,L,#,D,D,R,#,D,L,L,L,L,L,#", + alphabeticalKeyboard.scriptLine("Here we go Steelers")); + } + + @Test + void lettersAndErrorsAlphabetical() { + assertEquals("D,D,R,R,R,#,U,L,#,D,R,R,R,#,U,U,L,L,L,L,L,#,D,D,D,R,#,U,U,U,R,R,R,#,D,D,D,L,L,L,L,#," + + "E,U,R,R,R,#,U,U,R,#,D,D,L,L,L,#,U,L,#,D,D,R,R,#,U,U,#,D,L,#,D,L,#", + alphabeticalKeyboard.scriptLine("Pirates/Penguins")); + } + + @Test + void spacesAndErrorsAlphabetical() { + assertEquals("E,E,E,E,E,S,E,E,E,E,E,E,E", alphabeticalKeyboard.scriptLine("@@#$! #@$%^*%")); + } + + @Test + void lettersSpacesAndErrorsAlphabetical() { + assertEquals("D,R,R,R,R,R,#,U,L,#,D,D,D,L,L,L,#,E,L,#,S,U,U,#,D,R,R,#,S,R,#,U,U,R,#,D,D,L,L,L,#" + + ",D,L,#,E", alphabeticalKeyboard.scriptLine("Let's go Pens!")); + } + + @Test + void lettersSpacesAndNumbersAlphabetical() { + assertEquals("D,D,D,#,R,#,U,U,U,R,R,R,#,#,D,R,#,U,L,#,D,D,R,#,D,L,L,L,L,L,#,S,U,U,U,#,D,D,R,R,R," + + "R,R,#,U,U,L,#,S,D,D,L,L,L,#,D,R,#,U,L,L,#,U,U,R,#,R,R,R,#,D,D,R,#,S,D,D,L,L,L,#", + alphabeticalKeyboard.scriptLine("Steelers are number 1")); + } + + + + /** + * All Qwerty keyboard tests + */ + @Test + void emptyLineQwerty() { + assertEquals("", qwertyKeyboard.scriptLine("")); + } + + @Test + void selectAndDoneQwerty() { + assertEquals("#", qwertyKeyboard.scriptLine("Q")); + } + + @Test + void bottomRightQwerty() { + assertEquals("D,D,R,R,R,R,R,R,R,R,R,R,R,#", qwertyKeyboard.scriptLine("9")); + } + + @Test + void singleErrorQwerty() { + assertEquals("E", qwertyKeyboard.scriptLine("+")); + } + + @Test + void singleSpaceQwerty() { + assertEquals("S", qwertyKeyboard.scriptLine(" ")); + } + + @Test + void allLettersQwerty() { + assertEquals("D,D,R,R,R,R,R,R,#,U,U,R,#,D,D,L,L,L,L,L,#,U,R,R,R,#,L,L,L,L,L,#,U,R,R,#,D,R,R,R,R,R,R,#", + qwertyKeyboard.scriptLine("Michael")); + } + + @Test + void lettersAndSpacesQwerty() { + assertEquals("D,R,R,R,R,R,#,U,L,L,L,#,R,#,L,#,S,L,#,R,#,S,D,R,R,#,U,R,R,R,R,#,S,D,L,L,L,L,L,L,L," + + "#,U,R,R,R,#,L,L,#,#,D,R,R,R,R,R,R,#,U,L,L,L,L,L,L,#,R,#,D,L,L,#", + qwertyKeyboard.scriptLine("Here we go Steelers")); + } + + @Test + void lettersAndErrorsQwerty() { + assertEquals("R,R,R,R,R,R,R,R,R,#,L,L,#,L,L,L,L,#,D,L,L,L,#,U,R,R,R,R,#,L,L,#,D,L,#,E,U,R,R,R,R,R," + + "R,R,R,#,L,L,L,L,L,L,L,#,D,D,R,R,R,#,U,L,#,U,R,R,#,R,#,D,D,L,L,#,U,L,L,L,L,#", + qwertyKeyboard.scriptLine("Pirates/Penguins")); + } + + @Test + void spacesAndErrorsQwerty() { + assertEquals("E,E,E,E,E,S,E,E,E,E,E,E,E", qwertyKeyboard.scriptLine("@@#$! #@$%^*%")); + } + + @Test + void lettersSpacesAndErrorsQwerty() { + assertEquals("D,R,R,R,R,R,R,R,R,#,U,L,L,L,L,L,L,#,R,R,#,E,D,L,L,L,#,S,R,R,R,#,U,R,R,R,R,#,S,R," + + "#,L,L,L,L,L,L,L,#,D,D,R,R,R,#,U,L,L,L,L,#,E", qwertyKeyboard.scriptLine("Let's go Pens!")); + } + + @Test + void lettersSpacesAndNumbersQwerty() { + assertEquals("D,R,#,U,R,R,R,#,L,L,#,#,D,R,R,R,R,R,R,#,U,L,L,L,L,L,L,#,R,#,D,L,L,#,S,L,#,U,R,R,R" + + ",#,L,#,S,D,D,R,R,R,#,U,U,R,#,D,D,#,L,L,#,U,U,L,L,#,R,#,S,R,R,R,R,R,R,R,R,#", + qwertyKeyboard.scriptLine("Steelers are number 1")); + } + +} diff --git a/src/Point.java b/src/Point.java index ec0d750..ac46a25 100644 --- a/src/Point.java +++ b/src/Point.java @@ -1,26 +1,35 @@ public class Point { + /** + * The coordinate pair of this object + */ private int i; private int j; + /** + * Default constructor initializes both to 0 + */ public Point() { i = 0; j = 0; } - public int getI() { + /** + * Getters and setters for both i and j + */ + int getI() { return i; } - public int getJ() { + int getJ() { return j; } - public void setI(int i) { + void setI(int i) { this.i = i; } - public void setJ(int j) { + void setJ(int j) { this.j = j; } } diff --git a/src/inputFiles/testInput.txt b/src/inputFiles/testInput.txt index 780dd06..4f6cbed 100644 --- a/src/inputFiles/testInput.txt +++ b/src/inputFiles/testInput.txt @@ -1,4 +1,6 @@ -IT Crowd -This should be on a new line -Here's another one -I'm about to eat some dinner \ No newline at end of file +Michael +Here we go Steelers +Pirates/Penguins +@@#$! #@$%^*% +Let's go Pens! +Steelers are number 1 diff --git a/src/plan.txt b/src/plan.txt index 98dc6cf..995ca7a 100644 --- a/src/plan.txt +++ b/src/plan.txt @@ -40,4 +40,7 @@ Steps to implement this: Functions: Point(i, j) int getI() - int getJ() \ No newline at end of file + int getJ() + + +** NOTE: Not all of the above mentioned points were used in the final design, made some changes along the way ** \ No newline at end of file