diff --git a/training-assignments/src/java/eu/sig/training/ch05-interfacing/buildandsendmail/BuildAndSendMail.java b/training-assignments/src/java/eu/sig/training/ch05-interfacing/buildandsendmail/BuildAndSendMail.java index 7d07ca5..85acbef 100755 --- a/training-assignments/src/java/eu/sig/training/ch05-interfacing/buildandsendmail/BuildAndSendMail.java +++ b/training-assignments/src/java/eu/sig/training/ch05-interfacing/buildandsendmail/BuildAndSendMail.java @@ -1,20 +1,50 @@ package eu.sig.training.ch05.buildandsendmail; -public class BuildAndSendMail { - // tag::buildAndSendMail[] - public void buildAndSendMail(MailMan m, String firstName, String lastName, - String division, String subject, MailFont font, String message1, - String message2) { - // Format the email address - String mId = firstName.charAt(0) + "." + lastName.substring(0, 7) + "@" - + division.substring(0, 5) + ".compa.ny"; - // Format the message given the content type and raw message - MailMessage mMessage = formatMessage(font, - message1 + message2); - // Send message - m.send(mId, subject, mMessage); - } - // end::buildAndSendMail[] +public class EmailService { + private final MailMan mailMan; + + public EmailService(MailMan mailMan) { + this.mailMan = mailMan; + } + + public void sendEmail(EmailDetails details, EmailContent content) { + String emailAddress = generateEmailAddress(details); + MailMessage formattedMessage = formatMessage(content); + mailMan.send(emailAddress, details.getSubject(), formattedMessage); + } + + private String generateEmailAddress(EmailDetails details) { + return String.format("%c.%s@%.5s.compa.ny", + details.getFirstName().charAt(0), + details.getLastName().substring(0, Math.min(7, details.getLastName().length())), + details.getDivision()); + } + + private MailMessage formatMessage(EmailContent content) { + return new MailMessage(content.getFont(), content.getCombinedMessage()); + } +} + +class EmailDetails { + private final String firstName; + private final String lastName; + private final String division; + private final String subject; + + // Constructor, getters... +} + +class EmailContent { + private final MailFont font; + private final String message1; + private final String message2; + + // Constructor, getters... + + public String getCombinedMessage() { + return message1 + message2; + } +} @SuppressWarnings("unused") private MailMessage formatMessage(MailFont font, String string) { @@ -36,4 +66,4 @@ private class MailMessage { } -} \ No newline at end of file +}