-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryManagement.java
More file actions
248 lines (206 loc) · 8 KB
/
LibraryManagement.java
File metadata and controls
248 lines (206 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import java.util.ArrayList;
import java.util.Scanner;
class Book {
private String title;
private String author;
private String isbn;
private boolean isAvailable;
public Book(String title, String author, String isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.isAvailable = true;
}
public String getIsbn() {
return isbn;
}
public boolean isAvailable() {
return isAvailable;
}
public void setAvailable(boolean available) {
isAvailable = available;
}
public void displayBookDetails() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("ISBN: " + isbn);
System.out.println("Status: " + (isAvailable ? "Available" : "Borrowed"));
}
public String getTitle() {
return title;
}
}
class Member {
private int memberId;
private String name;
private ArrayList<Book> borrowedBooks;
private static int nextMemberId = 101;
public Member(String name) {
this.memberId = nextMemberId++;
this.name = name;
this.borrowedBooks = new ArrayList<>();
}
public int getMemberId() {
return memberId;
}
public String getName() {
return name;
}
public ArrayList<Book> getBorrowedBooks() {
return borrowedBooks;
}
public void displayMemberDetails() {
System.out.println("Member ID: " + memberId);
System.out.println("Name: " + name);
System.out.println("Borrowed Books: " + borrowedBooks.size());
if (!borrowedBooks.isEmpty()) {
System.out.println(" Currently Borrowed Books ");
for (Book book : borrowedBooks) {
System.out.println(" - " + book.getIsbn() + " (" + book.getTitle() + ")");
}
}
}
}
class Library {
private ArrayList<Book> books;
private ArrayList<Member> members;
public Library() {
this.books = new ArrayList<>();
this.members = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
System.out.println("Book '" + book.getTitle() + "' added to the library.");
}
public void registerMember(Member member) {
members.add(member);
System.out.println("Member '" + member.getName() + "' registered with ID: " + member.getMemberId());
}
public void lendBook(String isbn, int memberId) {
Book bookToLend = findBook(isbn);
Member member = findMember(memberId);
if (bookToLend == null) {
System.out.println("Error: Book with ISBN " + isbn + " not found.");
return;
}
if (member == null) {
System.out.println("Error: Member with ID " + memberId + " not found.");
return;
}
if (!bookToLend.isAvailable()) {
System.out.println("Error: Book '" + bookToLend.getTitle() + "' is currently borrowed.");
return;
}
bookToLend.setAvailable(false);
member.getBorrowedBooks().add(bookToLend);
System.out.println("Book '" + bookToLend.getTitle() + "' successfully lent to " + member.getName());
}
public void returnBook(String isbn, int memberId) {
Book bookToReturn = findBook(isbn);
Member member = findMember(memberId);
if (bookToReturn == null) {
System.out.println("Error: Book with ISBN " + isbn + " not found.");
return;
}
if (member == null) {
System.out.println("Error: Member with ID " + memberId + " not found.");
return;
}
if (!member.getBorrowedBooks().remove(bookToReturn)) {
System.out.println("Error: The book '" + bookToReturn.getTitle() + "' was not borrowed by this member.");
return;
}
bookToReturn.setAvailable(true);
System.out.println("Book '" + bookToReturn.getTitle() + "' successfully returned by " + member.getName());
}
public void displayAllBooks() {
System.out.println(" Library Book Catalog ");
for (Book book : books) {
book.displayBookDetails();
}
}
public void displayAllMembers() {
System.out.println("Registered Members ");
for (Member member : members) {
member.displayMemberDetails();
}
}
private Book findBook(String isbn) {
for (Book book : books) {
if (book.getIsbn().equals(isbn)) {
return book;
}
}
return null;
}
private Member findMember(int memberId) {
for (Member member : members) {
if (member.getMemberId() == memberId) {
return member;
}
}
return null;
}
}
public class LibraryManagement {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Library library = new Library();
// Adding some initial books and members for demonstration
library.addBook(new Book("The Lord of the Rings", "J.R.R. Tolkien", "9780618640685"));
library.addBook(new Book("Pride and Prejudice", "Jane Austen", "9780141439518"));
library.addBook(new Book("To Kill a Mockingbird", "Harper Lee", "9780061120084"));
library.registerMember(new Member("Alice"));
library.registerMember(new Member("Bob"));
while (true) {
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println(" Library Management System ");
System.out.println("1. Lend a Book");
System.out.println("2. Return a Book");
System.out.println("3. Display All Books");
System.out.println("4. Display All Members");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
String choice = scanner.nextLine();
switch (choice) {
case "1":
System.out.print("Enter ISBN of the book to lend: ");
String isbnToLend = scanner.nextLine();
System.out.print("Enter member ID: ");
try {
int memberIdToLend = Integer.parseInt(scanner.nextLine());
library.lendBook(isbnToLend, memberIdToLend);
} catch (NumberFormatException e) {
System.out.println("Invalid member ID. Please enter a number.");
}
break;
case "2":
System.out.print("Enter ISBN of the book to return: ");
String isbnToReturn = scanner.nextLine();
System.out.print("Enter member ID: ");
try {
int memberIdToReturn = Integer.parseInt(scanner.nextLine());
library.returnBook(isbnToReturn, memberIdToReturn);
} catch (NumberFormatException e) {
System.out.println("Invalid member ID. Please enter a number.");
}
break;
case "3":
library.displayAllBooks();
break;
case "4":
library.displayAllMembers();
break;
case "5":
System.out.println("\nExiting the Library Management System. Goodbye!");
return;
default:
System.out.println("\nInvalid choice. Please try again.");
break;
}
System.out.println("\nPress Enter to continue...");
scanner.nextLine();
}
}
}