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 more

Use Case 1: Partition Books by Availability of Description

Objective: Partition books into two lists: those with a description and those without.

def partitionBooksByDescription(library: List[Book]): (List[Book], List[Book]) = {
  // TODO
}

val (booksWithDescription, booksWithoutDescription) = partitionBooksByDescription(library)
println("Books with Description:")
booksWithDescription.foreach(book => println(s"${book.title} by ${book.author.name}"))

println("\nBooks without Description:")
booksWithoutDescription.foreach(book => println(s"${book.title} by ${book.author.name}"))

Use Case 2: Find the Most Expensive Book

Objective: Find the most expensive book in the library.

def findMostExpensiveBook(library: List[Book]): Option[Book] = {
  // TODO
}

val mostExpensiveBook = findMostExpensiveBook(library)
mostExpensiveBook match {
  case Some(book) => println(s"Most expensive book: ${book.title} by ${book.author.name} - $$${book.price}")
  case None => println("No books in the library")
}

Use Case 3: Get Distinct Authors

Objective: Get a list of distinct authors in the library.

def distinctAuthors(library: List[Book]): List[Author] = {
  // TODO
}

val authorsList = distinctAuthors(library)
println("Distinct Authors:")
authorsList.foreach(author => println(s"${author.name} (${author.nationality})"))

Use Case 4: Flatten Nested List of Book Lists

Objective: Flatten a list of lists of books into a single list of books.

val moreBooks = List(
  List(Book("Sapiens", authors(6), 2011, "Non-Fiction", 19.99, Some("A brief history of humankind."))),
  List(Book("Homo Deus", authors(6), 2015, "Non-Fiction", 21.99, Some("A brief history of tomorrow."))),
  List(Book("Brave New World", authors(3), 1932, "Dystopian", 12.99, None))
)

def flattenBookLists(bookLists: List[List[Book]]): List[Book] = {
  // TODO
}

val allBooks = flattenBookLists(moreBooks)
println("All Books:")
allBooks.foreach(book => println(s"${book.title} by ${book.author.name}"))

Use Case 5: Extract Titles of Books by a Specific Author Using flatMap

Objective: Extract the titles of books written by a specific author using flatMap.

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

val orwellTitles = titlesByAuthor(library, "George Orwell")
println("Titles by George Orwell:")
orwellTitles.foreach(println)

Use Case 6: Calculate Total Price of Books Using foldLeft

Objective: Calculate the total price of all books in the library using foldLeft.

def totalPrice(library: List[Book]): Double = {
  // TODO
}

val totalCost = totalPrice(library)
println(f"Total cost of all books: $$${totalCost}%.2f")

Use Case 7: Combine Books and Authors into a Map

Objective: Create a map of authors to their books.

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

val authorBooksMap = mapAuthorsToBooks(library)
authorBooksMap.foreach { case (author, books) =>
  println(s"${author.name}'s books:")
  books.foreach(book => println(s"  ${book.title}"))
}