Exercise 1: Promo Emails
In this example we have multiple users and we want to send a promo email advertising a sale to them (for simplicity's sake, sending an email will just be printing to the console), so long as they have an email on record. If we have the user's first name, then the email will address them by first name. Otherwise we will address them using their username, so long as we have that. Barring that, we will simply address them as "Valued Customer".If a user is null, or a list of users is null, or a user within a list of users is null, then we will throw an exception. (This is a bit contrived, I know, but it allows us to better test out a few things from chapter 6.)
Note that this conversion will primarily focus on converting the sendSalesPromotion and sendSalesPromotions methods and supporting methods. As such I will provide a partial Kotlin conversion that converts everything except these methods and their supporting methods.
import java.util.ArrayList; import java.util.List; public class JavaExample { public static void main(String[] args) { User jsmith432 = new User("jsmith432", "John", "Smith", "john.smith@yahoo.com"); User jdoe = new User("jdoe", null, null, "jdoe@gmail.com"); User unknown = new User(null, null, null, "abc123@hotmail.com"); User bob = new User("bob", "Bob", null, null); List<User> users = new ArrayList<>(); users.add(jsmith432); users.add(jdoe); users.add(unknown); users.add(bob); List<User> usersWithNull = new ArrayList<>(); usersWithNull.add(jsmith432); usersWithNull.add(null); usersWithNull.add(jdoe); try { sendSalesPromotion(null); } catch (Exception e) { System.out.println(e.getMessage() + "\n"); } sendSalesPromotion(jsmith432); sendSalesPromotion(jdoe); sendSalesPromotion(unknown); sendSalesPromotion(bob); try { sendSalesPromotions(null); } catch (Exception e) { System.out.println(e.getMessage() + "\n"); } try { sendSalesPromotions(usersWithNull); } catch (Exception e) { System.out.println(e.getMessage() + "\n"); } sendSalesPromotions(users); } public static void sendSalesPromotions(List<User> users) { if (users != null) { if(users.stream().anyMatch(user -> user == null)) { nullParameterException("user"); } users.forEach(JavaExample::sendSalesPromotion); } else { nullParameterException("users"); } } public static void sendSalesPromotion(User user) { if (user != null) { if (user.getEmailAddress() != null) { String emailAddress = user.getEmailAddress(); String subject = "Blowout XYZ Widget Sale!"; String message = "Dear " + (user.getFirstName() != null ? user.getFirstName() : user.getUsername() != null ? user.getUsername() : "Valued Customer") + "\n\n" + "We're having a massive sale on XYZ Widgets!\n" + "95% Off! Get yours today!"; sendEmail(new Email(emailAddress, subject, message)); } } else { nullParameterException("user"); } } public static void nullParameterException(String paramName) { throw new RuntimeException("Param " + paramName + " is null"); } public static void sendEmail(Email email) { System.out.println("Email sent:\n" + " emailAddress: " + email.getEmailAddress() + "\n" + " subject: " + email.getSubject() + "\n" + " message: " + email.getMessage() + "\n"); } } public class User { private final String username; private final String firstName; private final String lastName; private final String emailAddress; public User(String username, String firstName, String lastName, String emailAddress) { this.username = username; this.firstName = firstName; this.lastName = lastName; this.emailAddress = emailAddress; } public String getUsername() { return username; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public String getEmailAddress() { return emailAddress; } } public class Email { private final String emailAddress; private final String subject; private final String message; public Email(String emailAddress, String subject, String message) { this.emailAddress = emailAddress; this.subject = subject; this.message = message; } public String getEmailAddress() { return emailAddress; } public String getSubject() { return subject; } public String getMessage() { return message; } }And here's the partial Kotlin conversion
import java.lang.Exception fun main() { val jsmith432 = User("jsmith432", "John", "Smith", "john.smith@yahoo.com") val jdoe = User(username = "jdoe", emailAddress = "jdoe@gmail.com") val unknown = User(emailAddress = "abc123@hotmail.com") val bob = User(username = "bob", firstName = "Bob") val users = listOf(jsmith432, jdoe, unknown, bob) val usersWithNull = listOf(jsmith432, null, jdoe) try { sendSalesPromotion(null) } catch (e: Exception) { println("${e.message}\n") } sendSalesPromotion(jsmith432) sendSalesPromotion(jdoe) sendSalesPromotion(unknown) sendSalesPromotion(bob) try { sendSalesPromotions(null) } catch (e: Exception) { println("${e.message}\n") } try { sendSalesPromotions(usersWithNull) } catch (e: Exception) { println("${e.message}\n") } sendSalesPromotions(users) } data class User(val username: String? = null, val firstName: String? = null, val lastName: String? = null, val emailAddress: String? = null) data class Email(val emailAddress: String, val subject: String, val message: String)The answer can be found here.