article banner

Exercise: Blog Post Properties

In your project, you're working with a BlogPost class that represents a blog post, including its title, content, and author details. Your task is to enhance the BlogPost class by adding and implementing the following properties:

  • authorName - a string that combines the author's name and surname. For example, if the author's name is "John" and the surname is "Smith", the value of this property should be "John Smith". You can assume that you will need to use this property many times per blog post object.
  • wordCount - an integer that represents the number of words in the blog post. You can assume that this property is expensive to calculate and you might need it more than once per blog post object
  • isLongRead - a boolean that indicates whether the blog post is longer than 1000 characters. You can assume that you will need to use this property at most once per blog post object.
  • summary - a string that is calculated using the generateSummary function. This object is very heavy to calculate, so you don’t want to calculate it more than once.

You need to decide how to implement each of these properties. Your options are:

  • A val property defined by a value
  • A val property defined by a getter
  • A lazy property

Starting code:

data class BlogPost( val title: String, val content: String, val author: Author, ) { // TODO: Add properties here private fun generateSummary(content: String): String = content.take(100) + "..." } data class Author( val key: String, val name: String, val surname: String, )

This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in advanced/delegates/Lazy.kt. You can find there starting code.

Once you are done with the exercise, you can check your solution here.

Playground

data class BlogPost( val title: String, val content: String, val author: Author, ) { // TODO: Add properties here private fun generateSummary(content: String): String = content.take(100) + "..." } data class Author( val key: String, val name: String, val surname: String, ) fun main() { val post = BlogPost( title = "Hello, World!", content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + "Sed non risus. Suspendisse lectus tortor, dignissim sit amet, " + "adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. " + "Maecenas ligula massa, varius a, semper congue, euismod non, mi. " + "Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, " + "non fermentum diam nisl sit amet erat. Duis semper. Duis arcu massa, " + "scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue. " + "Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum " + "augue. Praesent egestas leo in pede. Praesent blandit odio eu enim. " + "Pellentesque sed dui ut augue blandit sodales. Vestibulum ante ipsum " + "primis in faucibus orci luctus et ultrices posuere cubilia Curae; " + "Aliquam nibh. Mauris ac mauris sed pede pellentesque fermentum. " + "Maecenas adipiscing ante non diam sodales hendrerit.", author = Author( key = "alex", name = "Alex", surname = "Smith", ), ) // println(post.authorName) // // Alex Smith // println(post.wordCount) // // 153 // println(post.isLongRead) // // true // println(post.summary) // // Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi. Proin portt... }