Dart Project: Library
A library project is a small program that stores books and tracks borrowers. It combines a class, a list, a map, and functions in one program.
The Book class
The Book class stores a title and an author. The main function builds one book and prints both fields.
class Book {
String title;
String author;
Book(this.title, this.author);
}
void main() {
var book = Book('Dune', 'Frank Herbert');
print(book.title);
print(book.author);
}A list of books and a count
The list holds two Book values. countBooks returns the length of the list, and the print shows 2.
class Book {
String title;
String author;
Book(this.title, this.author);
}
int countBooks(List<Book> books) {
return books.length;
}
void main() {
var books = [
Book('Dune', 'Frank Herbert'),
Book('Emma', 'Jane Austen'),
];
print(countBooks(books));
}Looking up a borrower
borrowerOf reads the map with the title as the key. Emma has no entry, so the fallback On shelf comes back.
String borrowerOf(Map<String, String> borrowers, String title) {
return borrowers[title] ?? 'On shelf';
}
void main() {
var borrowers = {'Dune': 'Ana'};
print(borrowerOf(borrowers, 'Dune'));
print(borrowerOf(borrowers, 'Emma'));
}Printing each book in a loop
printBook prints one book as a single line. The loop sends each book in the list to printBook.
class Book {
String title;
String author;
Book(this.title, this.author);
}
void printBook(Book book) {
print('${book.title} by ${book.author}');
}
void main() {
var books = [
Book('Dune', 'Frank Herbert'),
Book('Emma', 'Jane Austen'),
];
for (var book in books) {
printBook(book);
}
}Example
class Book {
String title;
String author;
Book(this.title, this.author);
}
void printBook(Book book) {
print('${book.title} by ${book.author}');
}
int countBooks(List<Book> books) {
return books.length;
}
String borrowerOf(Map<String, String> borrowers, String title) {
return borrowers[title] ?? 'On shelf';
}
void main() {
var books = [
Book('Dune', 'Frank Herbert'),
Book('Emma', 'Jane Austen'),
Book('The Hobbit', 'J. R. R. Tolkien'),
];
var borrowers = {'Dune': 'Ana', 'Emma': 'Bo'};
for (var book in books) {
printBook(book);
}
print('Books: ${countBooks(books)}');
print('Dune borrower: ${borrowerOf(borrowers, 'Dune')}');
print('The Hobbit borrower: ${borrowerOf(borrowers, 'The Hobbit')}');
}Key points
- The Book class holds the title and the author of one book.
- A list of Book values holds the whole collection.
- A map links each borrowed title to the name of its borrower.
- Functions do the printing, counting, and lookup work.
- Use ?? to give a title with no borrower a fallback label.
- books.length gives the count of books in the list.
In this example, the list holds three Book values and the loop prints each book in list order. countBooks returns 3, and that count prints after the book lines. borrowerOf finds Ana for Dune in the map. The Hobbit has no map entry, so borrowerOf returns the fallback On shelf.
Note
A missing map key gives null in Dart, and ?? turns that null into a plain label.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท DartPad
Exercise
A title is missing from the borrower map. What does borrowers[title] ?? 'On shelf' return for that title?