0% found this document useful (0 votes)
40 views10 pages

22dit081 Mad Practical 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views10 pages

22dit081 Mad Practical 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

IT366-MAD 22DIT081

LAB-2

Aim: Library Management System


Objective: Develop a simple console-based library management system for a book collection.
The system should allow users to perform various operations while demonstrating the use of
conditional statements, loops, functions, classes, inheritance, exception handling, and async
programming.

Task Requirements:

Book Class:

• Create a Book class with properties: title, author, yearPublished, and isAvailable
(boolean).

• Implement getters and setters for these properties.

• Include a method to display book details.

Library Class:

• Create a Library class that maintains a collection of Book objects.

• Include a method to add a book to the library.

• Include a method to borrow a book (mark it as not available).

• Include a method to return a book (mark it as available).

• Use a constructor to initialize the library with a predefined list of books.

User Interaction:

• Use a loop to present a menu to the user with the following options:

• Add a new book.

• Borrow a book.

• Return a book.

• List all books.

• Exit the system.


Use conditional statements to handle user inputs and navigate the menu.Switch-Case for Menu
Operations:

• Use a switch-case statement to process menu selections.

Handling Exceptions:
1
IT366-MAD 22DIT081

• Implement exception handling for cases like attempting to borrow a book that is
unavailable or returning a book that was not borrowed.

Inheritance:

• Create a subclass EBook that inherits from Book and adds a property fileSize.
Override the method that displays book details to include the file size.

Static Methods:

• Implement a static method in the Library class that keeps track of the total number of
books in the library.

Abstract Class:

• Create an abstract class User with an abstract method displayUserType(). Create a


subclass Member that implements this method.

Use of Async and Await:

• Simulate an asynchronous process for listing books with a method in the Library class
that returns a Future<List<Book>> and uses await to simulate a delay.

Theory:
The Library Management System project demonstrates core programming concepts through
a console-based application that manages a book collection.

 Object-Oriented Programming (OOP):

• Classes & Objects: Book and Library encapsulate data and methods.

• Inheritance: EBook extends Book, adding fileSize and overriding methods.

• Abstract Class: User defines a template for derived classes like Member.

• Static Methods: Track library-wide properties (e.g., total book count).

 Control Structures:

• Conditional Statements: Handle user inputs and validate actions.

• Loops: Present and navigate menu options.

• Switch-Case: Simplify menu operations.

 Exception Handling:

2
IT366-MAD 22DIT081

• Manage errors, such as borrowing unavailable books or returning non-borrowed


books.

 Async Programming:
• Simulates delays (e.g., fetching books) using async and await, showcasing
concurrency.

 Encapsulation:

• Use getters and setters for secure property access and modification.

Code:
import 'dart:async'; import

'dart:io';

abstract class User {

void displayUserType();

} class Member extends User

@override void displayUserType() { print("User

Type: Library Member");

} } class

Book {

String title; String

author; int

yearPublished;

bool isAvailable;

Book({

required this.title,

3
IT366-MAD 22DIT081

required this.author, required

this.yearPublished,

this.isAvailable = true,

});

void displayDetails() {

print("Title: $title\nAuthor: $author\nYear: $yearPublished\nAvailable: ${isAvailable ? 'Yes'


: 'No'}\n");
} } class EBook extends Book

{ double fileSize;

EBook({

required String title,

required String author,

required int yearPublished,

required this.fileSize,

}) : super(title: title, author: author, yearPublished: yearPublished);

@override void displayDetails() { super.displayDetails();

print("File

Size: ${fileSize}MB\n");

} } class Library {

List<Book> _books; static

int totalBooks = 0;

Library(this._books) { totalBooks

= _books.length;

4
IT366-MAD 22DIT081

void addBook(Book book) {

_books.add(book); totalBooks++;

print("Book added

successfully.\n");

} void borrowBook(String title)

try {

Book book = _books.firstWhere((b) => b.title == title);

if (!book.isAvailable) { throw Exception("The book is

already borrowed.");

book.isAvailable = false;

print("You borrowed: $title\n");

} catch (e) { print("Error:

${e.toString()}\n");

} void returnBook(String title)

try {

Book book = _books.firstWhere((b) => b.title == title);

if (book.isAvailable) { throw Exception("The book was

not borrowed.");

book.isAvailable = true; print("You

returned: $title\n");

} catch (e) { print("Error:

${e.toString()}\n");

5
IT366-MAD 22DIT081

Future<void> listBooks() async { print("Fetching

book list...\n"); await

Future.delayed(Duration(seconds: 2));

_books.forEach((book) => book.displayDetails());

} static void displayTotalBooks() { print("Total

Books in Library: $totalBooks\n");

} } void main() async

Library library = Library([

Book(title: "1984", author: "George Orwell", yearPublished: 1949),

Book(title: "To Kill a Mockingbird", author: "Harper Lee", yearPublished: 1960),

EBook(title: "Flutter for Beginners", author: "John Doe", yearPublished: 2020, fileSize:
1.5),

]);

Member user = Member(); user.displayUserType();

while (true) { print("\nLibrary Management System\n---------

----------------"); print("1. Add a new book\n2. Borrow a book\n3. Return a book\n4.


List all books\n5. Display total books\n6. Exit\n"); stdout.write("Enter your choice:
");

String? choice = stdin.readLineSync();

switch (choice) { case

"1":

stdout.write("Enter title: ");

String title =

stdin.readLineSync()!;

6
IT366-MAD 22DIT081

stdout.write("Enter author:

"); String author =

stdin.readLineSync()!;

stdout.write("Enter year

published: "); int year =

int.parse(stdin.readLineSyn

c()!);

library.addBook(Book(title:

title, author: author,

yearPublished: year));

break;

case "2":

stdout.write("Enter the title of the book to borrow: ");

String title = stdin.readLineSync()!;

library.borrowBook(title); break;

case "3":

stdout.write("Enter the title of the book to return: ");

String title = stdin.readLineSync()!;

library.returnBook(title); break;

case "4": await

library.listBooks(); break;

case "5":

Library.displayTotalBooks(); break;

case "6": print("Exiting the system. Goodbye!");

7
IT366-MAD 22DIT081

return;

default:

print("Invalid choice. Please try again.\n");

Output:

8
IT366-MAD 22DIT081

Latest Applications:
• School or College Libraries
• Community Libraries
• Mobile eBook Readers

9
IT366-MAD 22DIT081

Learning Outcomes:
• Object-Oriented Programming
• Programming Fundamentals
• Concurrency and Asynchronous Programming

10

You might also like