-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookCat.java
More file actions
228 lines (189 loc) · 6.16 KB
/
BookCat.java
File metadata and controls
228 lines (189 loc) · 6.16 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
221
222
223
224
225
226
227
228
//Java program to implement a catalogue for books and notes
import java.io.*;
import java.util.*;
//Definition of book class
class Book{
//length breadth width
private int l,b,w;
//Name of the book
private String name;
//Weight of the book
private int weight;
//index of each book mapped to its name
private int index;
//Function to set dimensions of the book
public void setDimensions(int length , int breadth , int width){
this.l = length;
this.b = breadth;
this.w = width;
this.weight = l*b*w*2;
}
//Function to return the weight of the book
public int getWeight(){
return this.weight;
}
//Function to set name of the book
public void setName(String name){
this.name = name;
}
//Function to return the name of the book
public String getName(){
return this.name;
}
//Function to set the index
public void setIndex(int index){
this.index = index;
}
//Function to return index of the book
public int getIndex(){
return this.index;
}
}
//Definition of Notes class
class Notes{
//Number of pages
private int numOfPg;
//Name of the notes
private String name;
//Index of the notes
private int index;
//Function to set the number of pages of the notes
public void setDimensions(int num){
this.numOfPg = num;
}
//Function to returtn the number of page
public int getPage(){
return this.numOfPg;
}
//Function to set name of notes
public void setName(String name){
this.name = name;
}
//Functiont to return the name of the notes
public String getName(){
return this.name;
}
//Function to set the index
public void setIndex(int index){
this.index = index;
}
//Function to return the index of the notes
public int getIndex(){
return this.index;
}
}
/**
* INterface for catalogue
*/
interface Catalogue {
//Function to return the weight of book
int weight(Book b);
//Function to return the number of pages for the notes
int weight(Notes n);
//Function to sort the books on the basis of their weights
void sort(Book b[] , int size);
//Function to sort the notes on the basis of the number of pages they contain
void sort(Notes n[], int size);
//Function to search a book from the catalogue
int search(Book b[] , int size , String name);
//Function to search a notes from the catalogue
int search(Notes b[] , int size , String name);
}
//Implementer class for catalogue
class X implements Catalogue{
//Functtion to return the weight of the book
public int weight(Book b){
return b.getWeight();
}
//Function to return the number of pages of the notes
public int weight(Notes n){
return n.getPage();
}
//Function to sort the books on the basis of their weights
//Bubble sort logic
public void sort(Book b[], int size){
for(int i = 0 ; i < size ; i++){
for(int j = 1 ; j < size - i ; j++){
if(weight(b[j]) < weight(b[j-1])){
Book temp = b[j];
b[j] = b[j-1];
b[j-1] = temp;
}
}
b[size - 1 - i].setIndex(size - i);
}
}
//Function to sort the notes on the basis of the number of pages they contain
//Bubble sort logic
public void sort(Notes n[], int size){
for(int i = 0 ; i < size ; i++){
for(int j = 1 ; j < size - i ; j++){
if(weight(n[j]) < weight(n[j-1])){
Notes temp = n[j];
n[j] = n[j-1];
n[j-1] = temp;
}
}
n[size - 1 - i].setIndex(size - i);
}
}
//Function to search a book from catalogue
public int search(Book b[] , int size , String name){
int index = 0;
for(int i = 0 ; i < size ; i++)
if(name.equals(b[i].getName()))
index = b[i].getIndex();
return index;
}
//Function to search notes from the catalogue
public int search(Notes n[] , int size , String name){
int index = 0;
for(int i = 0 ; i < size ; i++)
if(name.equals(n[i].getName()))
index = n[i].getIndex();
return index;
}
}
class BookCat{
//Main Function
public static void main(String args[])throws IOException{
X obj = new X();
Scanner wats = new Scanner(System.in);
System.out.println("Enter the number of books and notes");
int bSize = wats.nextInt();
int nSize = wats.nextInt();
Book b[] = new Book[bSize];
Notes n[] = new Notes[nSize];
for(int i = 0 ; i < bSize ; i++){
System.out.println("Enter the name of the Book");
String name = wats.next();
b[i] = new Book();
b[i].setName(name);
int length , breadth , width;
System.out.println("Enter the length breadth and width of the book");
length = wats.nextInt();
breadth = wats.nextInt();
width = wats.nextInt();
b[i].setDimensions(length , breadth , width);
b[i].setIndex(i);
}
for(int i = 0 ; i < nSize ; i++){
System.out.println("Enter the name of the Notes");
String name = wats.next();
n[i] = new Notes();
n[i].setName(name);
System.out.println("Enter the number of pages in the Notes");
int nop = wats.nextInt();
n[i].setDimensions(nop);
n[i].setIndex(i);
}
obj.sort(b , bSize);
obj.sort(n , nSize);
System.out.println("Enter the name of the book you want to search");
String name = wats.next();
System.out.printf("Index of the book you searched is %d\n",obj.search(b , bSize , name));
System.out.println("Enter the name of the notes you want to search");
name = wats.next();
System.out.printf("Index of the book you searched is %d\n",obj.search(n , nSize , name));
}
}