Admin Interface
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 105 of 110.
Challenge
EasyIn the previous lesson, you added search functionality to your Library class. Now let's create an admin interface that allows privileged users to manage the library's inventory - adding new books and removing existing ones.
You'll introduce a new Admin class and enhance your Library with administrative capabilities:
book.dart: Keep your existingBookclass unchanged.user.dart: Keep your existingUserclass unchanged.admin.dart: Create a newAdminclass that represents library administrators. An admin has anid(String) andname(String). OverridetoString()to returnAdmin [id]: name.library.dart: Add administrative functionality to yourLibraryclass:- Add a list to store registered admins
registerAdmin(Admin admin)- adds an admin to the libraryaddBookAsAdmin(String adminId, Book book)- if the admin exists, adds the book and printsadminId added "title". If the admin doesn't exist, printUnauthorized.removeBook(String adminId, String isbn)- if the admin exists and the book exists, removes the book from the library and printsadminId removed "title". If the admin doesn't exist, printUnauthorized. If the book doesn't exist, printBook not found.getBookCount()- returns the total number of books in the library
main.dart: Demonstrate the admin interface. Create aLibraryand register an admin (A001,Sarah Admin). Use the admin to add three 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 Martin
Total books: X. Then try to add a book with an unregistered admin idA999. Remove the second book using the registered admin. Print the final book count.
Expected output:
A001 added "The Pragmatic Programmer"
A001 added "JavaScript: The Good Parts"
A001 added "Clean Code"
Total books: 3
Unauthorized
A001 removed "JavaScript: The Good Parts"
Total books: 2Try it yourself
import 'book.dart';
import 'user.dart';
import 'library.dart';
import 'admin.dart';
void main() {
// TODO: Create a Library
// TODO: Create and register an admin (A001, Sarah Admin)
// TODO: Use the admin to add three books:
// - 978-0-13-468599-1, The Pragmatic Programmer, David Thomas
// - 978-0-596-51774-8, JavaScript: The Good Parts, Douglas Crockford
// - 978-0-13-235088-4, Clean Code, Robert Martin
// TODO: Print the book count as "Total books: X"
// TODO: Try to add a book with an unregistered admin id A999
// TODO: Remove the second book (978-0-596-51774-8) using the registered admin
// TODO: Print the final 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