Putting it together
- Dart course
- Intermediate
- Lesson 57/116
- 6 min
Combine the Library class with a menu loop.
Integration
Now it is time to combine all pieces. The Book class, the Library class, addBook, checkout, returnBook and search methods are ready. What remains is to create the library in main, add books, lend one and search.
The running session goes like this: create a library, add three books, lend one, run a search. The output includes both the lent status and the search result.
This shows how an object-oriented app is built piece by piece. Each lesson added a brick; now the building is complete.
Running session
All classes are defined. In main create a library, add three books, lend one and search. The output shows the lent status first, then the search result.
With three books and the query 'and': checkout(0) lends the first book, print(lib.books[0].borrowed) gives true. search('and') finds two books (Crime and Punishment, War and Peace), results.length gives 2.
The project is complete! You used OOP, lists, collection methods and conditions together. You can extend this structure to build bigger library systems.
Practice Working library
All classes are defined (Book, Library, addBook, checkout, returnBook, search). In main create a Library, add three books ('Les Miserables', 'Crime and Punishment', 'War and Peace'), call lib.checkout(0), print lib.books[0].borrowed, run lib.search('and') and print the length of the result.
Starter code
class Book {
String title;
bool borrowed = false;
Book(this.title);
}
class Library {
List<Book> books = [];
void addBook(Book b) {
books.add(b);
}
void checkout(int index) {
books[index].borrowed = true;
}
void returnBook(int index) {
books[index].borrowed = false;
}
List<Book> search(String query) {
return books.where((b) => b.title.contains(query)).toList();
}
}
// in main create a library, add books, call checkout and search
void main() {
// create a library, add three books, call checkout and search
}Sample solution
class Book {
String title;
bool borrowed = false;
Book(this.title);
}
class Library {
List<Book> books = [];
void addBook(Book b) {
books.add(b);
}
void checkout(int index) {
books[index].borrowed = true;
}
void returnBook(int index) {
books[index].borrowed = false;
}
List<Book> search(String query) {
return books.where((b) => b.title.contains(query)).toList();
}
}
void main() {
var lib = Library();
lib.addBook(Book('Les Miserables'));
lib.addBook(Book('Crime and Punishment'));
lib.addBook(Book('War and Peace'));
lib.checkout(0);
print(lib.books[0].borrowed);
var results = lib.search('and');
print(results.length);
}Expected output
true 2
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which order makes sense?
- addBook, checkout, search Correct answer
- checkout, addBook, search
- search, addBook, checkout
- addBook, search, checkout
Explanation: first books are added, then lent, finally searched.
2 Match methods to their purpose
| Term | Meaning |
|---|---|
addBook |
Add a book |
checkout |
Lend out |
returnBook |
Take back |
search |
Search by title |
3 Add the third book
void main() {
var lib = Library();
lib.addBook(Book('Les Miserables'));
lib.addBook(Book('Crime and Punishment'));
____.addBook(Book('War and Peace'));
lib.checkout(0);
print(lib.books[0].borrowed);
var results = lib.search('and');
print(results.length);
}
void main() {
var lib = Library();
lib.addBook(Book('Les Miserables'));
lib.addBook(Book('Crime and Punishment'));
lib.addBook(Book('War and Peace'));
lib.checkout(0);
print(lib.books[0].borrowed);
var results = lib.search('and');
print(results.length);
}
Explanation: lib.addBook appends the third book.