Exercises Bookstore filter map
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: Finding Books by a Specific Author
Objective: Find all books written by a specific author.
def findBooksByAuthor(library: List[Book], authorName: String): List[Book] = {
// TODO
}
val booksByOrwell = findBooksByAuthor(library, "George Orwell")
println(s"Books by George Orwell: ${booksByOrwell.map(_.title)}")
Use Case 2: Filtering Books Based on Price
Objective: Filter books that are within a certain price range.
def filterBooksByPrice(library: List[Book], minPrice: Double, maxPrice: Double): List[Book] = {
// TODO
}
val affordableBooks = filterBooksByPrice(library, 10.0, 15.0)
println(s"Affordable books: ${affordableBooks.map(_.title)}")
Use Case 3: Filtering Books by Category
Objective: Filter books belonging to a specific category.
def filterBooksByCategory(library: List[Book], category: String): List[Book] = {
// TODO
}
val dystopianBooks = filterBooksByCategory(library, "Dystopian")
println(s"Dystopian books: ${dystopianBooks.map(_.title)}")
Use Case 4: Displaying Book Titles with Prices
Objective: Create a display list of book titles along with their prices.
def displayBookTitlesWithPrices(library: List[Book]): List[String] = {
// TODO
}
val bookTitlesWithPrices = displayBookTitlesWithPrices(library)
bookTitlesWithPrices.foreach(println)
Use Case 5: Finding Books Published After a Certain Year
Objective: Find all books published after a specified year.
def findBooksPublishedAfter(library: List[Book], year: Int): List[Book] = {
// TODO
}
val modernBooks = findBooksPublishedAfter(library, 1950)
println(s"Books published after 1950: ${modernBooks.map(_.title)}")
Use Case 6: Creating a Summary Description for Books
Objective: Create a summary description for each book, combining title, author, and category.
def createBookSummaries(library: List[Book]): List[String] = {
// TODO
}
val bookSummaries = createBookSummaries(library)
bookSummaries.foreach(println)
Use Case 7: Finding Discounted Books
Objective: Apply a discount to all books and return the new prices.
def applyDiscount(library: List[Book], discountRate: Double): List[(String, Double)] = {
// TODO
}
val discountedBooks = applyDiscount(library, 0.10)
discountedBooks.foreach { case (title, price) => println(s"$title - $price") }