Search Functionality
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 104 of 110.
Challenge
EasyIn 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 existingBookclass unchanged.user.dart: Keep your existingUserclass unchanged.library.dart: Add three search methods to yourLibraryclass:searchByTitle(String query)- returns aList<Book>containing all books whose title contains the query string (case-insensitive)searchByAuthor(String query)- returns aList<Book>containing all books whose author name contains the query string (case-insensitive)getAvailableBooks()- returns aList<Book>containing only books that are currently available for borrowing
main.dart: Demonstrate the search functionality. Create aLibraryand add four books:978-0-13-468599-1,The Pragmatic Programmer,David Thomas978-0-596-51774-8,JavaScript: The Good Parts,Douglas Crockford978-0-13-235088-4,Clean Code,Robert Martin978-0-20-161622-4,The Pragmatic Programmer,Andrew Hunt
U001,Alice) and have them borrow the first book. Then:- Search for books with
pragmaticin the title and printTitle search 'pragmatic': X foundwhere X is the count - Search for books by author
davidand printAuthor search 'david': X found - Get available books and print
Available books: X - 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 HuntTry 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
1Fundamentals of OOP
External FilesLibraries & ImportsIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsInstance VariablesConstructor BasicsRecap - Simple Calculator4Null Safety
Intro to Null SafetyNullable vs Non-NullableThe ? and ! OperatorsLate Keyword & Null SafetyNull-Aware OperatorsNull Safety in ClassesRecap - User Profile System7Abstract Classes & Interfaces
Abstract ClassesAbstract MethodsInterfaces in DartImplicit InterfacesImplementing vs ExtendingMultiple InterfacesRecap - Shape Calculator10Collections & Generics
List, Set, Map OverviewType-Safe CollectionsGeneric ClassesGeneric MethodsGeneric ConstraintsIterable & IteratorRecap - Generic Storage13Advanced OOP Concepts
Composition vs InheritanceExtension MethodsCallable ClassesSealed Classes (Dart 3)Records (Dart 3)Patterns & Matching (3.0)Enums with Methods16Project: Library Management
Project OverviewBook and User Classes2Constructors in Dart
Default ConstructorNamed ConstructorsInitializer ListsConstant ConstructorsFactory ConstructorsRedirecting ConstructorsRecap - Shape Builder5Encapsulation
Public vs Private MembersThe _ Prefix ConventionLibrary-Level PrivacyGetters & Setters DepthInformation HidingRecap - Student Records8Mixins
Introduction to MixinsCreating MixinsUsing Multiple Mixinson Keyword in MixinsMixin vs InheritanceMixin vs InterfaceRecap - Animal System11Special Methods
toString() OverridehashCode & == OverrideComparable Interfacecall() MethodnoSuchMethod OverrideRecap - Custom Collection14Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern3Class Properties
Instance vs Static MembersFinal & Const FieldsLate VariablesStatic Methods & FieldsGetters and SettersRecap - Bank Account Manager6Inheritance
Basic InheritanceThe super KeywordMethod OverridingThe @override AnnotationThe final Class KeywordConstructors & InheritanceRecap - Employee Hierarchy9Polymorphism
Polymorphism BasicsPolymorphism via InterfacesRuntime Type CheckingThe is & as OperatorsCovariant KeywordRecap - Payment Processor