-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibrary.java
259 lines (218 loc) · 8.23 KB
/
Library.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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ListIterator;
import java.util.Scanner;
/** Structure of the library is set here
* all methods needed to be implemented in the main are created here.
*
* @author b1014963 , Abdul Al-FARAJ
* @Date 30/10/2012
*/
public class Library {
private SortedLinkedList<Book> booksLibrary;
private SortedLinkedList<User> usersLibrary;
File log;
public Library() throws IOException
{
booksLibrary = new SortedLinkedList<Book>();
usersLibrary = new SortedLinkedList<User>();
log= new File ("H:\\ECE Year 2012-2013\\Semester 1\\CSC2011 - Advance Programming\\Practicle\\CourseWork ( week 3 & 4 )\\src\\outputs.txt");
}
//Load books from txt file.
public void loadBooksUsers() throws FileNotFoundException
{
File inputFile = new File("H:\\ECE Year 2012-2013\\Semester 1\\CSC2011 - Advance Programming\\Practicle\\CourseWork ( week 3 & 4 )\\src\\inputLibrary.txt");
Scanner bookUsersScanner = new Scanner (inputFile);
/** The Following is to save the information about the Books and their authors.*/
int noOfBooks= bookUsersScanner.nextInt();
bookUsersScanner.nextLine(); // to skip the first empty line.
int i=0; // to increment.
while (bookUsersScanner.hasNext() && i<noOfBooks)
{
String TempBook=bookUsersScanner.nextLine();
String TempAuther= bookUsersScanner.nextLine();
String[] splits = TempAuther.split(" ");
Book tempBook = new Book (TempBook, splits[0],splits[1]); // assuming there is only one surname
booksLibrary.insert(tempBook);
i++;
}
int noOfUsers= bookUsersScanner.nextInt();
bookUsersScanner.nextLine(); // to skip the first empty line.
i=0;
while (bookUsersScanner.hasNext() && i<noOfUsers)
{
String tempUserName = bookUsersScanner.nextLine();
String[] splits = tempUserName.split(" ");
User tempUser = new User (splits[0],splits[1]); // assuming there is only one surname
usersLibrary.insert(tempUser);
i++;
}
bookUsersScanner.close();
}
public SortedLinkedList<Book> getBookList()
{
return this.booksLibrary;
}
public SortedLinkedList<User> getUserList()
{
return this.usersLibrary;
}
public char getChar(){
Scanner charScan = new Scanner(System.in);
String input = charScan.next();
while(input.length()!=1)
{
System.out.println("\nInvalid instruction, please try again: \n");
input=charScan.next();
}
char[] split = input.toCharArray();
return split[0];
}
// case b- books display
public void printBooks()
{
ListIterator<Book> bookIter = this.getBookList().listIterator();
while (bookIter.hasNext())
{
System.out.print(bookIter.next());
}
}
//case u- users display
public void printUsers()
{
ListIterator<User> bookIter = this.getUserList().listIterator();
while (bookIter.hasNext())
{
System.out.print(bookIter.next());
}
}
//case i- issue books
public void issueBook() throws IOException
{
PrintWriter outputLibrary =new PrintWriter(new FileWriter(log, true)); // This won't overwrite the content of the text file, it will add upon the file.
Scanner bookSearch = new Scanner (System.in);
System.out.println("\n To issue a book, please type the user surname: "); // we assume no user have similar names, as the assignment specification indicates
String wantedUser = bookSearch.nextLine();
boolean bookFound=false, userFound = false;
// 1st: Find User
User tempUser=null;
String tempUserSurname;
ListIterator<User> userIter = this.getUserList().listIterator();
while (userIter.hasNext() && !userFound){
tempUser=userIter.next();
tempUserSurname= tempUser.getLast();
if(tempUserSurname.compareToIgnoreCase(wantedUser)==0){
userFound=true;
}
}
//2nd: Find book, userFound=true
if (userFound)
{
System.out.println("\n User has been found, please type in the book name that you would like to issue: ");
String wantedBook = bookSearch.nextLine();
Book tempElem;
String tempTitle;
ListIterator<Book> bookIter = this.getBookList().listIterator();
while (bookIter.hasNext() && !bookFound)
{
tempElem = bookIter.next();
tempTitle = tempElem.getBookTitle();
if (tempTitle.compareToIgnoreCase(wantedBook)==0)
{
bookFound=true;
//3rd: is book borrowed?
if(!tempElem.isBorrowed()){
/**Update starts here*/
//4th: maximum number of books?
if (tempUser.getNumOfBooks()<3){
tempUser.borrowBook();
tempElem.setBorrowed(true);
tempElem.setTheBorrower(tempUser);
System.out.println("\nBook is found, and it is not borrowed"
+"\nThe following Book: " + tempElem.getBookTitle() + " Been issued to: "+tempElem.getTheBorrower());
}else{
System.out.println("User: " + tempUser.getUserName() + " has maximum number of book, " + tempUser.getFirst()+" can't borrow any more books.");
}
}else{
//Book is found but borrowed:
System.out.println("Book: " + tempElem.getBookTitle() + " is borrowed. \nAn appropriate letter will be generated for: " + tempElem.getTheBorrower());
outputLibrary.println("\n------------------------------------------------------------------------------");
outputLibrary.println("\n");
outputLibrary.println("\nDear:"+tempElem.getTheBorrower().getLast() );
outputLibrary.println("Please return the following books as soon as possible. It has been requested by a different user.");
outputLibrary.println("\nBook title:" + tempElem.getBookTitle()+"\n");
outputLibrary.println("\n");
outputLibrary.println("\n");
outputLibrary.println("\nThank you very much, Newcastle CS Library" );
outputLibrary.println("\n------------------------------------------------------------------------------");
}
}
}
//if book not found
if (!bookFound){
System.out.println("Book: " + wantedBook+ " is not found!");
}
//if user isn't found
}else{
System.out.println("User: " + wantedUser + " has not been found");
}
outputLibrary.close();
}
//case r- return books
public void returnBook()
{
boolean bookFound = false, userFound=false;
User tempUser = null; Book tempBook = null;
String tempBorrowerSurname = null, tempTitle = null;
Scanner returnedBook = new Scanner (System.in);
System.out.println("Please type in user's surname who is returning the book: ");
String returnerSurname = returnedBook.nextLine();
/**is user found?*/
ListIterator<User> userIter = this.getUserList().listIterator();
while (userIter.hasNext() && !userFound)
{
tempUser=userIter.next();
if(tempUser.getLast().compareToIgnoreCase(returnerSurname)==0){
userFound=true;
}
}
/**If user found, find book!*/
if(userFound){
ListIterator<Book> bookIter = this.getBookList().listIterator();
System.out.println("Please type in book title which you would like to return: ");
tempTitle= returnedBook.nextLine();
while (bookIter.hasNext() && !bookFound){
tempBook=bookIter.next();
if (tempBook.getBookTitle().compareToIgnoreCase(tempTitle)==0){
bookFound=true;
//check if its borrowed
if(tempBook.isBorrowed()){
tempBorrowerSurname=tempBook.getTheBorrower().getLast();
//check if they are the same as who is returning the book.
if ( tempBorrowerSurname.compareToIgnoreCase(returnerSurname)==0 ){
//update the library
tempBook.setBorrowed(false);
tempBook.setTheBorrower(null);
tempUser.returnBook();
System.out.println("Book is successfuly returned.");
}else{
System.out.println("User who borrowed the book is not the same as the user returing it....");
}
}else{
System.out.println("\nThe current book is not borrowed, please try again.");
}
}
}
/**if book isn't found*/
if(!bookFound){
System.out.println("The requried book is not found, please try again from the main menu, thanks....");
}
/**if user isn't found*/
} else{
System.out.println("User is not found");
}
}
}