Advertisement
Guest User

Untitled

a guest
Sep 10th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Template {
  4. private final String companyName;
  5. private final Set<String> templates = new HashSet<>();
  6.  
  7. public Template(String companyName, String... templates) {
  8. this.companyName = companyName;
  9. Arrays.stream(templates).forEach(this.templates::add);
  10. }
  11.  
  12. public List<String> formatFor(String name) {
  13. List<String> emailAddresses = new ArrayList<>();
  14.  
  15. for(String template : templates) {
  16. String[] firstAndLastNameOfTemplate = parseFirstAndLastNameFromTemplate(template);
  17.  
  18. if (firstAndLastNameOfTemplate.length == 1) {
  19. emailAddresses.add(formatForOnlyFirstName(name, template));
  20. }
  21. else if (firstAndLastNameOfTemplate.length == 2){
  22. emailAddresses.add(formatFirstAndSecondName(name, template));
  23. }
  24. else {
  25. throw new RuntimeException("Unknown format for template");
  26. }
  27. }
  28.  
  29. return emailAddresses;
  30. }
  31.  
  32. private String formatFirstAndSecondName(String name, String template) {
  33. String firstName = name.split(" ")[0];
  34. String lastName = name.split(" ")[1];
  35. String domain = getDomainFrom(template);
  36.  
  37.  
  38. if (templateUsesFirstLetterOfFirstName(template) && templateUsesFirstLetterOfLastName(template)) {
  39. return firstName.charAt(0) + "." + lastName.charAt(0) + "@" + domain;
  40. }
  41. else if (templateUsesFirstLetterOfFirstName(template)) {
  42. return firstName.charAt(0) + "." + lastName + "@" + domain;
  43. }
  44. else if (templateUsesFirstLetterOfLastName(template)) {
  45. return firstName + "." + lastName.charAt(0) + "@" + domain;
  46. }
  47. else {
  48. return firstName + "." + lastName + "@" + domain;
  49. }
  50. }
  51.  
  52. private String[] parseFirstAndLastNameFromTemplate(String template) {
  53. String namePartOfTemplate = template.split("@")[0];
  54. return namePartOfTemplate.split("\\.");
  55. }
  56.  
  57. private boolean templateUsesFirstLetterOfFirstName(String template) {
  58. String[] firstAndLastNameOfTemplate = parseFirstAndLastNameFromTemplate(template);
  59. return firstAndLastNameOfTemplate[0].length() == 1;
  60. }
  61.  
  62. private boolean templateUsesFirstLetterOfLastName(String template) {
  63. String[] firstAndLastNameOfTemplate = parseFirstAndLastNameFromTemplate(template);
  64. return firstAndLastNameOfTemplate[1].length() == 1;
  65. }
  66.  
  67. private String formatForOnlyFirstName(String name, String template) {
  68. //only first name
  69. String firstName = name.split(" ")[0];
  70. String domain = getDomainFrom(template);
  71. return firstName + "@" + domain;
  72. }
  73.  
  74. private String getDomainFrom(String template) {
  75. String[] emailAddress = template.split("@");
  76. return emailAddress[1];
  77. }
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. import org.junit.Test;
  92.  
  93. import java.util.List;
  94.  
  95. import static org.hamcrest.MatcherAssert.assertThat;
  96. import static org.hamcrest.core.IsCollectionContaining.hasItems;
  97. import static org.hamcrest.core.IsEqual.equalTo;
  98.  
  99. public class TemplateTest {
  100.  
  101. private final String name = "phil cummins";
  102.  
  103. @Test
  104. public void given_a_template_with_first_name_name_into_email_address() {
  105. Template template = new Template("Alphasights", "david@alphasights.com");
  106.  
  107. List<String> emailAddresses = template.formatFor(name);
  108. assertThat(emailAddresses.size(), equalTo(1));
  109. assertThat(emailAddresses.get(0), equalTo("phil@alphasights.com"));
  110. }
  111.  
  112. @Test
  113. public void given_a_template_with_first_and_second_name_into_email_address() {
  114. Template template = new Template("Stripe", "patrick.collison@stripe.com");
  115.  
  116. List<String> emailAddresses = template.formatFor(name);
  117. assertThat(emailAddresses.get(0), equalTo("phil.cummins@stripe.com"));
  118. }
  119.  
  120. @Test
  121. public void given_a_template_with_one_letter_of_first_name_and_last_name_into_email_address() {
  122. Template template = new Template("Apple", "s.jobs@apple.com");
  123.  
  124. List<String> emailAddresses = template.formatFor(name);
  125. assertThat(emailAddresses.get(0), equalTo("p.cummins@apple.com"));
  126. }
  127.  
  128. @Test
  129. public void given_a_template_with_first_name_and_one_letter_of_last_name_into_email_address() {
  130. Template template = new Template("Google", "larry.p@google.com");
  131.  
  132. List<String> emailAddresses = template.formatFor(name);
  133. assertThat(emailAddresses.get(0), equalTo("phil.c@google.com"));
  134. }
  135.  
  136. @Test
  137. public void given_a_template_with_first_letter_of_both_first_and_last_name_into_email_addresses() {
  138. Template template = new Template("Heroku", "a.b@heroku.com");
  139.  
  140. List<String> emailAddresses = template.formatFor(name);
  141. assertThat(emailAddresses.get(0), equalTo("p.c@heroku.com"));
  142. }
  143.  
  144. @Test
  145. public void given_multiple_templates_into_email_addresses() {
  146. Template template = new Template("Google", "s.bring@google.com", "larry.p@google.com");
  147. List<String> emailAddresses = template.formatFor(name);
  148. assertThat(emailAddresses, hasItems("p.cummins@google.com", "phil.c@google.com"));
  149. }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement