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
8 changes: 8 additions & 0 deletions .idea/.gitignore

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

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

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

13 changes: 13 additions & 0 deletions .idea/compiler.xml

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

7 changes: 7 additions & 0 deletions .idea/encodings.xml

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

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

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

13 changes: 13 additions & 0 deletions .idea/misc.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/vcs.xml

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

6 changes: 6 additions & 0 deletions lab3.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="CheckStyle-IDEA-Module" serialisationVersion="2">
<option name="activeLocationsIds" />
</component>
</module>
34 changes: 24 additions & 10 deletions src/main/java/org/translation/CountryCodeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
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.Iterator;
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
private final Map<String, String> codeName = new HashMap<>();
private final Map<String, String[]> nameCode = new HashMap<>();

/**
* Default constructor which will load the country codes from "country-codes.txt"
Expand All @@ -35,7 +36,23 @@ 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)
Iterator<String> iterator = lines.iterator();
if (iterator.hasNext()) {
iterator.next();
}
while (iterator.hasNext()) {
String line = iterator.next();
if (line.isEmpty()) {
continue;
}
String[] parts = line.split("\t");
String[] codes = new String[] {parts[1], parts[2], parts[parts.length - 1]};
String countryName = parts[0];
nameCode.put(countryName, codes);
for (String code : codes) {
codeName.put(code, countryName);
}
}

}
catch (IOException | URISyntaxException ex) {
Expand All @@ -50,8 +67,7 @@ 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;
return codeName.get(code);
}

/**
Expand All @@ -60,16 +76,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 nameCode.get(country)[1];
}

/**
* 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 nameCode.size();
}
}
71 changes: 39 additions & 32 deletions src/main/java/org/translation/InLabByHandTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,45 @@

// 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.
// support for one additional language 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.

/**
* An implementation of the Translator interface which translates
* the country code "can" to several languages.
* the country code CANADA to several languages.
*/
public class InLabByHandTranslator implements Translator {

public static final String CANADA = "can";

/**
* Returns the country abbreviations for all countries whose translations are
* available from this Translator.
*
* @return list of country abbreviations for which we have translations available
*/
@Override
public List<String> getCountries() {
return new ArrayList<>(List.of(CANADA));
}

/**
* Returns the language abbreviations for all languages whose translations are
* available for the given country.
*
* @param country the country
* @return list of language abbreviations which are available for this country
*/

@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.
*
* @return list of country abbreviations for which we have translations available
*/
@Override
public List<String> getCountries() {
return new ArrayList<>(List.of("can"));
}

/**
* Returns the name of the country based on the specified country abbreviation and language abbreviation.
*
Expand All @@ -53,22 +53,29 @@ 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")) {

if (!CANADA.equals(country)) {
return null;
}
if (language.equals("de")) {
return "Kanada";
}
else if (language.equals("en")) {
return "Canada";
}
else if ("zh".equals(language)) {
return "加拿大";
}
else {
return null;

String result = null;

switch (language) {
case "de":
result = "Kanada";
break;
case "en":
result = "Canada";
break;
case "zh":
result = "加拿大";
break;
case "es":
result = "Canadá";
break;
default:
}

return result;
}
}
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 @@ -33,8 +33,8 @@ 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 "";
JSONObject obj = jsonArray.getJSONObject(1);
return obj.getString("key1");
}

}
18 changes: 10 additions & 8 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 @@ -36,29 +37,30 @@ public JSONTranslationExample() {
* @return the Spanish translation of Canada
*/
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");
}

// 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) {
for (int i = 0; i < this.jsonArray.length(); i++) {
JSONObject obj = this.jsonArray.getJSONObject(i);
if (obj.getString("alpha3").equals(countryCode)) {
return obj.getString(languageCode);
}
}
return "Country not found";
}

/**
* Prints the Spanish translation of Canada.
* @param args not used
*/

public static void main(String[] args) {
JSONTranslationExample jsonTranslationExample = new JSONTranslationExample();

Expand Down
Loading