Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven committed May 28, 2020
0 parents commit 0844ad1
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Project exclude paths
/out/
41 changes: 41 additions & 0 deletions .idea/$PRODUCT_WORKSPACE_FILE$

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/checkstyle-idea.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/libraries/twitter4j_async_4_0_7.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
106 changes: 106 additions & 0 deletions src/Translator.java
Original file line number Diff line number Diff line change
@@ -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<Character, String> dictionary;

public Translator() {
this.dictionary = new HashMap<Character, String>();
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<String> morseCodeWords = Arrays.asList(message.split(" ")); // 2 spaces, array of full words (in morse code format)
List<String> 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

}
}
20 changes: 20 additions & 0 deletions src/TwitterStuff.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
}

61 changes: 61 additions & 0 deletions src/UserInterface.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
12 changes: 12 additions & 0 deletions twitter-client.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="twitter4j-async-4.0.7" level="project" />
</component>
</module>
5 changes: 5 additions & 0 deletions twitter4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
debug=true
oauth.consumerKey=
oauth.consumerSecret=
oauth.accessToken=
oauth.accessTokenSecret=

0 comments on commit 0844ad1

Please sign in to comment.