Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exercises Bookstore GroupBy

The Bookstore

case class Author(name: String, nationality: String)
case class Book(title: String, author: Author, year: Int, category: String, price: Double, description: Option[String])

val authors = List(
  Author("George Orwell", "British"),
  Author("Harper Lee", "American"),
  Author("F. Scott Fitzgerald", "American"),
  Author("Aldous Huxley", "British"),
  Author("Herman Melville", "American"),
  Author("J.D. Salinger", "American"),
  Author("Yuval Noah Harari", "Israeli")
)

val library = List(
  Book("1984", authors(0), 1949, "Dystopian", 15.99, Some("A dystopian social science fiction novel and cautionary tale.")),
  Book("To Kill a Mockingbird", authors(1), 1960, "Fiction", 10.99, Some("A novel about the serious issues of rape and racial inequality.")),
  Book("The Great Gatsby", authors(2), 1925, "Classic", 8.99, Some("A story of the mysteriously wealthy Jay Gatsby and his love for Daisy Buchanan.")),
  Book("Brave New World", authors(3), 1932, "Dystopian", 12.99, None),
  Book("Moby Dick", authors(4), 1851, "Classic", 9.99, Some("The narrative of Captain Ahab's obsessive quest to kill the giant white sperm whale Moby Dick.")),
  Book("The Catcher in the Rye", authors(5), 1951, "Fiction", 14.99, Some("A novel about teenage rebellion and alienation."))
)

Use Case 1: Grouping Books by Category

Objective: Group all books by their category.

def groupBooksByCategory(library: List[Book]): Map[String, List[Book]] = {
  // TODO 
}

val booksByCategory = groupBooksByCategory(library)
booksByCategory.foreach { case (category, books) =>
  println(s"$category:")
  books.foreach(book => println(s"  ${book.title} by ${book.author.name}"))
}

Use Case 2: Grouping Books by Author

Objective: Group all books by their author.

def groupBooksByAuthor(library: List[Book]): Map[String, List[Book]] = {
  // TODO 
}

val booksByAuthor = groupBooksByAuthor(library)
booksByAuthor.foreach { case (author, books) =>
  println(s"$author:")
  books.foreach(book => println(s"  ${book.title} [${book.category}]"))
}

Use Case 3: Grouping Books by Decade

Objective: Group all books by the decade they were published.

def groupBooksByDecade(library: List[Book]): Map[Int, List[Book]] = {
  // TODO 
}

val booksByDecade = groupBooksByDecade(library)
booksByDecade.foreach { case (decade, books) =>
  println(s"$decade:")
  books.foreach(book => println(s"  ${book.title} by ${book.author.name}"))
}

Use Case 4: Grouping Books by Price Range

Objective: Group all books by price range (e.g., <$10, $10-$20, >$20).

def groupBooksByPriceRange(library: List[Book]): Map[String, List[Book]] = {
  // TODO 
}

val booksByPriceRange = groupBooksByPriceRange(library)
booksByPriceRange.foreach { case (priceRange, books) =>
  println(s"$priceRange:")
  books.foreach(book => println(s"  ${book.title} by ${book.author.name} - $$${book.price}"))
}

Use Case 5: Grouping Books by Availability of Description

Objective: Group all books by whether they have a description or not.

def groupBooksByDescription(library: List[Book]): Map[String, List[Book]] = {
  // TODO 
}

val booksByDescription = groupBooksByDescription(library)
booksByDescription.foreach { case (descriptionStatus, books) =>
  println(s"$descriptionStatus:")
  books.foreach(book => println(s"  ${book.title} by ${book.author.name}"))
}