22dit081 Mad Practical 2
22dit081 Mad Practical 2
LAB-2
Task Requirements:
Book Class:
• Create a Book class with properties: title, author, yearPublished, and isAvailable
(boolean).
Library Class:
User Interaction:
• Use a loop to present a menu to the user with the following options:
• Borrow a book.
• Return a book.
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:
• 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.
• Classes & Objects: Book and Library encapsulate data and methods.
• Abstract Class: User defines a template for derived classes like Member.
Control Structures:
Exception Handling:
2
IT366-MAD 22DIT081
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';
void displayUserType();
} } class
Book {
author; int
yearPublished;
bool isAvailable;
Book({
required this.title,
3
IT366-MAD 22DIT081
this.yearPublished,
this.isAvailable = true,
});
void displayDetails() {
{ double fileSize;
EBook({
required this.fileSize,
print("File
Size: ${fileSize}MB\n");
} } class Library {
int totalBooks = 0;
Library(this._books) { totalBooks
= _books.length;
4
IT366-MAD 22DIT081
_books.add(book); totalBooks++;
print("Book added
successfully.\n");
try {
already borrowed.");
book.isAvailable = false;
${e.toString()}\n");
try {
not borrowed.");
returned: $title\n");
${e.toString()}\n");
5
IT366-MAD 22DIT081
Future.delayed(Duration(seconds: 2));
EBook(title: "Flutter for Beginners", author: "John Doe", yearPublished: 2020, fileSize:
1.5),
]);
"1":
String title =
stdin.readLineSync()!;
6
IT366-MAD 22DIT081
stdout.write("Enter author:
stdin.readLineSync()!;
stdout.write("Enter year
int.parse(stdin.readLineSyn
c()!);
library.addBook(Book(title:
yearPublished: year));
break;
case "2":
library.borrowBook(title); break;
case "3":
library.returnBook(title); break;
library.listBooks(); break;
case "5":
Library.displayTotalBooks(); break;
7
IT366-MAD 22DIT081
return;
default:
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