-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.java
More file actions
333 lines (315 loc) · 12.3 KB
/
Library.java
File metadata and controls
333 lines (315 loc) · 12.3 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
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
322
323
324
325
326
327
328
329
330
331
332
333
import java.util.ArrayList;
import java.util.Scanner;
class Book {
int bookId;
String title;
String author;
int yearPublished;
boolean isAvailable = true;
int days = 3;
boolean searchBook(String title) {
if (title.equals(this.title)) {
DisplayInfo();
return true;
}
return false;
}
boolean searchBook(String author, int yearPublished) {
if (author.equals(this.author) && yearPublished == this.yearPublished) {
DisplayInfo();
return true;
}
return false;
}
boolean searchBook(int bookId) {
if (bookId == this.bookId) {
DisplayInfo();
return true;
}
return false;
}
boolean issueBook(int bookId, int patronId) {
if (isAvailable == true) {
isAvailable = false;
System.out.println("Book issued sucessfully for 3 days");
return true;
} else {
System.out.println("Sorry Not available");
return false;
}
}
boolean issueBook(int bookId, int patronId, int days) {
if (isAvailable == true) {
isAvailable = false;
System.out.println("Book issued sucessfully for " + days + " days");
this.days = days;
return true;
} else {
System.out.println("Sorry Not available");
return false;
}
}
void DisplayInfo() {
System.out.println("Book Id:" + bookId +
"\nTitle:" + title + "\nAuthor:" +
author + "\nYear of publication: " + yearPublished + "\nAvailability: " + isAvailable + "\n");
}
}
class Patron {
int patronId = 1;
String name;
String membershipType;
int age = 18;
ArrayList<Book> bookIssued = new ArrayList<>();
void addPatron(String name, String membershipType, int patronId) {
this.name = name;
this.membershipType = membershipType;
this.patronId = patronId;
}
void addPatron(String name, String membershipType, int age, int patronId) {
this.name = name;
this.membershipType = membershipType;
this.patronId = patronId;
this.age = age;
}
void viewIssuedBooks() {
boolean key = false;
for (int i = 0; i < bookIssued.size(); i++) {
key = true;
bookIssued.get(i).DisplayInfo();
}
if (key == false) {
System.out.println("No books Issued\n");
}
}
}
public class Library {
static int key3 = 001;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Book> b = new ArrayList<>();
// addBook
int key = 0;
int patkey = 1;
boolean mark = false;
String name;
String membershipType;
int age;
int days;
int i = 0;
int key2 = 0;
int patronId;
String title;
String author;
int yearPublished;
int bookId;
Patron kp = new Patron();
ArrayList<Patron> p = new ArrayList<>();
b.add(addBook("Ramayana", "Valmiki", 1905));
b.add(addBook("Mahabharata", "Vasista", 1909));
b.add(addBook("Bhagavath Geetha", "Sri Krishna", 1920));
b.add(addBook("Panchatantra", "Darmaseena", 1958));
kp.addPatron("Kavana", "Regular", patkey++);
p.add(kp);
kp = new Patron();
kp.addPatron("ammu", "Premium", patkey++);
p.add(kp);
kp = new Patron();
kp.addPatron("acchu", "Premium", patkey++);
p.add(kp);
while (true) {
System.out.println(
"Enter 0:New Member\nEnter 1:Add a book to your patron id\nEnter 2:Return a book\nEnter 3:View your Issued Books\nEnter 4:Search a book\nEnter 5:Add Book(office use only)");
key = sc.nextInt();
switch (key) {
case 0: {
System.out.println("Welcome,Please Enter your name: ");
name = sc.next();
System.out.println("Membership Type(Regular/Premium): ");
membershipType = sc.next();
System.out.println("Would you like to share your age,Yes/No");
if (sc.next().equals("yes")) {
System.out.println("Age: ");
age = sc.nextInt();
kp = new Patron();
kp.addPatron(name, membershipType, age, patkey++);
p.add(kp);
} else {
kp = new Patron();
kp.addPatron(name, membershipType, patkey++);
p.add(kp);
}
System.out.println("Your patron id is: " + kp.patronId);
break;
}
case 1: {
System.out.println("Please Enter your Patron Id: ");
patronId = sc.nextInt();
mark = false;
for (i = 0; i < p.size(); i++) {
if (patronId == p.get(i).patronId) {
mark = true;
break;
}
}
if (mark == true) {
System.out.println("Enter the number of days of booking(minimun 3 days)");
days = sc.nextInt();
if (days == 3) {
System.out.println("Enter the book ID");
bookId = sc.nextInt();
for (int j = 0; j < b.size(); j++) {
if (bookId == b.get(j).bookId) {
mark = b.get(j).issueBook(bookId, patronId);
if (mark == true) {
p.get(i).bookIssued.add(b.get(j));
}
}
}
} else {
System.out.println("Enter the book ID");
bookId = sc.nextInt();
for (int j = 0; j < b.size(); j++) {
if (bookId == b.get(j).bookId) {
mark = b.get(j).issueBook(bookId, patronId, days);
if (mark == true) {
p.get(i).bookIssued.add(b.get(j));
}
}
}
}
}
break;
}
case 2: {
System.out.println("Please Enter your patron Id: ");
patronId = sc.nextInt();
mark = false;
for (i = 0; i < p.size(); i++) {
if (patronId == p.get(i).patronId) {
mark = true;
break;
}
}
if (mark == true) {
System.out.println("Enter the book ID");
bookId = sc.nextInt();
for (int j = 0; j < b.size(); j++) {
if (bookId == b.get(j).bookId) {
System.out.println("Please enter the number of days Delayed");
days = sc.nextInt();
if (days == 0) {
returnBook(b.get(j), p.get(i));
} else {
returnBook(b.get(j), p.get(i), days);
}
}
}
}
break;
}
case 3: {
System.out.println("Please Enter your patron Id: ");
patronId = sc.nextInt();
mark = false;
for (i = 0; i < p.size(); i++) {
if (patronId == p.get(i).patronId) {
mark = true;
break;
}
}
if (mark == true) {
p.get(i).viewIssuedBooks();
}
break;
}
case 4: {
System.out.println(
"Enter 1:To search by title\nEnter 2:Author name and Year of publication\nEnter 3:BookId");
key2 = sc.nextInt();
switch (key2) {
case 1: {
System.out.println("Enter the title: ");
title = sc.next();
for (int j = 0; j < b.size(); j++) {
mark = b.get(j).searchBook(title);
if (mark == true) {
break;
}
}
break;
}
case 2: {
System.out.println("Enter the Author name and Year of publication");
author = sc.next();
yearPublished = sc.nextInt();
for (int j = 0; j < b.size(); j++) {
mark = b.get(j).searchBook(author, yearPublished);
if (mark == true) {
break;
}
}
break;
}
case 3: {
System.out.println("Enter the bookId: ");
bookId = sc.nextInt();
for (int j = 0; j < b.size(); j++) {
mark = b.get(j).searchBook(bookId);
if (mark == true) {
break;
}
}
break;
}
default:
System.out.println("Wrong Input");
}
break;
}
case 5: {
System.out.println("Please Enter the Book title: ");
title = sc.next();
System.out.println("Author: ");
author = sc.next();
System.out.println("Year od publication: ");
yearPublished = sc.nextInt();
b.add(addBook(title, author, yearPublished));
break;
}
default:
System.out.println("Wrong Input");
}
}
}
static Book addBook(String title, String author, int yearPublished) {
Book b1 = new Book();
b1.title = title;
b1.author = author;
b1.yearPublished = yearPublished;
b1.bookId = key3++;
System.out.println();
b1.DisplayInfo();
return b1;
}
static Book addBook(Book b) {
Book b1 = new Book();
b1.title = b.title;
b1.author = b.author;
b1.yearPublished = b.yearPublished;
b1.bookId = key3++;
b1.DisplayInfo();
return b1;
}
static void returnBook(Book b, Patron p) {
b.isAvailable = true;
System.out.println("Book returned sucessfully\n");
p.bookIssued.remove(b);
}
static void returnBook(Book b, Patron p, int days) {
b.isAvailable = true;
double delayCharges = (days) * 7;
System.out.println("Delay charges: " + delayCharges);
p.bookIssued.remove(b);
}
}