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

Zihuang #154

Open
wants to merge 3 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
26 changes: 18 additions & 8 deletions src/main/java/org/translation/CountryCodeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@
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 final int INDEX = 4;
// TODO Task
private final Map<String, String> codeToCountryMap;
private final Map<String, String> countryToCodeMap;
/**
* Default constructor which will load the country codes from "country-codes.txt"
* in the resources folder.
*/

public CountryCodeConverter() {
this("country-codes.txt");
}
Expand All @@ -30,13 +31,22 @@ public CountryCodeConverter() {
* @throws RuntimeException if the resource file can't be loaded properly
*/
public CountryCodeConverter(String filename) {
codeToCountryMap = new HashMap<>();
countryToCodeMap = new HashMap<>();

try {
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[] parts = line.split("\t");
if (parts.length == INDEX) {
codeToCountryMap.put(parts[2].toLowerCase(), parts[0]);
countryToCodeMap.put(parts[0], parts[2].toLowerCase());
}
}
}
catch (IOException | URISyntaxException ex) {
throw new RuntimeException(ex);
Expand All @@ -51,7 +61,7 @@ public CountryCodeConverter(String filename) {
*/
public String fromCountryCode(String code) {
// TODO Task: update this code to use an instance variable to return the correct value
return code;
return codeToCountryMap.get(code);
}

/**
Expand All @@ -61,7 +71,7 @@ public String fromCountryCode(String code) {
*/
public String fromCountry(String country) {
// TODO Task: update this code to use an instance variable to return the correct value
return country;
return countryToCodeMap.get(country);
}

/**
Expand All @@ -70,6 +80,6 @@ public String fromCountry(String country) {
*/
public int getNumCountries() {
// TODO Task: update this code to use an instance variable to return the correct value
return 0;
return codeToCountryMap.size() - 1;
}
}
28 changes: 12 additions & 16 deletions src/main/java/org/translation/InLabByHandTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@
* the country code "can" to several languages.
*/
public class InLabByHandTranslator implements Translator {
public static final String CANADA = "can";
/**
* 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
Expand All @@ -51,24 +52,19 @@ public List<String> getCountries() {
* @param language the language
* @return the name of the country in the given language or null if no translation is available
*/

@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;
}
if (language.equals("de")) {
return "Kanada";
}
else if (language.equals("en")) {
return "Canada";
}
else if ("zh".equals(language)) {
return "加拿大";
}
else {
if (!CANADA.equals(country)) {
return null;
}
return switch (language) {
case "de" -> "Kanada";
case "en" -> "Canada";
case "zh" -> "加拿大";
default -> null;
};
}
}
}
14 changes: 12 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 All @@ -52,6 +53,15 @@ 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) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject country = jsonArray.getJSONObject(i);
String code = country.getString("alpha3").toUpperCase();
if (code.equals(countryCode.toUpperCase())) {
if (country.has(languageCode)) {
return country.getString(languageCode);
}
}
}
return "Country not found";
}

Expand Down
24 changes: 16 additions & 8 deletions src/main/java/org/translation/LanguageCodeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
* This class provides the service of converting language codes to their names.
*/
public class LanguageCodeConverter {

private Map<String, String> codeToLanguageMap;
private Map<String, String> languageToCodeMap;
// TODO Task: pick appropriate instance variables to store the data necessary for this class

/**
Expand All @@ -29,19 +30,26 @@ public LanguageCodeConverter() {
* @throws RuntimeException if the resource file can't be loaded properly
*/
public LanguageCodeConverter(String filename) {
codeToLanguageMap = new HashMap<>();
languageToCodeMap = new HashMap<>();

try {
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) {
for (String line : lines) {
String[] parts = line.split("\t");
if (parts.length == 2) {
codeToLanguageMap.put(parts[1], parts[0]);
languageToCodeMap.put(parts[0], parts[1]);
}
}
}
catch (IOException | URISyntaxException ex) {
throw new RuntimeException(ex);
}

}

/**
Expand All @@ -51,7 +59,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 codeToLanguageMap.get(code);
}

/**
Expand All @@ -61,7 +69,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 languageToCodeMap.get(language);
}

/**
Expand All @@ -70,6 +78,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 codeToLanguageMap.size() - 1 ;
}
}
53 changes: 39 additions & 14 deletions src/main/java/org/translation/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.translation;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

Expand All @@ -13,12 +15,13 @@
* - at any time, the user can type quit to quit the program<br/>
*/
public class Main {

private static final String QUIT_COMMAND = "quit";
/**
* This is the main entry point of our Translation System!<br/>
* A class implementing the Translator interface is created and passed into a call to runProgram.
* @param args not used by the program
*/

public static void main(String[] args) {

// TODO Task: once you finish the JSONTranslator,
Expand All @@ -39,16 +42,14 @@ public static void main(String[] args) {
public static void runProgram(Translator translator) {
while (true) {
String country = promptForCountry(translator);
// TODO CheckStyle: The String "quit" appears 3 times in the file.
// TODO Checkstyle: String literal expressions should be on the left side of an equals comparison
if (country.equals("quit")) {
if (QUIT_COMMAND.equals(country)) {
break;
}
// TODO Task: Once you switch promptForCountry so that it returns the country
// name rather than the 3-letter country code, you will need to
// convert it back to its 3-letter country code when calling promptForLanguage
String language = promptForLanguage(translator, country);
if (language.equals("quit")) {
if (QUIT_COMMAND.equals(language)) {
break;
}
// TODO Task: Once you switch promptForLanguage so that it returns the language
Expand All @@ -61,21 +62,32 @@ public static void runProgram(Translator translator) {
Scanner s = new Scanner(System.in);
String textTyped = s.nextLine();

if ("quit".equals(textTyped)) {
if (QUIT_COMMAND.equals(textTyped)) {
break;
}
}
}

// Note: CheckStyle is configured so that we don't need javadoc for private methods
private static String promptForCountry(Translator translator) {
// Get the list of countries
List<String> countries = translator.getCountries();
// TODO Task: replace the following println call, sort the countries alphabetically,
// and print them out; one per line
// hint: class Collections provides a static sort method
// TODO Task: convert the country codes to the actual country names before sorting
System.out.println(countries);

// Create an instance of CountryCodeConverter
CountryCodeConverter converter = new CountryCodeConverter();

// Convert country codes to actual country names using fromCountryCode
List<String> countryNames = new ArrayList<>();
for (String code : countries) {
// Need instance of converter for fromCountryCode
countryNames.add(converter.fromCountryCode(code));
}

Collections.sort(countryNames);
// Print every country in list countryNames
for (String country: countryNames) {
System.out.println(country);
}
System.out.println("select a country from above:");

Scanner s = new Scanner(System.in);
Expand All @@ -86,12 +98,25 @@ private static String promptForCountry(Translator translator) {
// Note: CheckStyle is configured so that we don't need javadoc for private methods
private static String promptForLanguage(Translator translator, String country) {

// TODO Task: replace the line below so that we sort the languages alphabetically and print them out; one per line
// TODO Task: convert the language codes to the actual language names before sorting
System.out.println(translator.getCountryLanguages(country));
List<String> languageCodes = translator.getCountryLanguages(country);
List<String> languageNames = new ArrayList<>();
LanguageCodeConverter converter = new LanguageCodeConverter();
// Convert language codes to actual language names
for (String code : languageCodes) {
languageNames.add(converter.fromLanguageCode(code));
}

// Sort the language names alphabetically
Collections.sort(languageNames);

// Print each language name on a new line
for (String language : languageNames) {
System.out.println(language);
}

System.out.println("select a language from above:");

// Get the user's choice of language
Scanner s = new Scanner(System.in);
return s.nextLine();
}
Expand Down