Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPiotr Stefański
3,080 PointsCode review - asking for opinion, some updates
Hi Community,
I am just after Local Development Environments course and I wanted to little bit check my knowledge. I am to be honest happy with my current progress but I was just wondering, could you look at my code and review it, looking for some better ways to solve my problem, or proposing some better solution. I am beginner and I wanted to summarize my current knowledge.
Below app named Wordix, will be used by me in order to learn new english words during my job or treehouse courses.
I wanted to use MVC model, but I am not sure am I on a level of understanding JAVA in order to start using this design pattern.
package pl.stefanjrklb;
public class Word {
private String mForeignWord;
private String mTranslation;
private String mExampleOfUse;
public Word(String foreignWord, String translation, String exampleOfUse){
mForeignWord = foreignWord;
mTranslation = translation;
mExampleOfUse = exampleOfUse;
}
public String getForeignWord(){
return mForeignWord;
}
public void setForeignWord(String foreignWord){
mForeignWord = foreignWord;
}
public String getTranslation(){
return mTranslation;
}
public void setTranslation(String translation){
mTranslation = translation;
}
public String getExampleOfUse(){
return mExampleOfUse;
}
public void setExampleOfUse(String exampleOfUse){
mExampleOfUse = exampleOfUse;
}
@Override
public String toString(){
return mForeignWord + " - " + mTranslation + " - " + mExampleOfUse;
}
}
package pl.stefanjrklb;
import java.util.List;
import java.util.ArrayList;
public class Dictionary {
private List<Word> mWords;
public Dictionary(){
mWords = new ArrayList<Word>();
}
public void addWords(String foreignWord, String translation, String exampleOfUse){
mWords.add(new Word(foreignWord, translation,exampleOfUse));
}
public List<Word> getWords(){
return mWords;
}
}
package pl.stefanjrklb;
import java.util.Map;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
public class WordixController {
private Dictionary mDictionary;
private Map<Integer,String> mMenuOptions;
private BufferedReader mReader;
public WordixController(){
mDictionary = new Dictionary();
mMenuOptions = new HashMap<Integer,String>();
mReader = new BufferedReader(new InputStreamReader(System.in));
initializeMenuOptions();
}
private void initializeMenuOptions(){
mMenuOptions.put(1,"Add foreign word");
mMenuOptions.put(2, "Review all words");
mMenuOptions.put(3, "Exit wordix");
}
public void startApplication(){
byte optionChoosed=0;
importFromFile();
do {
for(Map.Entry<Integer, String> item : mMenuOptions.entrySet()){
System.out.println(item.getKey() + " - " + item.getValue());
}
System.out.println("\nPlease choose your option");
try {
optionChoosed = Byte.parseByte(mReader.readLine());
} catch (IOException ioe){
ioe.printStackTrace();
} catch (NumberFormatException nfe) {
System.out.println("Please choose number values associated with correct option!!!\n");
}
switch (optionChoosed){
case 1: addWordToList();
break;
case 2: reviewAll();
break;
case 3: break;
default: System.out.println("\nPlease choose number values associated with correct option!!!\n");
}
} while (optionChoosed != 3);
exportToFile();
}
public void addWordToList(){
String foreignWord;
String translation;
String exampleOfUse;
try{
System.out.println("Please provide below information in order to add word");
System.out.print("Foreign word: ");
foreignWord = mReader.readLine();
System.out.print("Translation: ");
translation = mReader.readLine();
System.out.print("Example of use: ");
exampleOfUse = mReader.readLine();
mDictionary.addWords(foreignWord, translation, exampleOfUse);
} catch (IOException ioe){
ioe.printStackTrace();
}
}
public void reviewAll(){
System.out.printf("Number of words currently in a dictionary: %s\n\n",mDictionary.getWords().size());
for(Word word: mDictionary.getWords()){
System.out.println(word.toString());
}
System.out.print("\n");
}
public void exportToFile(){
try (
FileOutputStream fos = new FileOutputStream("wordix.txt");
PrintWriter writer = new PrintWriter(fos);
) {
for(Word word: mDictionary.getWords()){
writer.printf("%s,%s,%s",word.getForeignWord()
,word.getTranslation()
,word.getExampleOfUse());
}
} catch (FileNotFoundException fnf){
fnf.printStackTrace();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
public void importFromFile(){
try(
FileInputStream fis = new FileInputStream("wordix.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
){
String line;
while ((line = reader.readLine())!=null){
String[] wordArray = line.split(",");
mDictionary.addWords(wordArray[0], wordArray[1], wordArray[2]);
}
} catch (FileNotFoundException fnf){
fnf.printStackTrace();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
}
package pl.stefanjrklb;
public class Wordix {
public static void main(String[] args){
WordixController wordixController = new WordixController();
wordixController.startApplication();
}
}
Thank you for your review, support and everything!
Piotr Stefański
3,080 PointsPiotr Stefański
3,080 PointsMy next plan in improving this app is to add way to edit and delete words from dictionary.