Design A Library Management System
Design A Library Management System
•
System Requirements
•
Use case diagram
•
Class diagram
•
Activity diagrams
•
Code
System Requirements
#
2. Each book will have a unique identification number and other details
including a rack number which will help to physically locate the book.
3. There could be more than one copy of a book, and library members
should be able to check-out and reserve any copy. We will call each copy
of a book a book item
of a book, a book item.
(/learn)
7. The system should be able to collect fines for books returned after the
due date.
10. Each book and member card will have a unique barcode. The system
will be able to read barcodes from books and members’ library cards.
Here are the top use cases of the Library Management System:
Class diagram
Class diagram
# (/learn)
Library: The central part of the organization for which this software
has been designed. It has attributes like ‘Name’ to distinguish it from
any other libraries and ‘Address’ to describe its location.
Book: The basic building block of the system. Every book will have
ISBN, Title, Subject, Publishers, etc.
BookItem: Any book can have multiple copies, each copy will be
considered a book item in our system. Each book item will have a
unique barcode.
Account: We will have two types of accounts in the system, one will be
a general member, and the other will be a librarian.
LibraryCard: Each library user will be issued a library card, which will
be used to identify users while issuing or returning books.
Fine: This class will be responsible for calculating and collecting fines
from library members.
UML conventions
<<interface>>
Name
Interface: Classes implement interfaces, denoted by Generalization.
method1()
ClassName
property_name: type Class: Every class can have properties and methods.
Abstract classes are identified by their Italic names.
method(): type
A B Generalization: A implements B.
Activity diagrams
#
Check-out a book: Any library member or librarian can perform this
activity. Here are the set of steps to check-out a book:
[no]
[yes]
[else]
[yes]
[no]
System updates the status of the book to 'Loaned' Show error message
Return a book: Any library member or librarian can perform this activity.
The system will collect fines from members if they return books after the
due date. Here are the steps for returning a book:
(/learn)
(/learn)
Renew a book: While renewing (re-issuing) a book, the system will check for
fines and see if any other member has not reserved the same book, in that
case the book item cannot be renewed. Here are the different steps for
renewing a book:
[no]
Calculate fine
[yes]
[no]
Code
#
Here is the code for the use cases mentioned above: 1) Check-out a book, 2)
Return a book, and 3) Renew a book.
Note: This code only focuses on the design part of the use cases. Since you
are not required to write a fully executable code in an interview, you can
assume parts of the code to interact with the database, payment system, etc.
Enums and Constants: Here are the required enums, data types, and
constants:
Java Python
Java Python
1 // For simplicity, we are not defining getter and setter functions. The reader can
2 // assume that all class attributes are private and accessed through their respectiv
3 // public getter methods and modified only through their public methods function.
4
5 public abstract class Account {
6 (/learn)
private String id;
7 private String password;
8 private AccountStatus status;
9 private Person person;
10
11 public boolean resetPassword();
12 }
13
14 public class Librarian extends Account {
15 public boolean addBookItem(BookItem bookItem);
16
17 public boolean blockMember(Member member);
18
19 public boolean unBlockMember(Member member);
20 }
21
22 public class Member extends Account {
23 private Date dateOfMembership;
24 private int totalBooksCheckedout;
25
26 public int getTotalBooksCheckedout();
27
28 public boolean reserveBookItem(BookItem bookItem);
29
30 private void incrementTotalBooksCheckedout();
31
32 public boolean checkoutBookItem(BookItem bookItem) {
33 if (this.getTotalBooksCheckedOut() >= Constants.MAX_BOOKS_ISSUED_TO_A_USER) {
34 ShowError("The user has already checked-out maximum number of books");
35 return false;
36 }
37 BookReservation bookReservation = BookReservation.fetchReservationDetails(bookIt
38 if (bookReservation != null && bookReservation.getMemberId() != this.getId()) {
39 // book item has a pending reservation from another user
40 ShowError("This book is reserved by another member");
41 return false;
42 } else if (bookReservation != null) {
43 // book item has a pending reservation from the give member, update it
44 bookReservation.updateStatus(ReservationStatus.COMPLETED);
45 }
46
47 if (!bookItem.checkout(this.getId())) {
48 return false;
49 }
50
51 this.incrementTotalBooksCheckedout();
52 return true;
53 }
54
55 private void checkForFine(String bookItemBarcode) {
56 B kL di b kL di B kL di f t hL di D t il (b kIt B d )
56 BookLending bookLending = BookLending.fetchLendingDetails(bookItemBarcode);
57 Date dueDate = bookLending.getDueDate();
(/learn)
58 Date today = new Date();
59 // check if the book has been returned within the due date
60 if (today.compareTo(dueDate) > 0) {
61 long diff = todayDate.getTime() - dueDate.getTime();
62 long diffDays = diff / (24 * 60 * 60 * 1000);
63 Fine.collectFine(memberId, diffDays);
64 }
65 }
66
67 public void returnBookItem(BookItem bookItem) {
68 this.checkForFine(bookItem.getBarcode());
69 BookReservation bookReservation = BookReservation.fetchReservationDetails(bookIt
70 if (bookReservation != null) {
71 // book item has a pending reservation
72 bookItem.updateBookItemStatus(BookStatus.RESERVED);
73 bookReservation.sendBookAvailableNotification();
74 }
75 bookItem.updateBookItemStatus(BookStatus.AVAILABLE);
76 }
77
78 public bool renewBookItem(BookItem bookItem) {
79 this.checkForFine(bookItem.getBarcode());
80 BookReservation bookReservation = BookReservation.fetchReservationDetails(bookIt
81 // check if this book item has a pending reservation from another member
82 if (bookReservation != null && bookReservation.getMemberId() != this.getMemberId
83 ShowError("This book is reserved by another member");
84 member.decrementTotalBooksCheckedout();
85 bookItem.updateBookItemState(BookStatus.RESERVED);
86 bookReservation.sendBookAvailableNotification();
87 return false;
88 } else if (bookReservation != null) {
89 // book item has a pending reservation from this member
90 bookReservation.updateStatus(ReservationStatus.COMPLETED);
91 }
92 BookLending.lendBook(bookItem.getBarCode(), this.getMemberId());
93 bookItem.updateDueDate(LocalDate.now().plusDays(Constants.MAX_LENDING_DAYS));
94 return true;
95 }
96 }
97
Java Python
1 public class BookReservation {
(/learn)
2 private Date creationDate;
3 private ReservationStatus status;
4 private String bookItemBarcode;
5 private String memberId;
6
7 public static BookReservation fetchReservationDetails(String barcode);
8 }
9
10 public class BookLending {
11 private Date creationDate;
12 private Date dueDate;
13 private Date returnDate;
14 private String bookItemBarcode;
15 private String memberId;
16
17 public static void lendBook(String barcode, String memberId);
18 public static BookLending fetchLendingDetails(String barcode);
19 }
20
21 public class Fine {
22 private Date creationDate;
23 private double bookItemBarcode;
24 private String memberId;
25
26 public static void collectFine(String memberId, long days) {}
27 }
28
Java Python
Search interface and Catalog: The Catalog class will implement the Search
interface to facilitate searching of books.
Java Python
← Back Next →
(/courses/grokking- MARK(/courses/grokking-
AS COMPLETED
the- the-
object- object-
oriented- oriented-
Activity
design-
Diagrams Design adesign-
Parking Lot
interview/B8RPL3VEl8N) interview/gxM3gRxmr8Z)
Ask a Question
(https://discuss.educative.io/c/grokking-the-object-oriented-design-interview-
Report an
design-gurus/object-oriented-design-case-studies-design-a-library-management-
Issue
system)