Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Template {
- private final String companyName;
- private final Set<String> templates = new HashSet<>();
- public Template(String companyName, String... templates) {
- this.companyName = companyName;
- Arrays.stream(templates).forEach(this.templates::add);
- }
- public List<String> formatFor(String name) {
- List<String> emailAddresses = new ArrayList<>();
- for(String template : templates) {
- String[] firstAndLastNameOfTemplate = parseFirstAndLastNameFromTemplate(template);
- if (firstAndLastNameOfTemplate.length == 1) {
- emailAddresses.add(formatForOnlyFirstName(name, template));
- }
- else if (firstAndLastNameOfTemplate.length == 2){
- emailAddresses.add(formatFirstAndSecondName(name, template));
- }
- else {
- throw new RuntimeException("Unknown format for template");
- }
- }
- return emailAddresses;
- }
- private String formatFirstAndSecondName(String name, String template) {
- String firstName = name.split(" ")[0];
- String lastName = name.split(" ")[1];
- String domain = getDomainFrom(template);
- if (templateUsesFirstLetterOfFirstName(template) && templateUsesFirstLetterOfLastName(template)) {
- return firstName.charAt(0) + "." + lastName.charAt(0) + "@" + domain;
- }
- else if (templateUsesFirstLetterOfFirstName(template)) {
- return firstName.charAt(0) + "." + lastName + "@" + domain;
- }
- else if (templateUsesFirstLetterOfLastName(template)) {
- return firstName + "." + lastName.charAt(0) + "@" + domain;
- }
- else {
- return firstName + "." + lastName + "@" + domain;
- }
- }
- private String[] parseFirstAndLastNameFromTemplate(String template) {
- String namePartOfTemplate = template.split("@")[0];
- return namePartOfTemplate.split("\\.");
- }
- private boolean templateUsesFirstLetterOfFirstName(String template) {
- String[] firstAndLastNameOfTemplate = parseFirstAndLastNameFromTemplate(template);
- return firstAndLastNameOfTemplate[0].length() == 1;
- }
- private boolean templateUsesFirstLetterOfLastName(String template) {
- String[] firstAndLastNameOfTemplate = parseFirstAndLastNameFromTemplate(template);
- return firstAndLastNameOfTemplate[1].length() == 1;
- }
- private String formatForOnlyFirstName(String name, String template) {
- //only first name
- String firstName = name.split(" ")[0];
- String domain = getDomainFrom(template);
- return firstName + "@" + domain;
- }
- private String getDomainFrom(String template) {
- String[] emailAddress = template.split("@");
- return emailAddress[1];
- }
- }
- import org.junit.Test;
- import java.util.List;
- import static org.hamcrest.MatcherAssert.assertThat;
- import static org.hamcrest.core.IsCollectionContaining.hasItems;
- import static org.hamcrest.core.IsEqual.equalTo;
- public class TemplateTest {
- private final String name = "phil cummins";
- @Test
- public void given_a_template_with_first_name_name_into_email_address() {
- Template template = new Template("Alphasights", "david@alphasights.com");
- List<String> emailAddresses = template.formatFor(name);
- assertThat(emailAddresses.size(), equalTo(1));
- assertThat(emailAddresses.get(0), equalTo("phil@alphasights.com"));
- }
- @Test
- public void given_a_template_with_first_and_second_name_into_email_address() {
- Template template = new Template("Stripe", "patrick.collison@stripe.com");
- List<String> emailAddresses = template.formatFor(name);
- assertThat(emailAddresses.get(0), equalTo("phil.cummins@stripe.com"));
- }
- @Test
- public void given_a_template_with_one_letter_of_first_name_and_last_name_into_email_address() {
- Template template = new Template("Apple", "s.jobs@apple.com");
- List<String> emailAddresses = template.formatFor(name);
- assertThat(emailAddresses.get(0), equalTo("p.cummins@apple.com"));
- }
- @Test
- public void given_a_template_with_first_name_and_one_letter_of_last_name_into_email_address() {
- Template template = new Template("Google", "larry.p@google.com");
- List<String> emailAddresses = template.formatFor(name);
- assertThat(emailAddresses.get(0), equalTo("phil.c@google.com"));
- }
- @Test
- public void given_a_template_with_first_letter_of_both_first_and_last_name_into_email_addresses() {
- Template template = new Template("Heroku", "a.b@heroku.com");
- List<String> emailAddresses = template.formatFor(name);
- assertThat(emailAddresses.get(0), equalTo("p.c@heroku.com"));
- }
- @Test
- public void given_multiple_templates_into_email_addresses() {
- Template template = new Template("Google", "s.bring@google.com", "larry.p@google.com");
- List<String> emailAddresses = template.formatFor(name);
- assertThat(emailAddresses, hasItems("p.cummins@google.com", "phil.c@google.com"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement