-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryManagementSystem.java
More file actions
251 lines (213 loc) · 8.14 KB
/
Copy pathLibraryManagementSystem.java
File metadata and controls
251 lines (213 loc) · 8.14 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
249
250
251
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// 1. Book Class
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; // Books are available by default when added
}
public String getTitle() { return title; }
public String getAuthor() { return author; }
public String getIsbn() { return isbn; }
public boolean isAvailable() { return isAvailable; }
public void borrowBook() {
if (isAvailable) {
isAvailable = false;
}
}
public void returnBook() {
isAvailable = true;
}
public void displayBookInfo() {
System.out.println("Title: " + title + " | Author: " + author +
" | ISBN: " + isbn + " | Available: " + (isAvailable ? "Yes" : "No"));
}
}
// 2. Member Class
class Member {
private String name;
private String memberId;
private List<Book> borrowedBooks;
public Member(String name, String memberId) {
this.name = name;
this.memberId = memberId;
this.borrowedBooks = new ArrayList<>();
}
public String getName() { return name; }
public String getMemberId() { return memberId; }
public void borrowBook(Book book) {
borrowedBooks.add(book);
book.borrowBook();
System.out.println(name + " successfully borrowed: " + book.getTitle());
}
public void returnBook(Book book) {
if (borrowedBooks.remove(book)) {
book.returnBook();
System.out.println(name + " successfully returned: " + book.getTitle());
} else {
System.out.println("This book was not borrowed by this member.");
}
}
public void displayMemberInfo() {
System.out.println("Member ID: " + memberId + " | Name: " + name +
" | Books Borrowed: " + borrowedBooks.size());
}
}
// 3. Library Class
class Library {
private List<Book> books;
private List<Member> members;
public Library() {
this.books = new ArrayList<>();
this.members = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
System.out.println("Book added to the library successfully!");
}
public void addMember(Member member) {
members.add(member);
System.out.println("Member registered successfully!");
}
public Member searchMemberById(String memberId) {
for (Member member : members) {
if (member.getMemberId().equals(memberId)) {
return member;
}
}
return null;
}
public Book searchBookByTitle(String title) {
for (Book book : books) {
if (book.getTitle().equalsIgnoreCase(title)) {
return book;
}
}
return null;
}
// Borrow a book (Matches your screenshot logic)
public void borrowBook(String memberId, String bookTitle) {
Member member = searchMemberById(memberId);
Book book = searchBookByTitle(bookTitle);
if (member != null && book != null && book.isAvailable()) {
member.borrowBook(book);
} else if (book != null && !book.isAvailable()) {
System.out.println("The book is currently not available.");
} else {
System.out.println("Member or book not found.");
}
}
// Return a book (Matches your screenshot logic)
public void returnBook(String memberId, String bookTitle) {
Member member = searchMemberById(memberId);
Book book = searchBookByTitle(bookTitle);
if (member != null && book != null && !book.isAvailable()) {
member.returnBook(book);
} else {
System.out.println("Member or book not found, or the book was not borrowed.");
}
}
// Display all books
public void displayBooks() {
if (books.isEmpty()) {
System.out.println("No books in the library.");
return;
}
for (Book book : books) {
book.displayBookInfo();
}
}
// Display all members
public void displayMembers() {
if (members.isEmpty()) {
System.out.println("No members registered.");
return;
}
for (Member member : members) {
member.displayMemberInfo();
}
}
}
// Main Class (Driver Program)
public class LibraryManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Library library = new Library();5
// Initial Instructions for the user
System.out.println("Welcome to the Library Management System");
boolean exit = false;
while (!exit) {
System.out.println("\nChoose an option:");
System.out.println("1. Add a Book");
System.out.println("2. Register a Member");
System.out.println("3. Borrow a Book");
System.out.println("4. Return a Book");
System.out.println("5. Display All Books");
System.out.println("6. Display All Members");
System.out.println("7. Exit\n");
System.out.print("Enter your choice: ");
// Check if the input is an integer to prevent crash on wrong input type
if (!scanner.hasNextInt()) {
System.out.println("Invalid input. Please enter a number.");
scanner.next(); // Consume the invalid input
continue;
}
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
switch (choice) {
case 1: // Add a Book
System.out.println("Enter Book Title:");
String title = scanner.nextLine();
System.out.println("Enter Book Author:");
String author = scanner.nextLine();
System.out.println("Enter Book ISBN:");
String isbn = scanner.nextLine();
library.addBook(new Book(title, author, isbn));
break;
case 2: // Register a Member
System.out.println("Enter Member Name:");
String name = scanner.nextLine();
System.out.println("Enter Member ID:");
String memberId = scanner.nextLine();
library.addMember(new Member(name, memberId));
break;
case 3: // Borrow a Book
System.out.println("Enter Member ID:");
String memberToBorrow = scanner.nextLine();
System.out.println("Enter Book Title to Borrow:");
String bookToBorrow = scanner.nextLine();
library.borrowBook(memberToBorrow, bookToBorrow);
break;
case 4: // Return a Book
System.out.println("Enter Member ID:");
String memberToReturn = scanner.nextLine();
System.out.println("Enter Book Title to Return:");
String bookToReturn = scanner.nextLine();
library.returnBook(memberToReturn, bookToReturn);
break;
case 5: // Display All Books
System.out.println("\n--- Library Books ---");
library.displayBooks();
break;
case 6: // Display All Members
System.out.println("\n--- Registered Members ---");
library.displayMembers();
break;
case 7: // Exit
exit = true;
System.out.println("Exiting the Library Management System.");
break;
default:
System.out.println("Invalid choice. Please select a valid option.");
}
}
scanner.close();
}
}