-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBooks.java
168 lines (147 loc) · 5.83 KB
/
Books.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
// Java Program to Illustrate books class
// To Do all the Operations related to Books such as
// add, check-in, check-out,Valid books,Update books
// Importing required classes
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
// CLass
// Class
public class Books {
// Class data members
Book theBooks[] = new Book[50];
public static int count;
Scanner input = new Scanner(System.in);
// Method to compare books
public int compareBookObjects(Book b1, Book b2) {
if (b1.bookName.equalsIgnoreCase(b2.bookName)) {
System.out.println("Book of this Name Already Exists.");
return 0;
}
if (b1.sNo == b2.sNo) {
System.out.println("Book of this Serial No Already Exists.");
return 0;
}
return 1;
}
// Method to add book
public void addBook(Book b) {
for (int i = 0; i < count; i++) {
if (this.compareBookObjects(b, this.theBooks[i]) == 0)
return;
}
if (count < 50) {
theBooks[count] = b;
count++;
} else {
System.out.println("No Space to Add More Books.");
}
}
// Method to search book by serial number
public void searchBySno() {
System.out.println("\t\t\t\tSEARCH BY SERIAL NUMBER\n");
int sNo;
System.out.println("Enter Serial No of Book:");
sNo = input.nextInt();
int flag = 0;
System.out.println("S.No\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");
for (int i = 0; i < count; i++) {
if (sNo == theBooks[i].sNo) {
System.out.println(theBooks[i].sNo + "\t\t" + theBooks[i].bookName + "\t\t" + theBooks[i].authorName + "\t\t" + theBooks[i].bookQtyCopy + "\t\t" + theBooks[i].bookQty);
flag++;
return;
}
}
if (flag == 0)
System.out.println("No Book for Serial No " + sNo + " Found.");
}
// Method to search author by name
public void searchByAuthorName() {
System.out.println("\t\t\t\tSEARCH BY AUTHOR'S NAME");
input.nextLine();
System.out.println("Enter Author Name:");
String authorName = input.nextLine();
int flag = 0;
System.out.println("S.No\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");
for (int i = 0; i < count; i++) {
if (authorName.equalsIgnoreCase(theBooks[i].authorName)) {
System.out.println(theBooks[i].sNo + "\t\t" + theBooks[i].bookName + "\t\t" + theBooks[i].authorName + "\t\t" + theBooks[i].bookQtyCopy + "\t\t" + theBooks[i].bookQty);
flag++;
}
}
if (flag == 0)
System.out.println("No Books of " + authorName + " Found.");
}
// Method to display all books
public void showAllBooks() {
System.out.println("\t\t\t\tSHOWING ALL BOOKS\n");
System.out.println("S.No\t\tName\t\tAuthor\t\tAvailable Qty\t\tTotal Qty");
for (int i = 0; i < count; i++) {
System.out.println(theBooks[i].sNo + "\t\t" + theBooks[i].bookName + "\t\t" + theBooks[i].authorName + "\t\t" + theBooks[i].bookQtyCopy + "\t\t" + theBooks[i].bookQty);
}
}
// Method to edit the book
public void upgradeBookQty() {
System.out.println("\t\t\t\tUPGRADE QUANTITY OF A BOOK\n");
System.out.println("Enter Serial No of Book");
int sNo = input.nextInt();
for (int i = 0; i < count; i++) {
if (sNo == theBooks[i].sNo) {
System.out.println("Enter No of Books to be Added:");
int addingQty = input.nextInt();
theBooks[i].bookQty += addingQty;
theBooks[i].bookQtyCopy += addingQty;
return;
}
}
}
// Method to create menu
public void dispMenu() {
System.out.println("----------------------------------------------------------------------------------------------------------");
System.out.println("Press 1 to Add new Book.");
System.out.println("Press 0 to Exit Application.");
System.out.println("Press 2 to Upgrade Quantity of a Book.");
System.out.println("Press 3 to Search a Book.");
System.out.println("Press 4 to Show All Books.");
System.out.println("Press 5 to Register Student.");
System.out.println("Press 6 to Show All Registered Students.");
System.out.println("Press 7 to Check Out Book.");
System.out.println("Press 8 to Check In Book");
System.out.println("-------------------------------------------------------------------------------------------------------");
}
// Method to search the library
public int isAvailable(int sNo) {
for (int i = 0; i < count; i++) {
if (sNo == theBooks[i].sNo) {
if (theBooks[i].bookQtyCopy > 0) {
System.out.println("Book is Available.");
return i;
}
System.out.println("Book is Unavailable");
return -1;
}
}
System.out.println("No Book of Serial Number Available in Library.");
return -1;
}
// Method to remove the book from the library
public Book checkOutBook() {
System.out.println("Enter Serial No of Book to be Checked Out.");
int sNo = input.nextInt();
int bookIndex = isAvailable(sNo);
if (bookIndex != -1) {
theBooks[bookIndex].bookQtyCopy--;
return theBooks[bookIndex];
}
return null;
}
// Method to add the Book to the Library
public void checkInBook(Book b) {
for (int i = 0; i < count; i++) {
if (b.equals(theBooks[i])) {
theBooks[i].bookQtyCopy++;
return;
}
}
}
}