Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

victoria #132

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/main/java/org/translation/CountryCodeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
// TODO CheckStyle: Wrong lexicographical order for 'java.util.HashMap' import (remove this comment once resolved)
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* This class provides the service of converting country codes to their names.
*/
public class CountryCodeConverter {

// TODO Task: pick appropriate instance variable(s) to store the data necessary for this class
public static Map<String, String> codeMap = new HashMap<>();

/**
* Default constructor which will load the country codes from "country-codes.txt"
Expand All @@ -35,8 +34,10 @@ public CountryCodeConverter(String filename) {
List<String> lines = Files.readAllLines(Paths.get(getClass()
.getClassLoader().getResource(filename).toURI()));

// TODO Task: use lines to populate the instance variable(s)

for (String line : lines) {
String[] countryCodes = line.split("\t");
codeMap.put(countryCodes[0].trim(), countryCodes[2].trim());
}
}
catch (IOException | URISyntaxException ex) {
throw new RuntimeException(ex);
Expand All @@ -50,8 +51,12 @@ public CountryCodeConverter(String filename) {
* @return the name of the country corresponding to the code
*/
public String fromCountryCode(String code) {
// TODO Task: update this code to use an instance variable to return the correct value
return code;
for (Map.Entry<String, String> entry : codeMap.entrySet()) {
if (entry.getValue().equalsIgnoreCase(code)) {
return entry.getKey();
}
}
return null;
}

/**
Expand All @@ -60,16 +65,14 @@ public String fromCountryCode(String code) {
* @return the 3-letter code of the country
*/
public String fromCountry(String country) {
// TODO Task: update this code to use an instance variable to return the correct value
return country;
return codeMap.get(country).toLowerCase();
}

/**
* Returns how many countries are included in this code converter.
* @return how many countries are included in this code converter.
*/
public int getNumCountries() {
// TODO Task: update this code to use an instance variable to return the correct value
return 0;
return codeMap.size() - 1;
}
}
46 changes: 28 additions & 18 deletions src/main/java/org/translation/InLabByHandTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import java.util.ArrayList;
import java.util.List;

// TODO Task: modify this class so that it also supports the Spanish language code "es" and
// one more language code of your choice. Each member of your group should add
// support for one additional langauge code on a branch; then push and create a pull request on GitHub.

// Extra Task: if your group has extra time, you can add support for another country code in this class.

Expand All @@ -21,18 +18,16 @@ public class InLabByHandTranslator implements Translator {
* @param country the country
* @return list of language abbreviations which are available for this country
*/
public static final String CANADA = "can";

@Override
public List<String> getCountryLanguages(String country) {
// TODO Checkstyle: The String "can" appears 4 times in the file.
if ("can".equals(country)) {
if (CANADA.equals(country)) {
return new ArrayList<>(List.of("de", "en", "zh"));
}
return new ArrayList<>();
}

// TODO Checkstyle: Static variable definition in wrong order.
public static final String CANADA = "can";

/**
* Returns the country abbreviations for all countries whose translations are
* available from this Translator.
Expand All @@ -53,22 +48,37 @@ public List<String> getCountries() {
*/
@Override
public String translate(String country, String language) {
// TODO Checkstyle: Return count is 5 (max allowed for non-void methods/ lambdas is 2).
// TODO Checkstyle: String literal expressions should be on the left side of an equals comparison
if (!country.equals("can")) {
return null;
String thenReturn;
if (!country.equals(CANADA)) {
thenReturn = null;
}
if (language.equals("de")) {
return "Kanada";
if ("de".equals(language)) {
thenReturn = "Kanada";
}
else if (language.equals("en")) {
return "Canada";
else if ("en".equals(language)) {
thenReturn = "Canada";
}
else if ("zh".equals(language)) {
return "加拿大";
thenReturn = "加拿大";
}
else if ("es".equals(language)) {
thenReturn = "Canadá";
}
else if ("ja".equals(language)) {
thenReturn = "カナダ";
}
else if ("ko".equals(language)) {
thenReturn = "캐나다";
}
else if ("hy".equals(language)) {
thenReturn = "Կանադա";
}
else if ("it".equals(language)) {
thenReturn = "Canada";
}
else {
return null;
thenReturn = null;
}
return thenReturn;
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/translation/JSONDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void main(String[] args) {
*/
public static String getKeyOneOfSecond(JSONArray jsonArray) {
// TODO: Complete this method.
return "";
JSONObject second = jsonArray.getJSONObject(1);
return second.getString("key1");
}

}
5 changes: 3 additions & 2 deletions src/main/java/org/translation/JSONTranslationExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public JSONTranslationExample() {
// which we then create a new JSONArray object from.
// TODO CheckStyle: Line is longer than 120 characters
// (note: you can split a line such that the next line starts with a .method()... call
String jsonString = Files.readString(Paths.get(getClass().getClassLoader().getResource("sample.json").toURI()));
String jsonString = Files.readString(Paths.get(getClass()
.getClassLoader().getResource("sample.json").toURI()));
this.jsonArray = new JSONArray(jsonString);
}
catch (IOException | URISyntaxException ex) {
Expand All @@ -38,7 +39,7 @@ public JSONTranslationExample() {
public String getCanadaCountryNameSpanishTranslation() {

// TODO Checkstyle: '30' is a magic number.
JSONObject canada = jsonArray.getJSONObject(30);
JSONObject canada = jsonArray.getJSONObject(CANADA_INDEX);
return canada.getString("es");
}

Expand Down
46 changes: 35 additions & 11 deletions src/main/java/org/translation/JSONTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

/**
* An implementation of the Translator interface which reads in the translation
* data from a JSON file. The data is read in once each time an instance of this class is constructed.
*/
public class JSONTranslator implements Translator {

// TODO Task: pick appropriate instance variables for this class
private static JSONTranslator instance; // Singleton instance
private ArrayList<JSONObject> countryArray = new ArrayList<>(); // Instance variable

// private static ArrayList<JSONObject> countryArray = new ArrayList<>();

/**
* Constructs a JSONTranslator using data from the sample.json resources file.
Expand All @@ -37,9 +41,9 @@ public JSONTranslator(String filename) {

JSONArray jsonArray = new JSONArray(jsonString);

// TODO Task: use the data in the jsonArray to populate your instance variables
// Note: this will likely be one of the most substantial pieces of code you write in this lab.

for (int i = 0; i < jsonArray.length(); i++) {
countryArray.add(jsonArray.getJSONObject(i));
}
}
catch (IOException | URISyntaxException ex) {
throw new RuntimeException(ex);
Expand All @@ -48,21 +52,41 @@ public JSONTranslator(String filename) {

@Override
public List<String> getCountryLanguages(String country) {
// TODO Task: return an appropriate list of language codes,
// but make sure there is no aliasing to a mutable object
return new ArrayList<>();
List<String> countryLanguages = new ArrayList<>();
for (int j = 0; j < countryArray.size(); j++) {
JSONObject countryObject = countryArray.get(j);
if (countryObject.getString("alpha3").equals(country)) {
JSONArray keys = countryObject.names();
for (int i = 0; i < keys.length(); i++) {
String key = keys.getString(i);
countryLanguages.add(key);
}
break;
}
}
countryLanguages.remove("alpha2");
countryLanguages.remove("alpha3");
countryLanguages.remove("id");
return countryLanguages;
}

@Override
public List<String> getCountries() {
// TODO Task: return an appropriate list of country codes,
// but make sure there is no aliasing to a mutable object
return new ArrayList<>();
List<String> countryCodes = new ArrayList<>();
for (JSONObject country : countryArray) {
String countryCode = country.getString("alpha3");
countryCodes.add(countryCode);
}
return countryCodes;
}

@Override
public String translate(String country, String language) {
// TODO Task: complete this method using your instance variables as needed
for (JSONObject obj : countryArray) {
if (country.equals(obj.getString("alpha3"))) {
return obj.getString(language);
}
}
return null;
}
}
31 changes: 18 additions & 13 deletions src/main/java/org/translation/LanguageCodeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand All @@ -13,7 +14,7 @@
*/
public class LanguageCodeConverter {

// TODO Task: pick appropriate instance variables to store the data necessary for this class
public static Map<String, String> languageCode = new HashMap<>();

/**
* Default constructor which will load the language codes from "language-codes.txt"
Expand All @@ -34,14 +35,16 @@ public LanguageCodeConverter(String filename) {
List<String> lines = Files.readAllLines(Paths.get(getClass()
.getClassLoader().getResource(filename).toURI()));

// TODO Task: use lines to populate the instance variable
// tip: you might find it convenient to create an iterator using lines.iterator()

// TODO Checkstyle: '}' on next line should be alone on a line.
} catch (IOException | URISyntaxException ex) {
Iterator<String> languageIterator = lines.iterator();
languageIterator.next();
while (languageIterator.hasNext()) {
String[] language = languageIterator.next().split("\t");
languageCode.put(language[1].trim(), language[0].trim());
}
}
catch (IOException | URISyntaxException ex) {
throw new RuntimeException(ex);
}

}

/**
Expand All @@ -50,8 +53,7 @@ public LanguageCodeConverter(String filename) {
* @return the name of the language corresponding to the code
*/
public String fromLanguageCode(String code) {
// TODO Task: update this code to use your instance variable to return the correct value
return code;
return languageCode.get(code);
}

/**
Expand All @@ -60,16 +62,19 @@ public String fromLanguageCode(String code) {
* @return the 2-letter code of the language
*/
public String fromLanguage(String language) {
// TODO Task: update this code to use your instance variable to return the correct value
return language;
for (Map.Entry<String, String> entry : languageCode.entrySet()) {
if (entry.getValue().equalsIgnoreCase(language)) {
return entry.getKey().toLowerCase();
}
}
return null;
}

/**
* Returns how many languages are included in this code converter.
* @return how many languages are included in this code converter.
*/
public int getNumLanguages() {
// TODO Task: update this code to use your instance variable to return the correct value
return 0;
return languageCode.size();
}
}
Loading