Skip to content

Commit f092123

Browse files
committed
[Manan] ADD:Library Management System using a Doubly Linked List
1 parent 4c90807 commit f092123

File tree

1 file changed

+255
-0
lines changed

1 file changed

+255
-0
lines changed

LibraryManagementSystem.java

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
// Class representing a Book node in the doubly linked list
2+
class Book {
3+
String title;
4+
String author;
5+
String genre;
6+
int bookID;
7+
boolean isAvailable;
8+
Book next; // Pointer to the next book
9+
Book prev; // Pointer to the previous book
10+
11+
// Constructor to initialize book details
12+
public Book(String title, String author, String genre, int bookID, boolean isAvailable) {
13+
this.title = title;
14+
this.author = author;
15+
this.genre = genre;
16+
this.bookID = bookID;
17+
this.isAvailable = isAvailable;
18+
this.next = null;
19+
this.prev = null;
20+
}
21+
}
22+
23+
// Class to manage the Library using a doubly linked list
24+
class LibraryManager {
25+
private Book head; // Points to the first book
26+
private Book tail; // Points to the last book
27+
private int bookCount; // Stores the total number of books
28+
29+
// Method to add a book at the beginning
30+
public void addBookAtBeginning(String title, String author, String genre, int bookID, boolean isAvailable) {
31+
Book newBook = new Book(title, author, genre, bookID, isAvailable);
32+
if (head == null) {
33+
head = tail = newBook;
34+
} else {
35+
newBook.next = head;
36+
head.prev = newBook;
37+
head = newBook;
38+
}
39+
bookCount++;
40+
}
41+
42+
// Method to add a book at the end
43+
public void addBookAtEnd(String title, String author, String genre, int bookID, boolean isAvailable) {
44+
Book newBook = new Book(title, author, genre, bookID, isAvailable);
45+
if (tail == null) {
46+
head = tail = newBook;
47+
} else {
48+
tail.next = newBook;
49+
newBook.prev = tail;
50+
tail = newBook;
51+
}
52+
bookCount++;
53+
}
54+
55+
// Method to add a book at a specific position
56+
public void addBookAtPosition(String title, String author, String genre, int bookID, boolean isAvailable, int position) {
57+
if (position <= 0) {
58+
System.out.println("Invalid position.");
59+
return;
60+
}
61+
62+
Book newBook = new Book(title, author, genre, bookID, isAvailable);
63+
64+
if (position == 1) {
65+
addBookAtBeginning(title, author, genre, bookID, isAvailable);
66+
return;
67+
}
68+
69+
Book temp = head;
70+
for (int i = 1; i < position - 1 && temp != null; i++) {
71+
temp = temp.next;
72+
}
73+
74+
if (temp == null || temp.next == null) {
75+
addBookAtEnd(title, author, genre, bookID, isAvailable);
76+
} else {
77+
newBook.next = temp.next;
78+
newBook.prev = temp;
79+
temp.next.prev = newBook;
80+
temp.next = newBook;
81+
bookCount++;
82+
}
83+
}
84+
85+
// Method to remove a book by Book ID
86+
public void removeBook(int bookID) {
87+
if (head == null) {
88+
System.out.println("No books in the library.");
89+
return;
90+
}
91+
92+
Book temp = head;
93+
while (temp != null && temp.bookID != bookID) {
94+
temp = temp.next;
95+
}
96+
97+
if (temp == null) {
98+
System.out.println("Book with ID " + bookID + " not found.");
99+
return;
100+
}
101+
102+
if (temp == head) {
103+
head = temp.next;
104+
if (head != null) {
105+
head.prev = null;
106+
} else {
107+
tail = null;
108+
}
109+
} else if (temp == tail) {
110+
tail = temp.prev;
111+
tail.next = null;
112+
} else {
113+
temp.prev.next = temp.next;
114+
temp.next.prev = temp.prev;
115+
}
116+
117+
bookCount--;
118+
System.out.println("Book with ID " + bookID + " removed.");
119+
}
120+
121+
// Method to search for a book by Title
122+
public void searchBookByTitle(String title) {
123+
Book temp = head;
124+
boolean found = false;
125+
while (temp != null) {
126+
if (temp.title.equalsIgnoreCase(title)) {
127+
System.out.println("Book Found: " + temp.title + " | Author: " + temp.author);
128+
found = true;
129+
}
130+
temp = temp.next;
131+
}
132+
if (!found) {
133+
System.out.println("No books found with title '" + title + "'.");
134+
}
135+
}
136+
137+
// Method to search for a book by Author
138+
public void searchBookByAuthor(String author) {
139+
Book temp = head;
140+
boolean found = false;
141+
while (temp != null) {
142+
if (temp.author.equalsIgnoreCase(author)) {
143+
System.out.println("Book Found: " + temp.title + " | Author: " + temp.author);
144+
found = true;
145+
}
146+
temp = temp.next;
147+
}
148+
if (!found) {
149+
System.out.println("No books found by author '" + author + "'.");
150+
}
151+
}
152+
153+
// Method to update a book’s availability status by Book ID
154+
public void updateBookAvailability(int bookID, boolean isAvailable) {
155+
Book temp = head;
156+
while (temp != null) {
157+
if (temp.bookID == bookID) {
158+
temp.isAvailable = isAvailable;
159+
System.out.println("Updated availability of book ID " + bookID + " to " + (isAvailable ? "Available" : "Not Available"));
160+
return;
161+
}
162+
temp = temp.next;
163+
}
164+
System.out.println("Book with ID " + bookID + " not found.");
165+
}
166+
167+
// Method to display all books in forward order
168+
public void displayBooksForward() {
169+
if (head == null) {
170+
System.out.println("No books in the library.");
171+
return;
172+
}
173+
174+
System.out.println("Books (Forward Order):");
175+
Book temp = head;
176+
while (temp != null) {
177+
System.out.println(temp.bookID + " | " + temp.title + " | " + temp.author + " | " + temp.genre + " | " + (temp.isAvailable ? "Available" : "Not Available"));
178+
temp = temp.next;
179+
}
180+
}
181+
182+
// Method to display all books in reverse order
183+
public void displayBooksReverse() {
184+
if (tail == null) {
185+
System.out.println("No books in the library.");
186+
return;
187+
}
188+
189+
System.out.println("Books (Reverse Order):");
190+
Book temp = tail;
191+
while (temp != null) {
192+
System.out.println(temp.bookID + " | " + temp.title + " | " + temp.author + " | " + temp.genre + " | " + (temp.isAvailable ? "Available" : "Not Available"));
193+
temp = temp.prev;
194+
}
195+
}
196+
197+
// Method to count total books in the library
198+
public int countTotalBooks() {
199+
return bookCount;
200+
}
201+
}
202+
203+
// Main class to run the program
204+
public class LibraryManagementSystem {
205+
public static void main(String[] args) {
206+
LibraryManager library = new LibraryManager();
207+
208+
// Adding books
209+
library.addBookAtEnd("Gulliver's Travels", "Jonathan Swift", "Adventure", 101, true);
210+
library.addBookAtEnd("Story of My Life", "Helen Keller", "Autobiography", 102, true);
211+
library.addBookAtBeginning("Java Essentials", "James Gosling", "Education", 103, false);
212+
library.addBookAtPosition("Panchatantra", "Vishnu Sharma", "Stories", 104, true, 1);
213+
214+
// Display books
215+
library.displayBooksForward();
216+
library.displayBooksReverse();
217+
218+
// Search books
219+
library.searchBookByTitle("Story of My Life");
220+
library.searchBookByAuthor("Jonathan Swift");
221+
222+
// Update availability
223+
library.updateBookAvailability(102, false);
224+
225+
// Remove book
226+
library.removeBook(103);
227+
228+
// Display after updates
229+
library.displayBooksForward();
230+
231+
// Count total books
232+
System.out.println("Total Books: " + library.countTotalBooks());
233+
}
234+
}
235+
236+
//SampleOutput
237+
//Books (Forward Order):
238+
//104 | Panchatantra | Vishnu Sharma | Stories | Available
239+
//103 | Java Essentials | James Gosling | Education | Not Available
240+
//101 | Gulliver's Travels | Jonathan Swift | Adventure | Available
241+
//102 | Story of My Life | Helen Keller | Autobiography | Available
242+
//Books (Reverse Order):
243+
//102 | Story of My Life | Helen Keller | Autobiography | Available
244+
//101 | Gulliver's Travels | Jonathan Swift | Adventure | Available
245+
//103 | Java Essentials | James Gosling | Education | Not Available
246+
//104 | Panchatantra | Vishnu Sharma | Stories | Available
247+
//Book Found: Story of My Life | Author: Helen Keller
248+
//Book Found: Gulliver's Travels | Author: Jonathan Swift
249+
//Updated availability of book ID 102 to Not Available
250+
//Book with ID 103 removed.
251+
//Books (Forward Order):
252+
//104 | Panchatantra | Vishnu Sharma | Stories | Available
253+
//101 | Gulliver's Travels | Jonathan Swift | Adventure | Available
254+
//102 | Story of My Life | Helen Keller | Autobiography | Not Available
255+
//Total Books: 3

0 commit comments

Comments
 (0)