Menu
Coddy logo textTech

Search Functionality

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 104 of 110.

challenge icon

Challenge

Easy

In the previous lesson, you built a Library class with borrowing and returning functionality. Now let's add search capabilities so users can find books in the library's collection.

You'll enhance your Library class with three new search methods that help users discover books:

  • book.dart: Keep your existing Book class unchanged.
  • user.dart: Keep your existing User class unchanged.
  • library.dart: Add three search methods to your Library class:
    • searchByTitle(String query) - returns a List<Book> containing all books whose title contains the query string (case-insensitive)
    • searchByAuthor(String query) - returns a List<Book> containing all books whose author name contains the query string (case-insensitive)
    • getAvailableBooks() - returns a List<Book> containing only books that are currently available for borrowing
  • main.dart: Demonstrate the search functionality. Create a Library and add four books:
    • 978-0-13-468599-1, The Pragmatic Programmer, David Thomas
    • 978-0-596-51774-8, JavaScript: The Good Parts, Douglas Crockford
    • 978-0-13-235088-4, Clean Code, Robert Martin
    • 978-0-20-161622-4, The Pragmatic Programmer, Andrew Hunt
    Register a user (U001, Alice) and have them borrow the first book. Then:
    1. Search for books with pragmatic in the title and print Title search 'pragmatic': X found where X is the count
    2. Search for books by author david and print Author search 'david': X found
    3. Get available books and print Available books: X
    4. Print each available book on its own line

Expected output:

U001 borrowed "The Pragmatic Programmer"
Title search 'pragmatic': 2 found
Author search 'david': 1 found
Available books: 3
[978-0-596-51774-8] JavaScript: The Good Parts by Douglas Crockford
[978-0-13-235088-4] Clean Code by Robert Martin
[978-0-20-161622-4] The Pragmatic Programmer by Andrew Hunt

Try it yourself

import 'book.dart';
import 'user.dart';
import 'library.dart';

void main() {
  // Create a Library
  Library library = Library();
  
  // TODO: Add four books to the library:
  // 1. 978-0-13-468599-1, The Pragmatic Programmer, David Thomas
  // 2. 978-0-596-51774-8, JavaScript: The Good Parts, Douglas Crockford
  // 3. 978-0-13-235088-4, Clean Code, Robert Martin
  // 4. 978-0-20-161622-4, The Pragmatic Programmer, Andrew Hunt
  
  // TODO: Register a user (U001, Alice)
  
  // TODO: Have the user borrow the first book
  
  // TODO: Search for books with 'pragmatic' in the title
  // Print: Title search 'pragmatic': X found
  
  // TODO: Search for books by author 'david'
  // Print: Author search 'david': X found
  
  // TODO: Get available books and print: Available books: X
  
  // TODO: Print each available book on its own line
}

All lessons in Object Oriented Programming