Skip to content
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
37 changes: 27 additions & 10 deletions src/main/java/org/translation/CountryCodeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@
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 {
private static final int COUNTRYCODETEXTLINELENGTH = 4;

// TODO Task: pick appropriate instance variable(s) to store the data necessary for this class
// TO DO Task: pick appropriate instance variable(s) to store the data necessary for this class
private final Map<String, String> codeToCountry = new HashMap<>();
private final Map<String, String> countryToCode = new HashMap<>();
private int numcountries;

/**
* Default constructor which will load the country codes from "country-codes.txt"
* in the resources folder.
*/

public CountryCodeConverter() {
this("country-codes.txt");
}
Expand All @@ -34,9 +38,21 @@ public CountryCodeConverter(String filename) {
try {
List<String> lines = Files.readAllLines(Paths.get(getClass()
.getClassLoader().getResource(filename).toURI()));
// TO DO Task: use lines to populate the instance variable(s)
int i = 1;

// TODO Task: use lines to populate the instance variable(s)
while (i < lines.size()) {
String[] parts = lines.get(i).split("\t");
if (parts.length >= COUNTRYCODETEXTLINELENGTH) {
String countryName = parts[0].trim();
String alpha3 = parts[2].trim();

codeToCountry.put(alpha3, countryName);
countryToCode.put(countryName.toLowerCase(), alpha3);
i += 1;
}
}
this.numcountries = codeToCountry.size();
}
catch (IOException | URISyntaxException ex) {
throw new RuntimeException(ex);
Expand All @@ -50,8 +66,8 @@ 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;
// TO DO Task: update this code to use an instance variable to return the correct value
return codeToCountry.get(code.toUpperCase());
}

/**
Expand All @@ -60,16 +76,17 @@ 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;
// TO DO Task: update this code to use an instance variable to return the correct value
String code = countryToCode.get(country);
return code;
}

/**
* 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;
// TO DO Task: update this code to use an instance variable to return the correct value
return numcountries;
}
}
32 changes: 18 additions & 14 deletions src/main/java/org/translation/InLabByHandTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public class InLabByHandTranslator implements Translator {
* @param country the country
* @return list of language abbreviations which are available for this country
*/
private final String canstring = "can";

@Override
public List<String> getCountryLanguages(String country) {
// TODO Checkstyle: The String "can" appears 4 times in the file.
if ("can".equals(country)) {
if (canstring.equals(country)) {
return new ArrayList<>(List.of("de", "en", "zh"));
}
return new ArrayList<>();
Expand All @@ -41,7 +42,7 @@ public List<String> getCountryLanguages(String country) {
*/
@Override
public List<String> getCountries() {
return new ArrayList<>(List.of("can"));
return new ArrayList<>(List.of(canstring));
}

/**
Expand All @@ -53,22 +54,25 @@ 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 none = "None";
String translation = none;
if ("de".equals(language)) {
translation = "Kanada";
}
if (language.equals("de")) {
return "Kanada";
}
else if (language.equals("en")) {
return "Canada";
else if ("en".equals(language)) {
translation = "Canada";
}
else if ("zh".equals(language)) {
return "加拿大";
translation = "加拿大";
}
else {
if (!canstring.equals(country)) {
translation = none;
}
if (none.equals(translation)) {
return null;
}
else {
return translation;
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/org/translation/JSONDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public static void main(String[] args) {
* @return value of key "key1" from the second object in the given jsonArray
*/
public static String getKeyOneOfSecond(JSONArray jsonArray) {
// TODO: Complete this method.
return "";
// TO DO: Complete this method.
JSONObject x = jsonArray.getJSONObject(1);
return x.getString("key1");
}

}
28 changes: 20 additions & 8 deletions src/main/java/org/translation/JSONTranslationExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@
public class JSONTranslationExample {

public static final int CANADA_INDEX = 30;

// final makes it not magical anymore
private static final int MAGIC_NUMBER = 30;
private final JSONArray jsonArray;

// Note: CheckStyle is configured so that we are allowed to omit javadoc for constructors
public JSONTranslationExample() {
try {
// this next line of code reads in a file from the resources folder as a String,
// which we then create a new JSONArray object from.
// TODO CheckStyle: Line is longer than 120 characters
// TO DO 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 @@ -36,22 +40,30 @@ public JSONTranslationExample() {
* @return the Spanish translation of Canada
*/
public String getCanadaCountryNameSpanishTranslation() {

// TODO Checkstyle: '30' is a magic number.
JSONObject canada = jsonArray.getJSONObject(30);
// TO DO Checkstyle: '30' is a magic number.
// Change this to use the constant defined above.
// You can also use the constant in the getCountryNameTranslation method below
// once you implement it.
JSONObject canada = jsonArray.getJSONObject(MAGIC_NUMBER);
return canada.getString("es");
}

// TODO Task: Complete the method below to generalize the above to get the country name
// for any country code and language code from sample.json.

/**
* Returns the name of the country based on the provided country and language codes.
* @param countryCode the country, as its three-letter code.
* @param languageCode the language to translate to, as its two-letter code.
* @return the translation of country to the given language or "Country not found" if there is no translation.
*/
public String getCountryNameTranslation(String countryCode, String languageCode) {
int i = 0;
while (i < jsonArray.length()) {
var x = jsonArray.getJSONObject(i);
if (x.getString("alpha3").equalsIgnoreCase(countryCode)) {
var y = x.getString(languageCode);
return y;
}
i += 1;
}
return "Country not found";
}

Expand Down
49 changes: 40 additions & 9 deletions src/main/java/org/translation/JSONTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
*/
public class JSONTranslator implements Translator {

// TODO Task: pick appropriate instance variables for this class

// TO DO Task: pick appropriate instance variables for this class
public static final String ALPHATHREE = "alpha3";
private final List<String> countryCodes = new ArrayList<>();
private final List<org.json.JSONObject> countryTranslations = new ArrayList<>();
private final CountryCodeConverter countryCodeConverter = new CountryCodeConverter();
private final LanguageCodeConverter languageCodeConverter = new LanguageCodeConverter();
/**
* Constructs a JSONTranslator using data from the sample.json resources file.
*/

public JSONTranslator() {
this("sample.json");
}
Expand All @@ -33,12 +38,21 @@ public JSONTranslator(String filename) {
// read the file to get the data to populate things...
try {

String jsonString = Files.readString(Paths.get(getClass().getClassLoader().getResource(filename).toURI()));
String jsonString = Files.readString(Paths.get(getClass().getClassLoader()
.getResource(filename).toURI()));

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.
int i = 0;
while (i < jsonArray.length()) {
org.json.JSONObject country = jsonArray.getJSONObject(i);
String alpha3 = country.getString(ALPHATHREE);
countryCodes.add(alpha3);
countryTranslations.add(country);
i += 1;
}

}
catch (IOException | URISyntaxException ex) {
Expand All @@ -48,21 +62,38 @@ public JSONTranslator(String filename) {

@Override
public List<String> getCountryLanguages(String country) {
// TODO Task: return an appropriate list of language codes,
// TO DO Task: return an appropriate list of language codes,
// but make sure there is no aliasing to a mutable object
return new ArrayList<>();
for (org.json.JSONObject obj : countryTranslations) {
if (obj.getString(ALPHATHREE).equalsIgnoreCase(country)) {
List<String> languages = new ArrayList<>();
for (String key : obj.keySet()) {
if (!"alpha2".equals(key) && !key.equals(ALPHATHREE) && !"id".equals(key)) {
languages.add(key);
}
}
return languages;
}
}
return new ArrayList<>(countryCodes);
}

@Override
public List<String> getCountries() {
// TODO Task: return an appropriate list of country codes,
// TO DO Task: return an appropriate list of country codes,
// but make sure there is no aliasing to a mutable object
return new ArrayList<>();
return new ArrayList<>(countryCodes);
}

@Override
public String translate(String country, String language) {
// TODO Task: complete this method using your instance variables as needed
return null;
// TO DO Task: complete this method using your instance variables as needed
for (org.json.JSONObject obj : countryTranslations) {
if (obj.getString("alpha3").equalsIgnoreCase(country)) {
return obj.optString(language, "Translation not available");
}
}
return "Country not found";

}
}
36 changes: 29 additions & 7 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 @@ -14,11 +15,14 @@
public class LanguageCodeConverter {

// TODO Task: pick appropriate instance variables to store the data necessary for this class

private final Map<String, String> codeToLanguage = new HashMap<>();
private final Map<String, String> languageToCode = new HashMap<>();
private int numlanguages;
/**
* Default constructor which will load the language codes from "language-codes.txt"
* in the resources folder.
*/

public LanguageCodeConverter() {
this("language-codes.txt");
}
Expand All @@ -34,11 +38,29 @@ 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
// TO DO Task: use lines to populate the instance variable
// tip: you might find it convenient to create an iterator using lines.iterator()
Iterator<String> iter = lines.iterator();

if (iter.hasNext()) {
iter.next();
}

// TODO Checkstyle: '}' on next line should be alone on a line.
} catch (IOException | URISyntaxException ex) {
while (iter.hasNext()) {
String line = iter.next();
String[] parts = line.split("\t");

if (parts.length >= 2) {
String languageName = parts[0].strip();
String code = parts[1].strip();

codeToLanguage.put(code, languageName);
languageToCode.put(languageName.toLowerCase(), code);
}
}
numlanguages = codeToLanguage.size();
}
catch (IOException | URISyntaxException ex) {
throw new RuntimeException(ex);
}

Expand All @@ -51,7 +73,7 @@ public LanguageCodeConverter(String filename) {
*/
public String fromLanguageCode(String code) {
// TODO Task: update this code to use your instance variable to return the correct value
return code;
return codeToLanguage.getOrDefault(code, "Unknown Language");
}

/**
Expand All @@ -61,7 +83,7 @@ public String fromLanguageCode(String code) {
*/
public String fromLanguage(String language) {
// TODO Task: update this code to use your instance variable to return the correct value
return language;
return languageToCode.getOrDefault(language.toLowerCase(), "Unknown Code");
}

/**
Expand All @@ -70,6 +92,6 @@ public String fromLanguage(String language) {
*/
public int getNumLanguages() {
// TODO Task: update this code to use your instance variable to return the correct value
return 0;
return numlanguages;
}
}
Loading