DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 1.1
Student Name: Priyanshu Dhatwalia UID: 23BCS12606
Branch: BE-CSE Section/Group: 605A
Semester: 4th Date of Performance:
Subject Name: OOPs using Java Subject Code: 23CSP-202
1. Aim: Design a system where books can be borrowed and return,
modulating the relationship between book, member and library class using
inheritance and Composition.
2. Objective:
The objective of this system is to design a library management system by
applying object-oriented programming principles, specifically focusing
on:
1. Class Structure:
Define appropriate classes (Book, Member, Library, etc.) to
represent real-world entities.
Encapsulate attributes and behaviors related to each entity.
2. Inheritance:
Establish a hierarchy where common properties and behaviors are
inherited from parent classes to reduce code redundancy.
Example: A Person base class could be inherited by Member and
other types of users like Staff.
3. Composition:
Model "has-a" relationships using composition to represent how
different objects interact.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Example: A Library has multiple Book and Member objects, and a
member has borrowed books.
3. Java Code:
import java.util.*;
public class Testing {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the library name: ");
String libraryName = sc.nextLine();
Library library = new Library(libraryName);
System.out.print("How many books do you want to add to
the library? ");
int bookCount = sc.nextInt();
sc.nextLine();
for (int i = 0; i < bookCount; i++) {
System.out.print("Enter the title of book " + (i) + ": ");
String title = sc.nextLine();
System.out.print("Enter the author of book " + (i) + ": ");
String author = sc.nextLine();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
library.addBook(new Book(title, author));
}
System.out.print("Enter the name of the member: ");
String memberName = sc.nextLine();
Member member = new Member(memberName);
System.out.print("Enter the title of the book to borrow: ");
String borrowTitle = sc.nextLine();
library.borrowBook(borrowTitle, member);
System.out.print("Enter the title of the book to return: ");
String returnTitle = sc.nextLine();
library.returnBook(returnTitle, member);
sc.close();
}
}
class Library {
private String name;
private List<Book> books;
public Library(String name) {
this.name = name;
this.books = new ArrayList<>();
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
public void addBook(Book book) {
books.add(book);
System.out.println(book.getTitle() + " added to " + name);
}
public void borrowBook(String title, Member member) {
for (Book book : books) {
if (book.getTitle().equalsIgnoreCase(title)) {
if (book.isAvailable()) {
book.setAvailable(false);
member.borrowBook(book);
System.out.println(member.getName() + " borrowed
" + title);
} else {
System.out.println(title + " is currently not
available.");
}
return;
}
}
System.out.println(title + " not found in the library.");
}
public void returnBook(String title, Member member) {
for (Book book : books) {
if (book.getTitle().equalsIgnoreCase(title)) {
book.setAvailable(true);
member.returnBook(book);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
System.out.println(member.getName() + " returned " +
title);
return;
}
}
System.out.println(title + " does not belong to this library.");
}
}
class Book {
private String title;
private String author;
private boolean isAvailable;
public Book(String title, String author) {
this.title = title;
this.author = author;
this.isAvailable = true;
}
public String getTitle() {
return title;
}
public boolean isAvailable() {
return isAvailable;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
public void setAvailable(boolean available) {
isAvailable = available;
}
}
class Member {
private String name;
private List<Book> borrowedBooks;
public Member(String name) {
this.name = name;
this.borrowedBooks = new ArrayList<>();
}
public String getName() {
return name;
}
public void borrowBook(Book book) {
borrowedBooks.add(book);
}
public void returnBook(Book book) {
borrowedBooks.remove(book);
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
4. Output: