Question: Hi, I am trying to make a program in JAVA that takes in the Name, Sex, Age and/or Country of a `Person` and outputs the
Hi, I am trying to make a program in JAVA that takes in the Name, Sex, Age and/or Country of a `Person` and outputs the correct `Name` from the `Registry`, when queried with any of the mentioned traits. If the trait has a special character (i.e. !#$%&'()*+,-./:;<>?@[]^_`{|}~0123456789 [see isValidName method]), then the output should be "Error".
For example,
------------------------------------------
Testcase # 1:
-
Input cases: 3
-
Name=Rob Sex=Man Age=42 Country=US
-
Name=Peter Sex=Man Age=66 Country=RU
-
Name=Pete Sex=Man Age=66 Country=US=&@!#
-
-
Required output criteria: Age=42 Country=US
Therefore, the program should return `Error` as Testcase # 3 has special characters in the country name (Country=US=&@!#), however I am getting the output as `Rob`, which would be correct if none of the entries has any special characters. I have made the method isValidName to check if any of the traits has special characters, however, I am not sure where to insert it in the overall program code.
------------------------------------------
Testcase # 2:
-
Input cases: 3
-
Name=Rob Sex=Man Age=42 Country=US
-
Name=Pete Sex=Man Age=6 Country=US
-
Name=Xiaoling Sex=Woman Age=26 Country=CH
-
-
Required output criteria: Age=6 Country=US
Therefore, the program should return a `Person` of `Age=6` and `Country=US`, which is `Pete`. This works fine.
------------------------------------------
Can you please just highlight where to insert the isValidName method to ensure the names do not contain any special characters?
------------------------------------------
My code:
private static class Person { private Map Traits = new TreeMap<>(); } private static Person ConvertRawInputToPerson(String rawInput) { Person person = new Person(); String[] traits = rawInput.split(" "); for (String trait : traits) { String[] keyValuePair = trait.split("="); String key = keyValuePair[0]; String value = keyValuePair[1]; person.Traits.put(key, value); } return person; } private static boolean StringEquals(String s1, String s2) { return s1.equals(s2); } private static boolean HasTrait(Map traits, Map.Entry soughtTrait) { for (Map.Entry trait : traits.entrySet()) { if (StringEquals(trait.getKey(), soughtTrait.getKey()) && StringEquals(trait.getValue(), soughtTrait.getValue())) { return true; } } return false; } static class Registry { List mPersons = new ArrayList<>(); void add(Person person) { mPersons.add(person); } Person findMatchingPersonByRawTraits(String rawTraits) { Person soughtPerson = new Person(); String[] traits = rawTraits.split(" "); int soughtTraitCount = 0; for (String trait : traits) { String[] keyValuePair = trait.split("="); String key = keyValuePair[0]; String value = keyValuePair[1]; soughtPerson.Traits.put(key, value); soughtTraitCount++; } for (Person person : mPersons) { int count = 0; for (Map.Entry soughtTrait : soughtPerson.Traits.entrySet()) { if (HasTrait(person.Traits, soughtTrait)) { count++; } } if (count == soughtTraitCount) { return person; } } Person unknown = new Person(); unknown.Traits.put("Name", "Unknown"); return unknown; } } private static String GetNameFromRegistryByTraits(String[] rawRegistry, String soughtTraits) { Registry registry = new Registry(); for (String rawInput : rawRegistry) { registry.add(ConvertRawInputToPerson(rawInput)); } Person person = registry.findMatchingPersonByRawTraits(soughtTraits); return person.Traits.get("Name"); } private static boolean isValidName(String name) { String specialCharacters = "!#$%&'()*+,-./:;<>?@[]^_`{|}~0123456789"; String[] arr = name.split(""); int count = 0; for (String s : arr) { if (specialCharacters.contains(s)) { count++; } } return count == 0; } ------------------------------------------
Thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
