Book and User Classes
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 102 of 110.
Challenge
EasyIn the previous lesson, you created a Book class with basic properties and a toString() method. Now it's time to expand your library system by adding a User class to represent library members who can borrow books.
You'll need to create a new file and update your existing files:
book.dart: Enhance your existingBookclass by adding anisAvailableproperty (boolean) that defaults totrue. This will track whether a book can be borrowed.user.dart: Create a newUserclass to represent library members. Each user should have anid(String),name(String), and aborrowedBookslist that holdsBookobjects. Include a constructor that initializes the id and name, with an empty list for borrowed books. OverridetoString()to return the formatUser [id]: name (X books borrowed)where X is the count of borrowed books.main.dart: Import both your book and user files. Create a book with ISBN978-0-13-468599-1, titleThe Pragmatic Programmer, and authorDavid Thomas. Create a user with idU001and nameAlice Johnson. Print the book, then print whether it's available using the formatAvailable: trueorAvailable: false. Print the user. Then add the book to the user's borrowed books list, set the book'sisAvailabletofalse, and print the user again to show the updated book count.
This establishes the relationship between books and users that you'll build upon when implementing the full borrowing system in the next lesson!
Expected output:
[978-0-13-468599-1] The Pragmatic Programmer by David Thomas
Available: true
User U001: Alice Johnson (0 books borrowed)
User U001: Alice Johnson (1 books borrowed)Try it yourself
import 'book.dart';
// TODO: Import user.dart
void main() {
// TODO: Create a book with ISBN '978-0-13-468599-1', title 'The Pragmatic Programmer', and author 'David Thomas'
// TODO: Create a user with id 'U001' and name 'Alice Johnson'
// TODO: Print the book
// TODO: Print whether the book is available using format: Available: true/false
// TODO: Print the user
// TODO: Add the book to the user's borrowedBooks list
// TODO: Set the book's isAvailable to false
// TODO: Print the user again to show updated book 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