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

Adding changes for the Main.java file #137

Open
wants to merge 15 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
51 changes: 38 additions & 13 deletions src/main/java/org/translation/CountryCodeConverter.java
Original file line number Diff line number Diff line change
@@ -1,75 +1,100 @@

package org.translation;

import java.io.IOException;
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
private Map<String, String> nameCode = new HashMap<>();
private Map<String, String> codeName = new HashMap<>();

// Define constants for the indices of each field after splitting
private static final int COUNTRY_NAME_INDEX = 0;
private static final int ALPHA3_CODE_INDEX = 2;

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

public CountryCodeConverter() {
this("country-codes.txt");
}

/**
* Overloaded constructor which allows us to specify the filename to load the country code data from.
*
* @param filename the name of the file in the resources folder to load the data from
* @throws RuntimeException if the resource file can't be loaded properly
*/
public CountryCodeConverter(String filename) {

try {
List<String> lines = Files.readAllLines(Paths.get(getClass()
.getClassLoader().getResource(filename).toURI()));

// TODO Task: use lines to populate the instance variable(s)
for (int i = 1; i < lines.size(); i++) {
String line = lines.get(i).trim();

if (line.isEmpty()) {
continue;
}

String[] tokens = line.split("\\t|\\s{2,}");

String countryName = tokens[COUNTRY_NAME_INDEX].trim();
String alpha3Code = tokens[ALPHA3_CODE_INDEX].trim().toUpperCase();

// Populate the maps
nameCode.put(countryName, alpha3Code);
codeName.put(alpha3Code, countryName);
}
}
catch (IOException | URISyntaxException ex) {
throw new RuntimeException(ex);
}

}

/**
* Returns the name of the country for the given country code.
*
* @param code the 3-letter code of the country
* @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;
if (code == null) {
return null;
}
return codeName.get(code.toUpperCase());
}

/**
* Returns the code of the country for the given country name.
*
* @param country the name of the country
* @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;
if (country == null) {
return null;
}
return nameCode.get(country.trim());
}

/**
* 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();
}
}
}
43 changes: 25 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,9 @@
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.
// 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.

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

// Checkstyle: Static variable definition in wrong order.
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", "zh", "es", "hu"));
}
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
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,27 @@ 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 ReturnCountry = null;
if ("de".equals(language)) {
ReturnCountry = "Kanada";
}
else if (language.equals("en")) {
return "Canada";
else if ("en".equals(language)) {
ReturnCountry = "Canada";
}
else if ("zh".equals(language)) {
return "加拿大";
ReturnCountry = "加拿大";
}
else {
return null;
else if ("es".equals(language)) {
ReturnCountry = "el Canadá";
}
else if ("hu".equals(language)) {
ReturnCountry = "Kanada";
}
return ReturnCountry;
}
}
3 changes: 1 addition & 2 deletions src/main/java/org/translation/JSONDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ 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 "";
return jsonArray.getJSONObject(1).getString("key1");
}

}
20 changes: 11 additions & 9 deletions src/main/java/org/translation/JSONTranslationExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ 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
// (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 +35,25 @@ 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 < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if (jsonObject.getString("alpha3").equals(countryCode)) {
return jsonObject.getString(languageCode);
}
}

return "Country not found";
}

Expand Down
52 changes: 44 additions & 8 deletions src/main/java/org/translation/JSONTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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
// Task: pick appropriate instance variables for this class
private static final String ALPHA3_CODE = "alpha3";
private final Map<String, Map<String, String>> countries = new HashMap<>();

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

JSONArray jsonArray = new JSONArray(jsonString);

// TODO Task: use the data in the jsonArray to populate your instance variables
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String country = jsonObject.getString(ALPHA3_CODE);

// Load available translations
Map<String, String> translations = new HashMap<>();

for (String key : jsonObject.keySet()) {
if (!"id".equals(key) && !"alpha2".equals(key) && !ALPHA3_CODE.equals(key)) {
translations.put(key, jsonObject.getString(key));
}
}
countries.put(country, translations);
}
// 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.

}
Expand All @@ -48,21 +67,38 @@ public JSONTranslator(String filename) {

@Override
public List<String> getCountryLanguages(String country) {
// TODO Task: return an appropriate list of language codes,
List<String> languageCodes = new ArrayList<>();
Map<String, String> languages = countries.get(country);
for (String language : languages.keySet()) {
languageCodes.add(language);
}
// Task: return an appropriate list of language codes,
// but make sure there is no aliasing to a mutable object
return new ArrayList<>();
return new ArrayList<>(languageCodes);
}

@Override
public List<String> getCountries() {
// TODO Task: return an appropriate list of country codes,
List<String> countryCodes = new ArrayList<>();
for (String country : countries.keySet()) {
if (!countryCodes.contains(country)) {
countryCodes.add(country);
}
}
// 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
if (countries.containsKey(country)) {
Map<String, String> languages = countries.get(country);
if (languages.containsKey(language)) {
return languages.get(language);
}
}
// Task: complete this method using your instance variables as needed
return null;
}
}
}
Loading