Menu
Coddy logo textTech

Borrowing System

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

challenge icon

Challenge

Easy

In 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 existing Book class with isbn, title, author, isAvailable, and toString().
  • user.dart: Keep your existing User class with id, name, borrowedBooks, and toString().
  • library.dart: Create a new Library class that manages books and users. It should have two lists: one for books and one for users. Include methods to:
    • addBook(Book book) - adds a book to the library's collection
    • registerUser(User user) - adds a user to the library
    • borrowBook(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 prints userId borrowed "title". If the book doesn't exist, print Book not found. If the book isn't available, print Book not available. If the user doesn't exist, print User 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 prints userId returned "title". If the book doesn't exist, print Book not found. If the user doesn't exist, print User not found.
  • main.dart: Import all your files and demonstrate the borrowing system. Create a Library, add two books (978-0-13-468599-1, The Pragmatic Programmer, David Thomas and 978-0-596-51774-8, JavaScript: The Good Parts, Douglas Crockford), and register one user (U001, Alice Johnson). Then:
    1. Borrow the first book for Alice
    2. Try to borrow the same book again (should fail)
    3. Borrow the second book for Alice
    4. Print Alice to show her borrowed count
    5. Return the first book
    6. 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