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
52 changes: 39 additions & 13 deletions src/main/java/org/translation/CountryCodeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
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.Map;

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

// Wrong lexicographical order for 'java.util.HashMap' import (remove this comment once resolved)

/**
* 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

private final JSONArray jsonArray;
/**
* Default constructor which will load the country codes from "country-codes.txt"
* in the resources folder.
*/
@SuppressWarnings("checkstyle:EmptyLineSeparator")
public CountryCodeConverter() {
this("country-codes.txt");
}
Expand All @@ -32,11 +34,21 @@ public CountryCodeConverter() {
public CountryCodeConverter(String filename) {

try {
// pick appropriate instance variable(s) to store the data necessary for this class
List<String> lines = Files.readAllLines(Paths.get(getClass()
.getClassLoader().getResource(filename).toURI()));

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

// use lines to populate the instance variable(s)
this.jsonArray = new JSONArray();
String[] headers = lines.get(0).split("\t");
for (int i = 1; i < lines.size(); i++) {
String[] values = lines.get(i).split("\t");
JSONObject obj = new JSONObject();
for (int j = 0; j < headers.length && j < values.length; j++) {
obj.put(headers[j].trim(), values[j].trim());
}
jsonArray.put(obj);
}
}
catch (IOException | URISyntaxException ex) {
throw new RuntimeException(ex);
Expand All @@ -50,8 +62,15 @@ 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;
// update this code to use an instance variable to return the correct value
String temp = "";
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
if (code.toUpperCase().equals(obj.getString("Alpha-3 code"))) {
temp = obj.getString("Country");
}
}
return temp;
}

/**
Expand All @@ -60,16 +79,23 @@ 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;
// update this code to use an instance variable to return the correct value
String temp = "1";
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
if (country.equals(obj.getString("Country"))) {
temp = obj.getString("Alpha-3 code");
}
}
return temp;
}

/**
* 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;
// update this code to use an instance variable to return the correct value
return jsonArray.length();
}
}
37 changes: 19 additions & 18 deletions src/main/java/org/translation/InLabByHandTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.ArrayList;
import java.util.List;

// TODO Task: modify this class so that it also supports the Spanish language code "es" and
// 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.

Expand All @@ -21,17 +21,19 @@ 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)) {
return new ArrayList<>(List.of("de", "en", "zh"));
// Checkstyle: The String "can" appears 4 times in the file.
if (CANADA.equals(country)) {
return new ArrayList<>(List.of("de", "en", "es", "zh"));
}
return new ArrayList<>();
}

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

/**
* Returns the country abbreviations for all countries whose translations are
Expand All @@ -41,7 +43,7 @@ public List<String> getCountryLanguages(String country) {
*/
@Override
public List<String> getCountries() {
return new ArrayList<>(List.of("can"));
return new ArrayList<>(List.of(CANADA));
}

/**
Expand All @@ -53,22 +55,21 @@ 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")) {
// Checkstyle: Return count is 5 (max allowed for non-void methods/ lambdas is 2).
// Checkstyle: String literal expressions should be on the left side of an equals comparison
if (!country.equals(CANADA)) {
return null;
}
if (language.equals("de")) {
return "Kanada";
String value = null;
if ("de".equals(language)) {
value = "Kanada";
}
else if (language.equals("en")) {
return "Canada";
else if ("en".equals(language)) {
value = "Canada";
}
else if ("zh".equals(language)) {
return "加拿大";
}
else {
return null;
value = "加拿大";
}
return value;
}
}
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 "";
// Complete this method.
JSONObject jsonObject = jsonArray.getJSONObject(1);
return jsonObject.getString("key1");
}

}
22 changes: 17 additions & 5 deletions src/main/java/org/translation/JSONTranslationExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ 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
// 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 @@ -37,8 +38,8 @@ public JSONTranslationExample() {
*/
public String getCanadaCountryNameSpanishTranslation() {

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

Expand All @@ -52,7 +53,18 @@ public String getCanadaCountryNameSpanishTranslation() {
* @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) {
return "Country not found";
int temp = -1;
for (int i = 0; i < jsonArray.length(); i++) {
if (jsonArray.getJSONObject(i).getString("alpha3").equals(countryCode)) {
temp = i;
break;
}
}
if (temp == -1) {
return "Country not found";
}
JSONObject country = jsonArray.getJSONObject(temp);
return country.getString(languageCode);
}

/**
Expand Down
43 changes: 37 additions & 6 deletions src/main/java/org/translation/JSONTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
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
// pick appropriate instance variables for this class
private final JSONArray jsonArray;
private final String codeForCountry = "alpha3";

/**
* Constructs a JSONTranslator using data from the sample.json resources file.
Expand All @@ -35,11 +39,10 @@ public JSONTranslator(String filename) {

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

JSONArray jsonArray = new JSONArray(jsonString);
this.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.

}
catch (IOException | URISyntaxException ex) {
throw new RuntimeException(ex);
Expand All @@ -50,19 +53,47 @@ public JSONTranslator(String filename) {
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<>();
ArrayList<String> temp = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
if (obj.getString(codeForCountry).equals(country)) {
Iterator<String> keys = obj.keys();
int index = 0;
while (keys.hasNext()) {
String key = keys.next();
index++;
if (index < 4) {
continue;
}
temp.add(key);
}
}
}
return temp;
}

@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<>();
ArrayList<String> temp = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
temp.add(obj.getString(codeForCountry));
}
return temp;
}

@Override
public String translate(String country, String language) {
// TODO Task: complete this method using your instance variables as needed
return null;
String temp = null;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
if (country.equals(obj.getString(codeForCountry)) && getCountryLanguages(country).contains(language)) {
temp = obj.getString(language);
}
}
return temp;
}
}
Loading