diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..21b4487 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Project exclude paths +/out/ \ No newline at end of file diff --git a/.idea/$PRODUCT_WORKSPACE_FILE$ b/.idea/$PRODUCT_WORKSPACE_FILE$ new file mode 100644 index 0000000..d4d7d1e --- /dev/null +++ b/.idea/$PRODUCT_WORKSPACE_FILE$ @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + 1.8 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml new file mode 100644 index 0000000..034029d --- /dev/null +++ b/.idea/checkstyle-idea.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/libraries/twitter4j_async_4_0_7.xml b/.idea/libraries/twitter4j_async_4_0_7.xml new file mode 100644 index 0000000..d34ae0c --- /dev/null +++ b/.idea/libraries/twitter4j_async_4_0_7.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..0c6a671 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..1d471e0 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,13 @@ +import twitter4j.Status; +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.TwitterFactory; + +public class Main { + public static void main(String[] args) { + + UserInterface ui = new UserInterface(); + ui.start(); + } + +} diff --git a/src/Translator.java b/src/Translator.java new file mode 100644 index 0000000..16e14ae --- /dev/null +++ b/src/Translator.java @@ -0,0 +1,106 @@ +// Takes in an English message and outputs the message in morse code +// each character (0 -> 9, a -> z, !, ., ?, SPACE) is separated 1 space from each other +// each full word is separated 3 spaces from each other + +import java.util.*; + +public class Translator { + private HashMap dictionary; + + public Translator() { + this.dictionary = new HashMap(); + initializeDictionary(); + } + + // REQUIRED: only accepts A -> Z, 0 -> 9, commas, periods, questions marks, exclamation marks and + // spaces + public String encode(String message) { + String messageInMorseCode = ""; + char[] characters = message.toLowerCase().toCharArray(); // array of characters + for (int i = 0; i < message.length(); i++) { + messageInMorseCode = messageInMorseCode + dictionary.get(characters[i]) + " "; + // translate each character in array to morsecode character e -> . for example + } + + return messageInMorseCode; + } + + // REQUIRED: only accepts periods, underscores, single spaces and double spaces + // Recall that each character is separated by a single space, each full word is separated by a double space + public String decode(String message) { + + String messageInEnglish = ""; // this is eventually what we will be returning + List morseCodeWords = Arrays.asList(message.split(" ")); // 2 spaces, array of full words (in morse code format) + List morseCodeCharacters = new ArrayList<>(); // array of morse code characters ex: "._.." is a comma + + + for (int i = 0; i < morseCodeWords.size(); i++) { + String[] arr = (morseCodeWords.get(i).split(" ")); // 1 space, splits each morse code word into morse code character + + + for (int j = 0; j < arr.length; j++) { + morseCodeCharacters.add(arr[j]); + } + } + + // 'a' is key, "._" is the value (for dictionary hash map) + for (String character : morseCodeCharacters) { + for (Character theKey : dictionary.keySet()) { + + if (dictionary.get(theKey).equals(" ")) { + messageInEnglish = messageInEnglish + " "; + + } else if (dictionary.get(theKey).equals(character)) { + messageInEnglish = messageInEnglish + theKey; // adds the English Character + } + } + + } + return messageInEnglish; + } + + private void initializeDictionary() { + dictionary.put('a', "._"); + dictionary.put('b', "_..."); + dictionary.put('c', "_._."); + dictionary.put('d', "_.."); + dictionary.put('e', "."); + dictionary.put('f', ".._."); + dictionary.put('g', "__."); + dictionary.put('h', "...."); + dictionary.put('i', ".."); + dictionary.put('j', ".___"); + dictionary.put('k', "_._"); + dictionary.put('l', "._.."); + dictionary.put('m', "__"); + dictionary.put('n', "_."); + dictionary.put('o', "___"); + dictionary.put('p', ".__."); + dictionary.put('q', "__._"); + dictionary.put('r', "._."); + dictionary.put('s', "..."); + dictionary.put('t', "_"); + dictionary.put('u', ".._"); + dictionary.put('v', "..._"); + dictionary.put('w', ".__"); + dictionary.put('x', "_.._"); + dictionary.put('y', "_.__"); + dictionary.put('z', "__.."); + dictionary.put('0', "_____"); + dictionary.put('1', ".____"); + dictionary.put('2', "..___"); + dictionary.put('3', "...__"); + dictionary.put('4', "...._"); + dictionary.put('5', "....."); + dictionary.put('6', "_...."); + dictionary.put('7', "__..."); + dictionary.put('8', "___.."); + dictionary.put('9', "____."); + dictionary.put('?', "..__.."); + dictionary.put('.', "._._._"); + dictionary.put(',', "__..__"); + dictionary.put('!', "_..._"); + dictionary.put(' ', " "); // empty space + + } +} diff --git a/src/TwitterStuff.java b/src/TwitterStuff.java new file mode 100644 index 0000000..ee17f9f --- /dev/null +++ b/src/TwitterStuff.java @@ -0,0 +1,20 @@ +import twitter4j.Status; +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.TwitterFactory; + + +public class TwitterStuff { + private Twitter myTwitter = TwitterFactory.getSingleton(); + + public void newTweet(String message) { + try { + Status status = myTwitter.updateStatus(message); + System.out.println("Your tweet was successful"); + } catch (TwitterException e) { + e.printStackTrace(); + } + + } +} + diff --git a/src/UserInterface.java b/src/UserInterface.java new file mode 100644 index 0000000..0f4975d --- /dev/null +++ b/src/UserInterface.java @@ -0,0 +1,61 @@ +import java.util.Scanner; + +public class UserInterface { + + private TwitterStuff twit; + private Translator translate; + private Scanner reader; + + public UserInterface() { + twit = new TwitterStuff(); + translate = new Translator(); + reader = new Scanner(System.in); + } + + public void start() { + while (true) { + System.out.println("Please type your message or press 2 if you would like to quit."); + String message = reader.nextLine(); + + if (isStringInt(message) && Integer.parseInt(message) == 2) { + break; + } + + System.out.println("Press 0 if you want to tweet your message" + + "out in morse code. " + + "Press 1 if you want to tweet your message out in English."); + + String ans = reader.nextLine(); + + if (isStringInt(ans)) { + int answerAsInt = Integer.parseInt(ans); + performAction(answerAsInt, message); + } else { + System.out.println("Your response is invalid. Please try again."); + } + + } + } + + public void performAction(int ans, String message) { + if (ans == 0) { + String inMorseCode = translate.encode(message); + twit.newTweet(inMorseCode); + + } else if (ans == 1) { + twit.newTweet(message); + + } else { + System.out.println("Your response is invalid. Please try again."); + } + } + + public boolean isStringInt(String s) { + try { + Integer.parseInt(s); + return true; + } catch (NumberFormatException e) { + return false; + } + } +} diff --git a/twitter-client.iml b/twitter-client.iml new file mode 100644 index 0000000..38dc580 --- /dev/null +++ b/twitter-client.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/twitter4j.properties b/twitter4j.properties new file mode 100644 index 0000000..8a55c32 --- /dev/null +++ b/twitter4j.properties @@ -0,0 +1,5 @@ +debug=true +oauth.consumerKey= +oauth.consumerSecret= +oauth.accessToken= +oauth.accessTokenSecret= \ No newline at end of file