-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHasmap_Library.java
More file actions
157 lines (136 loc) · 4.62 KB
/
Hasmap_Library.java
File metadata and controls
157 lines (136 loc) · 4.62 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
package com.fresco;
import java.util.HashMap;
import java.util.Objects;
class Library
{
String bookName;
String author;
Library()
{
}
@Override
public int hashCode() {
int hash = 3;
hash = 83 * hash + Objects.hashCode(this.bookName);
hash = 83 * hash + Objects.hashCode(this.author);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Library other = (Library) obj;
if (!Objects.equals(this.bookName, other.bookName)) {
return false;
}
if (!Objects.equals(this.author, other.author)) {
return false;
}
return true;
}
Library(String bookName,String author)
{
this.bookName=bookName;
this.author=author;
}
public HashMap<Integer,Library> createLibraryMap(String booksInLibrary)
{
String[] str=booksInLibrary.split("\\|");
String[] temp_str=null;
HashMap<Integer,Library> hs1=new HashMap<>();
for(String temp:str) {
// System.out.println(temp);
temp_str=temp.split("\\,");
hs1.put(Integer.parseInt(temp_str[0]),new Library(temp_str[1],temp_str[2]));
}
return hs1;
}
public HashMap<Integer,Integer> createUserMap(String borrowedUsers)
{
String[] str=borrowedUsers.split("\\|");
String[] temp_str=null;
HashMap<Integer,Integer> hs2=new HashMap<>();
for(String temp:str) {
// System.out.println(temp);
temp_str=temp.split("\\,");
// System.out.println(temp_str[0]);
hs2.put(Integer.parseInt(temp_str[0]), Integer.parseInt(temp_str[1]));
}
return hs2;
}
public String getQuery(String booksInLibrary,String borrowedUsers,String query)
{
HashMap<Integer,Library> hs_book=createLibraryMap(booksInLibrary);
HashMap<Integer,Integer> hs_user=createUserMap(borrowedUsers);
String[] temp_str = query.split("\\,");
int q=Integer.parseInt(temp_str[0]);
if(q==1){
// System.out.println(hs_book);
int book_id=Integer.parseInt(temp_str[1]);
if(hs_book.containsKey(book_id) && !hs_user.containsKey(book_id)){
return "It is available\nAuthor is "+hs_book.get(book_id).author+"\n";
}
else{
return "No Stock\nIt is owned by "+hs_user.get(book_id).toString()+"\n";
}
}
else if(q==2){
int user_id=Integer.parseInt(temp_str[1]);
String str="";
for (Integer key : hs_user.keySet()) {
if(hs_user.get( key ).equals( user_id )){
str+=key+" "+hs_book.get(key).bookName+"\n";
}
}
return str;
}
else if(q==3){
String book_name=temp_str[1];
int count_in=0;
int count_out=0;
for (Integer key : hs_book.keySet()){
String temp_book_name=hs_book.get(key).bookName;
if(book_name.equals(temp_book_name)){
count_in+=1;
// System.out.println(key);
for(Integer key2 : hs_user.keySet()){
if(key2.equals(key)){
count_out+=1;
}
}
}
}
return count_out+" out\n"+(count_in-count_out)+" in\n";
}
else if(q==4) {
String author_name=temp_str[1];
String res="";
for (Integer key : hs_book.keySet()) {
String temp_author_name=hs_book.get(key).author;
if(author_name.equals(temp_author_name)) {
res+=hs_book.get(key).bookName+"\n";
}
}
return res;
}
else if(q==5) {
String search_key=temp_str[1];
String res="";
for (Integer key : hs_book.keySet()) {
String temp_book_name=hs_book.get(key).bookName.toLowerCase();
if(temp_book_name.contains(search_key)) {
res+=key+" "+hs_book.get(key).bookName+"\n";
}
}
return res;
}
return null;
}
}