-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrarySystem.java
More file actions
220 lines (184 loc) · 6.95 KB
/
LibrarySystem.java
File metadata and controls
220 lines (184 loc) · 6.95 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
package LibraryProject;
import java.util.LinkedList;
import java.util.ListIterator;
public class LibrarySystem {
// data members
private LinkedList<Book> booksList;
private LinkedList<LibMember> memberList;
private int bookListSize;
private int membersListSize;
// default constructor
public LibrarySystem () {
booksList = new LinkedList<Book>();
memberList = new LinkedList<LibMember>();
bookListSize = 0;
membersListSize = 0;
}
// add book method: adds a book to the end of bookList
public boolean addBook (Book book) {
if(searchBook(book.getAccessionNum()) != -1) {
System.out.println("Book already exists.");
return false;
}
booksList.add(book);
bookListSize++;
System.out.println("Book added successfully.");
return true;
}
// delete book method: deletes the book from bookList
public boolean deleteBook (long accNum) {
int bookIndex = searchBook(accNum);
if (bookIndex == -1 || isBookIssued(accNum)) {
System.out.println("Cannot delete book.");
return false;
}
booksList.remove(bookIndex);
bookListSize--;
System.out.println("Book successfully deleted");
return true;
}
// add member method: adds a member to the end of memberList
public boolean addMember (LibMember member) {
if(searchMember(member.getCprNum()) != -1) {
System.out.println("Member already exists.");
return false;
}
memberList.add(member);
membersListSize++;
System.out.println("Member added successfully.");
return true;
}
// delete member method: deleted a member from memberList
public boolean deleteMember (long cpr) {
int index = searchMember(cpr);
if(index != -1)
{
LibMember member1 = memberList.get(index);
if(member1.getNumBooksIssued() == 0)
{
memberList.remove(index);
membersListSize--;
System.out.println("Member successfully removed.");
return true;
}
}
System.out.println("Cannot remove member.");
return false;
}
// search book method: returns the index of the book in bookList. if not found returns -1
public int searchBook (long accNum) {
if (booksList.isEmpty())
return -1;
ListIterator<Book> iter = booksList.listIterator();
int index = 0;
while (iter.hasNext())
{
Book book = iter.next();
if (book.getAccessionNum() == accNum)
return index;
index++;
}
return -1;
}
// search member method: returns the index of the member in memberList. if not found returns -1
public int searchMember (long cpr) {
if (memberList.isEmpty())
return -1;
ListIterator<LibMember> iter = memberList.listIterator();
int index = 0;
while (iter.hasNext())
{
LibMember member = iter.next();
if (member.getCprNum() == cpr)
return index;
index++;
}
return -1;
}
// empty methods: returns true if the list is empty else returns fasle
public boolean isEmptyBooksList () {
return booksList.isEmpty();
}
public boolean isEmptyMembersList () {
return memberList.isEmpty();
}
// size methods: returns the data member size for bookList and memberList, respectively
public int sizeBooksList () {
return bookListSize;
}
public int sizeMembersList () {
return membersListSize;
}
// issue book method: issues a book to a member if the book exists and the member satisfies the conditions
public boolean issueBook (long accNum, long cpr) {
int bookIndex = searchBook(accNum);
int memberIndex = searchMember(cpr);
if (bookIndex == -1 || memberIndex == -1 || isBookIssued(accNum) || memberList.get(memberIndex).getNumBooksIssued() >= 10) {
System.out.println("Book cannot be issued");
return false;
}
LibMember member = memberList.get(memberIndex);
Book book = booksList.get(bookIndex);
member.getBooksIssued()[member.getNumBooksIssued()] = book;
member.setNumBooksIssued(member.getNumBooksIssued() + 1);
book.setIssuedTo(member);
System.out.println("Book successfully issued.");
return true;
}
// return book method: the method returns an already issued book back to the library
public boolean returnBook (long accNum) {
int bookIndex = searchBook(accNum);
if (bookIndex == -1 || !isBookIssued(accNum)) {
System.out.println("Book cannot be returned");
return false;
}
LibMember member = booksList.get(bookIndex).getIssuedTo();
Book[] booksIssued = member.getBooksIssued();
int issuedSize = member.getNumBooksIssued();
int removeIndex = 0;
for (int i = 0; i < issuedSize; i++)
{
if (booksIssued[i].equals(booksList.get(bookIndex)))
{
for (int j = removeIndex; j < issuedSize - 1; j++)
booksIssued[j] = booksIssued[j+1];
member.setNumBooksIssued(member.getNumBooksIssued() - 1);
member.setBooksIssued(booksIssued);
booksList.get(bookIndex).setIssuedTo(null);
System.out.println("Book successfully returned.");
return true;
}
removeIndex++;
}
return false;
}
// print books issued: prints all the books issued to a certain member
public void printBooksIssued(long cpr)
{
int memberIndex = searchMember(cpr);
if(memberIndex != -1)
{
LibMember member = memberList.get(memberIndex);
Book[] books = member.getBooksIssued();
int booksNum = member.getNumBooksIssued();
if (booksNum == 0)
System.out.println("There are no books issued for this member.");
else {
for (int i = 0; i < booksNum; i++)
System.out.println(books[i]);
}
}
else
System.out.println("Member not found.");
}
// Is book issued method: returns true if a book is already issued to a member else returns false
public boolean isBookIssued (long accessionNum) {
int index = searchBook(accessionNum);
if(index == -1)
return false;
LibMember issued = booksList.get(index).getIssuedTo();
if (issued == null)
return false;
return true;
}
}