Menu
Coddy logo textTech

Admin Interface

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 105 of 110.

challenge icon

Challenge

Easy

In 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 existing Book class unchanged.
  • user.dart: Keep your existing User class unchanged.
  • admin.dart: Create a new Admin class that represents library administrators. An admin has an id (String) and name (String). Override toString() to return Admin [id]: name.
  • library.dart: Add administrative functionality to your Library class:
    • Add a list to store registered admins
    • registerAdmin(Admin admin) - adds an admin to the library
    • addBookAsAdmin(String adminId, Book book) - if the admin exists, adds the book and prints adminId added "title". If the admin doesn't exist, print Unauthorized.
    • removeBook(String adminId, String isbn) - if the admin exists and the book exists, removes the book from the library and prints adminId removed "title". If the admin doesn't exist, print Unauthorized. If the book doesn't exist, print Book not found.
    • getBookCount() - returns the total number of books in the library
  • main.dart: Demonstrate the admin interface. Create a Library and register an admin (A001, Sarah Admin). 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
    Print the book count as Total books: X. Then try to add a book with an unregistered admin id A999. 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: 2

Try 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