Borrowing System
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 103 of 110.
Challenge
EasyIn the previous lesson, you created Book and User classes with basic properties. Now it's time to build a proper borrowing system with a Library class that manages the entire borrowing process.
You'll create a new file and update your existing files to implement borrowing logic:
book.dart: Keep your existingBookclass withisbn,title,author,isAvailable, andtoString().user.dart: Keep your existingUserclass withid,name,borrowedBooks, andtoString().library.dart: Create a newLibraryclass that manages books and users. It should have two lists: one forbooksand one forusers. Include methods to:addBook(Book book)- adds a book to the library's collectionregisterUser(User user)- adds a user to the libraryborrowBook(String isbn, String userId)- finds the book and user, then if the book exists, is available, and the user exists, marks the book as unavailable, adds it to the user's borrowed books, and printsuserId borrowed "title". If the book doesn't exist, printBook not found. If the book isn't available, printBook not available. If the user doesn't exist, printUser not found.returnBook(String isbn, String userId)- finds the book and user, then if both exist and the user has that book, marks the book as available, removes it from the user's borrowed books, and printsuserId returned "title". If the book doesn't exist, printBook not found. If the user doesn't exist, printUser not found.
main.dart: Import all your files and demonstrate the borrowing system. Create aLibrary, add two books (978-0-13-468599-1,The Pragmatic Programmer,David Thomasand978-0-596-51774-8,JavaScript: The Good Parts,Douglas Crockford), and register one user (U001,Alice Johnson). Then:- Borrow the first book for Alice
- Try to borrow the same book again (should fail)
- Borrow the second book for Alice
- Print Alice to show her borrowed count
- Return the first book
- Print Alice again to show the updated count
Expected output:
U001 borrowed "The Pragmatic Programmer"
Book not available
U001 borrowed "JavaScript: The Good Parts"
User U001: Alice Johnson (2 books borrowed)
U001 returned "The Pragmatic Programmer"
User U001: Alice Johnson (1 books borrowed)Try it yourself
import 'book.dart';
import 'user.dart';
import 'library.dart';
void main() {
// TODO: Create a Library instance
// TODO: Create two books:
// - Book 1: isbn '978-0-13-468599-1', title 'The Pragmatic Programmer', author 'David Thomas'
// - Book 2: isbn '978-0-596-51774-8', title 'JavaScript: The Good Parts', author 'Douglas Crockford'
// TODO: Add both books to the library
// TODO: Create a user with id 'U001' and name 'Alice Johnson'
// TODO: Register the user with the library
// TODO: Borrow the first book for Alice
// TODO: Try to borrow the same book again (should fail with 'Book not available')
// TODO: Borrow the second book for Alice
// TODO: Print Alice to show her borrowed count
// TODO: Return the first book
// TODO: Print Alice again to show the updated count
}
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