From 91413b2c5c8f8620fc4b3b786ea2257e163e69a2 Mon Sep 17 00:00:00 2001 From: Anh Dang Phuong Date: Mon, 23 Sep 2024 10:01:52 -0400 Subject: [PATCH 1/9] Fix Checkstyle of JSONTranslator --- src/main/java/org/translation/JSONTranslator.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/translation/JSONTranslator.java b/src/main/java/org/translation/JSONTranslator.java index ec58dacd..dd6cc7a3 100644 --- a/src/main/java/org/translation/JSONTranslator.java +++ b/src/main/java/org/translation/JSONTranslator.java @@ -16,6 +16,7 @@ public class JSONTranslator implements Translator { // TODO Task: pick appropriate instance variables for this class + private String id; /** * Constructs a JSONTranslator using data from the sample.json resources file. From b3340efd7d3be672da36269d91d39ed766e0173b Mon Sep 17 00:00:00 2001 From: Anh Dang Phuong Date: Mon, 23 Sep 2024 10:04:05 -0400 Subject: [PATCH 2/9] Modified JSONTranslator --- src/main/java/org/translation/JSONTranslator.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/translation/JSONTranslator.java b/src/main/java/org/translation/JSONTranslator.java index dd6cc7a3..ec58dacd 100644 --- a/src/main/java/org/translation/JSONTranslator.java +++ b/src/main/java/org/translation/JSONTranslator.java @@ -16,7 +16,6 @@ public class JSONTranslator implements Translator { // TODO Task: pick appropriate instance variables for this class - private String id; /** * Constructs a JSONTranslator using data from the sample.json resources file. From 6d64b5436d6174ba0b0cdbac44e2f62a47257bb9 Mon Sep 17 00:00:00 2001 From: mxiimy Date: Mon, 23 Sep 2024 10:58:42 -0400 Subject: [PATCH 3/9] Created hashmap for country code and code country. num countries work, need to figure out how to get country from code and vice versa still --- .../org/translation/CountryCodeConverter.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/translation/CountryCodeConverter.java b/src/main/java/org/translation/CountryCodeConverter.java index 83dfd3e7..5e100481 100644 --- a/src/main/java/org/translation/CountryCodeConverter.java +++ b/src/main/java/org/translation/CountryCodeConverter.java @@ -4,9 +4,8 @@ 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; /** @@ -14,12 +13,13 @@ */ public class CountryCodeConverter { - // TODO Task: pick appropriate instance variable(s) to store the data necessary for this class - + private Map nameCode = new HashMap<>(); + private Map codeName = new HashMap<>(); /** * Default constructor which will load the country codes from "country-codes.txt" * in the resources folder. */ + public CountryCodeConverter() { this("country-codes.txt"); } @@ -35,7 +35,14 @@ public CountryCodeConverter(String filename) { List 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); + String name = line.substring(0, line.length() - 11); + String code = line.substring(line.length() - 7, line.length() - 3); + + nameCode.put(name, code); + codeName.put(code, name); + } } catch (IOException | URISyntaxException ex) { @@ -50,8 +57,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); } /** @@ -60,8 +66,7 @@ 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); } /** @@ -69,7 +74,7 @@ public String fromCountry(String country) { * @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(); } + } From b114590c5c217b52f12fb4f28b96c7ae8c030d9e Mon Sep 17 00:00:00 2001 From: mxiimy Date: Mon, 23 Sep 2024 17:36:25 -0400 Subject: [PATCH 4/9] finally finished debugging :'D --- .../org/translation/CountryCodeConverter.java | 45 ++++++++++++------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/translation/CountryCodeConverter.java b/src/main/java/org/translation/CountryCodeConverter.java index 5e100481..7ffc8e9a 100644 --- a/src/main/java/org/translation/CountryCodeConverter.java +++ b/src/main/java/org/translation/CountryCodeConverter.java @@ -15,66 +15,81 @@ public class CountryCodeConverter { private Map nameCode = new HashMap<>(); private Map 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 lines = Files.readAllLines(Paths.get(getClass() .getClassLoader().getResource(filename).toURI())); for (int i = 1; i < lines.size(); i++) { - String line = lines.get(i); - String name = line.substring(0, line.length() - 11); - String code = line.substring(line.length() - 7, line.length() - 3); + String line = lines.get(i).trim(); - nameCode.put(name, code); - codeName.put(code, name); - } + if (line.isEmpty()) { + continue; + } - } - catch (IOException | URISyntaxException ex) { + 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) { - return codeName.get(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) { - return nameCode.get(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() { return nameCode.size(); } - -} +} \ No newline at end of file From cb041f60f8ec85b75db847929c46050668477b49 Mon Sep 17 00:00:00 2001 From: Anh Dang Phuong Date: Mon, 23 Sep 2024 23:49:53 -0400 Subject: [PATCH 5/9] Update JSONTranslator (almost complete) --- .../java/org/translation/JSONTranslator.java | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/translation/JSONTranslator.java b/src/main/java/org/translation/JSONTranslator.java index ec58dacd..daca0b26 100644 --- a/src/main/java/org/translation/JSONTranslator.java +++ b/src/main/java/org/translation/JSONTranslator.java @@ -5,9 +5,12 @@ 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 @@ -16,7 +19,8 @@ public class JSONTranslator implements Translator { // TODO Task: pick appropriate instance variables for this class - + private final Map> countries = new HashMap<>(); + private static final String ALPHA3_CODE = "alpha3"; /** * Constructs a JSONTranslator using data from the sample.json resources file. */ @@ -37,6 +41,19 @@ public JSONTranslator(String filename) { JSONArray jsonArray = new JSONArray(jsonString); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject jsonObject = jsonArray.getJSONObject(i); + String country = jsonObject.getString(ALPHA3_CODE); + + Map translations = new HashMap<>(); // Load available translations + + for (String key : jsonObject.keySet()) { + if (!key.equals("id") && !key.equals("alpha2") && !key.equals(ALPHA3_CODE)) { + translations.put(key, jsonObject.getString(key)); + } + } + countries.put(country, translations); + } // 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. @@ -48,20 +65,37 @@ public JSONTranslator(String filename) { @Override public List getCountryLanguages(String country) { + List languageCodes = new ArrayList<>(); + Map languages = countries.get(country); + for (String language : languages.keySet()) { + languageCodes.add(language); + } // TODO 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 getCountries() { + List countryCodes = new ArrayList<>(); + for (String country : countries.keySet()) { + if (!countryCodes.contains(country)) { + countryCodes.add(country); + } + } // TODO 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) { + if (countries.containsKey(country)) { + Map languages = countries.get(country); + if (languages.containsKey(language)) { + return languages.get(language); + } + } // TODO Task: complete this method using your instance variables as needed return null; } From fc21bb57278d1e52dfdc0e7199a369e6462183ed Mon Sep 17 00:00:00 2001 From: Anh Dang Phuong Date: Wed, 25 Sep 2024 02:16:12 -0400 Subject: [PATCH 6/9] Fill in the Demo and Example, Fix bug in JSONTranslator --- src/main/java/org/translation/JSONDemo.java | 3 +-- .../translation/JSONTranslationExample.java | 20 ++++++++++--------- .../java/org/translation/JSONTranslator.java | 18 +++++++++-------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/translation/JSONDemo.java b/src/main/java/org/translation/JSONDemo.java index 9614e867..595dbc75 100644 --- a/src/main/java/org/translation/JSONDemo.java +++ b/src/main/java/org/translation/JSONDemo.java @@ -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"); } } diff --git a/src/main/java/org/translation/JSONTranslationExample.java b/src/main/java/org/translation/JSONTranslationExample.java index 1483638c..be5fbc27 100644 --- a/src/main/java/org/translation/JSONTranslationExample.java +++ b/src/main/java/org/translation/JSONTranslationExample.java @@ -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) { @@ -36,15 +35,10 @@ 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. @@ -52,6 +46,14 @@ 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 jsonObject = jsonArray.getJSONObject(i); + if (jsonObject.getString("alpha3").equals(countryCode)) { + return jsonObject.getString(languageCode); + } + } + return "Country not found"; } diff --git a/src/main/java/org/translation/JSONTranslator.java b/src/main/java/org/translation/JSONTranslator.java index daca0b26..231a96ea 100644 --- a/src/main/java/org/translation/JSONTranslator.java +++ b/src/main/java/org/translation/JSONTranslator.java @@ -19,8 +19,9 @@ public class JSONTranslator implements Translator { // TODO Task: pick appropriate instance variables for this class - private final Map> countries = new HashMap<>(); private static final String ALPHA3_CODE = "alpha3"; + private final Map> countries = new HashMap<>(); + /** * Constructs a JSONTranslator using data from the sample.json resources file. */ @@ -45,10 +46,11 @@ public JSONTranslator(String filename) { JSONObject jsonObject = jsonArray.getJSONObject(i); String country = jsonObject.getString(ALPHA3_CODE); - Map translations = new HashMap<>(); // Load available translations + // Load available translations + Map translations = new HashMap<>(); for (String key : jsonObject.keySet()) { - if (!key.equals("id") && !key.equals("alpha2") && !key.equals(ALPHA3_CODE)) { + if (!"id".equals(key) && !"alpha2".equals(key) && !ALPHA3_CODE.equals(key)) { translations.put(key, jsonObject.getString(key)); } } @@ -65,11 +67,11 @@ public JSONTranslator(String filename) { @Override public List getCountryLanguages(String country) { - List languageCodes = new ArrayList<>(); - Map languages = countries.get(country); - for (String language : languages.keySet()) { - languageCodes.add(language); - } + List languageCodes = new ArrayList<>(); + Map languages = countries.get(country); + for (String language : languages.keySet()) { + languageCodes.add(language); + } // TODO Task: return an appropriate list of language codes, // but make sure there is no aliasing to a mutable object return new ArrayList<>(languageCodes); From d6a92db164df3cc348d40d1be6800ae2306e46a1 Mon Sep 17 00:00:00 2001 From: dorcagombar Date: Wed, 25 Sep 2024 18:37:01 -0400 Subject: [PATCH 7/9] languageConverter --- .../translation/LanguageCodeConverter.java | 43 ++++++++++++++----- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/translation/LanguageCodeConverter.java b/src/main/java/org/translation/LanguageCodeConverter.java index 8f3daea8..30a16dcb 100644 --- a/src/main/java/org/translation/LanguageCodeConverter.java +++ b/src/main/java/org/translation/LanguageCodeConverter.java @@ -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; @@ -13,7 +14,11 @@ */ public class LanguageCodeConverter { - // TODO Task: pick appropriate instance variables to store the data necessary for this class + // task 1: pick appropriate instance variables to store the data necessary for this class. + + // We'll store the code of the language as the key of the map, and the name of the country as the value. + + private Map directory = new HashMap<>(); /** * Default constructor which will load the language codes from "language-codes.txt" @@ -34,11 +39,20 @@ public LanguageCodeConverter(String filename) { List 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() + // Task 2: use lines to populate the instance variable + // you might find it convenient to create an iterator using lines.iterator() + + Iterator iterate = lines.iterator(); + iterate.next(); + while (iterate.hasNext()) { + String line = iterate.next(); + int lastIndex = line.lastIndexOf("\t"); + this.directory.put(line.substring(lastIndex + 1), line.substring(0, lastIndex)); + } + } + // The } needs to be alone in this line due to the custom configuration. - // TODO Checkstyle: '}' on next line should be alone on a line. - } catch (IOException | URISyntaxException ex) { + catch (IOException | URISyntaxException ex) { throw new RuntimeException(ex); } @@ -50,8 +64,8 @@ public LanguageCodeConverter(String filename) { * @return the name of the language corresponding to the code */ public String fromLanguageCode(String code) { - // TODO Task: update this code to use your instance variable to return the correct value - return code; + // Task 3: update this code to use your instance variable to return the correct value + return this.directory.get(code); } /** @@ -59,9 +73,17 @@ public String fromLanguageCode(String code) { * @param language the name of the language * @return the 2-letter code of the language */ + public String fromLanguage(String language) { - // TODO Task: update this code to use your instance variable to return the correct value - return language; + // Task 4: update this code to use your instance variable to return the correct value + String countryCode = ""; + for (Map.Entry entry : this.directory.entrySet()) { + if (entry.getValue().equals(language)) { + countryCode = entry.getKey(); + break; + } + } + return countryCode; } /** @@ -69,7 +91,6 @@ public String fromLanguage(String language) { * @return how many languages are included in this code converter. */ public int getNumLanguages() { - // TODO Task: update this code to use your instance variable to return the correct value - return 0; + return this.directory.size(); } } From 2b14fc2761abaf5962159a32a16939018a4f9d1e Mon Sep 17 00:00:00 2001 From: dorcagombar Date: Thu, 26 Sep 2024 20:01:31 -0400 Subject: [PATCH 8/9] push --- .../org/translation/CountryCodeConverter.java | 9 +++++++-- .../java/org/translation/JSONTranslator.java | 12 ++++++------ .../org/translation/LanguageCodeConverter.class | Bin 0 -> 3054 bytes 3 files changed, 13 insertions(+), 8 deletions(-) create mode 100644 target/classes/org/translation/LanguageCodeConverter.class diff --git a/src/main/java/org/translation/CountryCodeConverter.java b/src/main/java/org/translation/CountryCodeConverter.java index 7ffc8e9a..2cf5c702 100644 --- a/src/main/java/org/translation/CountryCodeConverter.java +++ b/src/main/java/org/translation/CountryCodeConverter.java @@ -1,3 +1,4 @@ + package org.translation; import java.io.IOException; @@ -24,6 +25,7 @@ public class CountryCodeConverter { * Default constructor which will load the country codes from "country-codes.txt" * in the resources folder. */ + public CountryCodeConverter() { this("country-codes.txt"); } @@ -55,7 +57,8 @@ public CountryCodeConverter(String filename) { nameCode.put(countryName, alpha3Code); codeName.put(alpha3Code, countryName); } - } catch (IOException | URISyntaxException ex) { + } + catch (IOException | URISyntaxException ex) { throw new RuntimeException(ex); } } @@ -80,7 +83,9 @@ public String fromCountryCode(String code) { * @return the 3-letter code of the country */ public String fromCountry(String country) { - if (country == null) return null; + if (country == null) { + return null; + } return nameCode.get(country.trim()); } diff --git a/src/main/java/org/translation/JSONTranslator.java b/src/main/java/org/translation/JSONTranslator.java index 231a96ea..a69e38f9 100644 --- a/src/main/java/org/translation/JSONTranslator.java +++ b/src/main/java/org/translation/JSONTranslator.java @@ -18,7 +18,7 @@ */ 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> countries = new HashMap<>(); @@ -56,7 +56,7 @@ public JSONTranslator(String filename) { } countries.put(country, translations); } - // TODO Task: use the data in the jsonArray to populate your instance variables + // 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. } @@ -72,7 +72,7 @@ public List getCountryLanguages(String country) { for (String language : languages.keySet()) { languageCodes.add(language); } - // TODO Task: return an appropriate list of language codes, + // Task: return an appropriate list of language codes, // but make sure there is no aliasing to a mutable object return new ArrayList<>(languageCodes); } @@ -85,7 +85,7 @@ public List getCountries() { countryCodes.add(country); } } - // TODO Task: return an appropriate list of country codes, + // Task: return an appropriate list of country codes, // but make sure there is no aliasing to a mutable object return new ArrayList<>(countryCodes); } @@ -98,7 +98,7 @@ public String translate(String country, String language) { return languages.get(language); } } - // TODO Task: complete this method using your instance variables as needed + // Task: complete this method using your instance variables as needed return null; } -} +} \ No newline at end of file diff --git a/target/classes/org/translation/LanguageCodeConverter.class b/target/classes/org/translation/LanguageCodeConverter.class new file mode 100644 index 0000000000000000000000000000000000000000..6b4affaabec68ab69e7034a5cb1a2165dc3e0a70 GIT binary patch literal 3054 zcma)8X;Tze6g`hw8itllG2$8x3IaNAxM55bMMnov5Qru*Hqb)r&;va^3Yx|2`@Wie z=Sx1ssstofCHa_2RX*hBBvpwy?{)XkC|0FvYP#Qh_nvp}x#zyuzyJHw&j5B{vKU1I zRT;w`${R!GhJoy$ncL`&y9q=Q(-7C8p;%yT))`8=j$!9AhHGW*WZL&=r9*4hzF<18 z=?KL4ShnTv6{xOHpEE8PNr{~7b{)$eYFXc#Kmw&2bcwn^pkmgfv;Ukq;0i?Q*Y|2D z7pPQgdDqG$4;#7Rc4IVwGL)7ut|}c1u}Gj~&~oUMbtVMLz3r4eZCm6{i*?+EB?84m zrrVk^ayfy8_3J}%sY#kH#WD@cb*#WjBFzTnJCz0g>8vp*v#hxt#aklQ-B_(*jgEUz zOBQay3IIxp*k$Ijd1t^BSTi42UYNG&CVRTl#9fCv4fQ(Kqd`Ddxc;I*%+2<6rO0Ox zm$#ClTN5^D*r+3kP0ZeNv$DyvR>n*oGu&Zv5@9d`wSEwRtIv}iHYVN{Y}K$$M>DqH zhSx?=;^~fQ4DQQh(v~fU>jL08NItx(SK7+CEv49rT^e@lXu%$VG8JlJnMJfHFoVSw zSRACOmQ(&b5%%If4fpHVhy4`wEJ9!+5Vg&5Cc7v=Q1bM&r~D7-IEXd@ZP>_lC{qgZ zl!n7|#Omp}7)Jy|NdkwE*3pg*fl{i>P1%Fyc<0$W^gv45AJcIhT~u%`-=9;>A+1wk zZTdYUtxrhn9+`J)K9f@P;QA>|`$*{TY)dhD`3Pl<&~2lEOusC*opQ0+5d9b!0F??U=UfOms_`R|b_=+Tj$- zo|VEQQz7QKpn+2JI!4)&%-Vwzrxaswb>yYUF&O@N2%U9x@v*iw^3%Xrn;5>z)xYO`KAQ+|4LVV2ktdku{hpx<9#OxCm z7IT5asJxSTs)ixRq>^-jkU$ikLM0(G1bLXjM_Qei0*-Rc&TXFt0=SeWx!uu&?58Ki96bkv2 z;VH!U&d&t?Kh2qD&athbHB^adM6M~LXZWAs0S$LKJ%tB16bH z=Pox)qom;m%1%zB;zv|AOrd%THH}kPHHEd;(Rdx3+nZ*vL!e{B4DJ=^Wi3RO9aOPywH2QzJ z#$d}af>Au=KTk7%nZ|xp@V}O$YOd6gi+Uzh$C(Xi#717cN#2Z`(1y+E5@#2Y zcJMseiA!9S=^tjoZS;BpFA}R0%kUCjX5zI-;T62f{TsR0Yj_=QUs5(Zy%{}1U@d{{Yje`t&UU;w@b(woD-OV z?G;Q_YEw|DFOl zck{cThvfFE7#nE24y*79KII$ZC`5Y%pQ&ggybnI-TaltsYSOd+g7%WUazy??mEvvh zmcMBx@FlMJbZz5P5|Z$8draUvj>1%(4oN-XZ(fA2l-1YxM*0$AjMvJy@sj@l&Xo5j literal 0 HcmV?d00001 From 63c92af432d29fcc092d7a638d4bda3170e8ae95 Mon Sep 17 00:00:00 2001 From: dorcagombar Date: Sat, 28 Sep 2024 13:28:02 -0400 Subject: [PATCH 9/9] push --- .../translation/InLabByHandTranslator.java | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/translation/InLabByHandTranslator.java b/src/main/java/org/translation/InLabByHandTranslator.java index bcf55adf..671553d1 100644 --- a/src/main/java/org/translation/InLabByHandTranslator.java +++ b/src/main/java/org/translation/InLabByHandTranslator.java @@ -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. @@ -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 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 @@ -41,7 +43,7 @@ public List getCountryLanguages(String country) { */ @Override public List getCountries() { - return new ArrayList<>(List.of("can")); + return new ArrayList<>(List.of(CANADA)); } /** @@ -53,22 +55,27 @@ public List 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; } }