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 zip

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: Creating Pairs of Book Titles and Prices with zip

Objective: Pair each book title with its price.

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

val titlesWithPrices = pairTitlesWithPrices(library)
titlesWithPrices.foreach { case (title, price) => println(s"$title: $$${price}") }

Use Case 2: Creating Pairs of Book Titles and Their Indexes with zipWithIndex

Objective: Pair each book title with its index in the list.

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

val titlesWithIndexes = pairTitlesWithIndexes(library)
titlesWithIndexes.foreach { case (title, index) => println(s"$index: $title") }

Use Case 3: Matching Authors to Their Books with zip

Objective: Pair each author with their respective book titles.

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

val authorsWithBooks = matchAuthorsToBooks(library)
authorsWithBooks.foreach { case (author, books) =>
  println(s"$author: ${books.mkString(", ")}")
}

Use Case 4: Pairing Book Titles with Publication Years with zip

Objective: Pair each book title with its publication year.

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

val titlesWithYears = pairTitlesWithYears(library)
titlesWithYears.foreach { case (title, year) => println(s"$title: $year") }

Use Case 5: Creating Pairs of Original and Discounted Prices with zip

Objective: Create pairs of original and discounted prices for each book.

def pairOriginalWithDiscountedPrices(library: List[Book], discountRate: Double): List[(Double, Double)] = {
  // TODO
}

val originalWithDiscountedPrices = pairOriginalWithDiscountedPrices(library, 0.10)
originalWithDiscountedPrices.foreach { case (original, discounted) => println(f"Original: $$${original}%.2f, Discounted: $$${discounted}%.2f") }

Use Case 6: Creating a List of Book Titles with Their Indexes for Display Purposes with zipWithIndex

Objective: Pair each book title with its index to create a display list.

def displayBooksWithIndexes(library: List[Book]): List[String] = {
  // TODO
}

val booksWithIndexes = displayBooksWithIndexes(library)
booksWithIndexes.foreach(println)