-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibraryStorage.java
96 lines (85 loc) · 2.78 KB
/
LibraryStorage.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
import java.util.*;
import Includes.*;
public class LibraryStorage {
// HashMap
// process return
private HashMap<Integer, BookData> storage;
private RequestQueue rqQueue;
private PendingRequests prLinkedList;
public LibraryStorage() {
storage = new HashMap<Integer, BookData>();
for (int i=100001; i<100011; i++) {
BookData book = new BookData();
MyDate dateor = new MyDate();
dateor.month = 0;
dateor.year = 0;
book.BorrowedStatus = false;
book.BorrowedByUserID = 0;
book.ISBN = i;
book.Publisher = "publisher";
book.Author = "author";
book.DateOfReturn = dateor;
storage.put(i, book);
}
rqQueue = new RequestQueue();
prLinkedList = new PendingRequests();
}
public void push(int ISBN, int UserID) {
rqQueue.push(ISBN, UserID);
return;
}
public boolean processQueue() {
/*
* Your code here.
*/
// iterate over all requests in requestQueue
// I can iterate using front of requestQueue
RequestData front = rqQueue.getFront();
while (front != null ){
// check if book is available in library and not borrowed
int key = front.ISBN;
BookData book = storage.get(key);
if (storage.containsKey(key) && book.BorrowedStatus == false ){
// return true and change borrowed status , userid , pop front from request queue
book.BorrowedStatus = true;
book.BorrowedByUserID=front.UserID;
rqQueue.pop();
return true;
}
// if borrowed , add in pending linklist and pop queue in this case return false
//if book is not available return false and also add this into pending linklist and pop
else{
Node<RequestData> a = new Node<>();
a.data=front;
prLinkedList.insert(a);
rqQueue.pop();
return false;
}
}
return false;
}
public boolean processReturn(BookData book) { // I have assumed this takes BookData object as argument, can also work with ISBN perhaps
/*
* Your code here.
*/
//check if returened book is in pending list
Node<RequestData> elem = prLinkedList.find(book.ISBN);
if (elem != null){
// if in pending list then change the book borrowed user id , and remove it from pending list
book.BorrowedByUserID = elem.data.UserID;
prLinkedList.delete(elem);
return true;
}
else{
book.BorrowedStatus=false;
book.BorrowedByUserID = -1 ;
return false ;
}
}
public String rqQueueToString(){
return rqQueue.toString();
}
public String prLinkedListToString(){
return prLinkedList.toString();
}
}