-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
LibraryManagementSystem.java
321 lines (262 loc) · 6.97 KB
/
LibraryManagementSystem.java
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* package librarysystem;
import java.util.ArrayList;
import java.util.Scanner;
public class Librarysorter extends Libraryinfo{
public static void main(String[] args) {
stock = new ArrayList<>();
borrowers = new ArrayList<>();
Scanner sc = new Scanner(System.in);
int choice = 0;
do {
System.out.println("Welcome To Libray Management System");
System.out.println("What Do You Want To Do? ");
System.out.println("");
System.out.println("1. Register a new borrower");
System.out.println("2. Add a new book to the stock");
System.out.println("3. Borrow a book");
System.out.println("4. Return a book");
System.out.println("5. Produce a list of books available");
System.out.println("6. Produce a list of borrowed books");
System.out.println("7. Borrower can view borrowed book/s");
System.out.println("0. Exit");
System.out.println(" ");
System.out.print("Enter choice: ");
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
registerBorrower();
break;
case 2:
addBook();
break;
case 3:
borrowBook();
break;
case 4:
returnBook();
break;
case 5:
printAvailableBooks();
break;
case 6:
printBorrowedBooks();
break;
case 7:
viewBorrowedBooks();
break;
}
} while (choice != 0);
}
}
*/
import java.awt.print.Book;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LibraryManagementSystem {
static ArrayList<Book> stock;
static ArrayList<Borrower> borrowers;
static class Book {
String title;
private boolean availability;
Book(String title) {
this.title = title;
this.availability = true;
}
public String getTitle() {
return title;
}
public boolean isAvailable() {
return availability;
}
public void setAvailability(boolean availability) {
this.availability = availability;
}
}
static class Borrower {
private String name;
private ArrayList<Book> borrowedBooks;
Borrower(String name) {
this.name = name;
this.borrowedBooks = new ArrayList<>();
}
public String getName() {
return name;
}
public ArrayList<Book> getBorrowedBooks() {
return borrowedBooks;
}
public void borrowBook(Book book) {
this.borrowedBooks.add(book);
}
public void returnBook(Book book) {
this.borrowedBooks.remove(book);
}
}
public static void registerBorrower() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter borrower name: ");
String name = sc.nextLine();
borrowers.add(new Borrower( name));
System.out.println("Borrower registered successfully.");
}
public static void addBook() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter title: ");
String title = sc.nextLine();
stock.add(new Book(title));
System.out.println("Book added to stock successfully.");
}
public static void borrowBook() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter borrower Name: ");
String name = sc.nextLine();
System.out.print("Enter the Title of the Book: ");
String title = sc.nextLine();
Borrower borrower = null;
Book book = null;
for (Borrower b : borrowers) {
if (name.equals(b.getName())) {
borrower = b;
break;
}
}
if (borrower == null) {
System.out.println("Borrower not found.");
return;
}
for (Book b : stock) {
if (b.getTitle().equals(title) && b.isAvailable()) {
book = b;
break;
}
}
if (book == null) {
System.out.println("Book not found or not available.");
return;
}
borrower.borrowBook(book);
book.setAvailability(false);
System.out.println("Book borrowed successfully.");
}
public static void returnBook() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter borrower Name: ");
String name= sc.nextLine();
System.out.print("Enter the Title of the Book: ");
String title = sc.nextLine();
Borrower borrower = null;
Book book = null;
for (Borrower b : borrowers) {
if (b.getName().equals(name)) {
borrower = b;
break;
}
}
if (borrower == null) {
System.out.println("Borrower not found.");
return;
}
for (Book b : borrower.getBorrowedBooks()) {
if (b.getTitle().equals(title)) {
book = b;
break;
}
}
if (book == null) {
System.out.println("Book not found or not borrowed by this borrower.");
return;
}
borrower.returnBook(book);
book.setAvailability(true);
System.out.println("Book returned successfully.");
}
public static void printAvailableBooks() {
System.out.println("\nList of available books:");
for (Book book : stock) {
if (book.isAvailable()) {
System.out.println(book.title);
}
}
}
public static void printBorrowedBooks() {
System.out.println("\nList of borrowed books:");
for (Book book : stock) {
if (!book.isAvailable()) {
System.out.println(book.title);
}
}
}
public static void viewBorrowedBooks() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter borrower Name: ");
String name = sc.nextLine();
Borrower borrower = null;
for (Borrower b : borrowers) {
if (b.getName().equals(name)) {
borrower = b;
break;
}
}
if (borrower == null) {
System.out.println("Borrower not found.");
return;
}
System.out.println("\nBorrowed books by " + borrower.getName() + ":");
for (Book book : borrower.getBorrowedBooks()) {
System.out.println(book.title);
}
}
}
class Bk {
private String title;
private boolean isAvailable;
public Bk(String ISBN, String title, String author, int publicationYear) {
this.title = title;
this.isAvailable = true;
}
public String getTitle() {
return title;
}
public boolean isAvailable() {
return isAvailable;
}
public void setAvailability(boolean availability) {
isAvailable = availability;
}
@Override
public String toString() {
return "Book{" +
", title='" + title + '\'' +
", isAvailable=" + isAvailable +
'}';
}
}
class Br {;
private String name;
private List<Book> borrowedBooks;
public Br( String name) {
this.name = name;
this.borrowedBooks = new ArrayList<>();
}
public String getName() {
return name;
}
public List<Book> getBorrowedBooks() {
return borrowedBooks;
}
public void borrowBook(Book book) {
borrowedBooks.add(book);
}
public void returnBook(Book book) {
borrowedBooks.remove(book);
}
@Override
public String toString() {
return "Borrower{" +
", name='" + name + '\'' +
'}';
}
}