Repository navigation
Expand file tree
/
Copy pathBookDB.java
More file actions
315 lines (287 loc) · 9.04 KB
/
Copy pathBookDB.java
File metadata and controls
315 lines (287 loc) · 9.04 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package bookdb;
import java.util.ArrayList;
public class BookDB {
private ArrayList<Book> books;
public BookDB() {
//TODO
this.books = new ArrayList<>();
//Declare private attributes using constructors
}
//Initialize the ArrayList<Books>
public void add(Book b) {
// TODO
books.add(b);
if(books.size()>=2){
for(int i=0;i<books.size()-1;i++){
for(int j =0;j<books.size()-1-i;j++){
Book book1=books.get(j);
Book book2=books.get(j+1);
//The specific processing methods for ArrayList
//Extract and analyze the elements inside separately
if(book1.title.charAt(0)>book2.title.charAt(0)){
books.set(j,book2);
books.set(j+1,book1);
//Set a structure similar to bubble sort
}
}
}
}
}
//Using bubble sort to arrange titles of books
public void printBooks(ArrayList<Book> books) {
// TODO
for (Book book : books) {
System.out.println(book.getDetails());
if (book instanceof GraphicNovel gn) {
System.out.println("Colour palette: " +
(gn.getPalette()?"full colour" : "black and white") );
//Set the ternary operator to make the code more concise
//Determine the type to output the specific attributes
}
System.out.println("Type: "+book.getType()+"\n");
}
//We can judge the type of book to print different information by using polymorphism of java
}
public ArrayList<Book> getAllBooks() {
// TODO
return books;
}
public ArrayList<Book> getBooksByAuthor(String author) {
// TODO
ArrayList<Book> NewBooks1=new ArrayList<Book>();
for (Book book : books) {
if (author.equals(book.author)) {
NewBooks1.add(book);
}
}
return NewBooks1;
}
//Judge whether the name is equal to author's name
public ArrayList<Book> getBooksByGenre(String genre) {
// TODO
ArrayList<Book> NewBooks2=new ArrayList<Book>();
for (Book book : books) {
if (book.genres.contains(genre)) {
NewBooks2.add(book);
}
}
return NewBooks2;
}
//Judge whether the genre is contained by the genre of book
public ArrayList<Book> getBooksByTitle(String title) {
// TODO
ArrayList<Book> NewBooks3=new ArrayList<Book>();
for (Book book : books) {
if (book.title.contains(title)) {
NewBooks3.add(book);
}
}
return NewBooks3;
}
//Judge whether the title is equal to book's title
public ArrayList<String> getAllGenres() {
// TODO
ArrayList<String> NewGenres=new ArrayList<String>();
for (Book book : books) {
for(int i=0;i<book.genres.size();i++){
if(!(NewGenres.contains(book.genres.get(i)))){
NewGenres.add(book.genres.get(i));
}
}
}
return NewGenres;
}
//Return all the genres
public ArrayList<Book> getPhysicalBooks() {
// TODO
ArrayList<Book> NewBooks5=new ArrayList<Book>();
for (Book book : books) {
if (book.getClass()==PhysicalBook.class) {
NewBooks5.add(book);
}
}
return NewBooks5;
}
//Return all the physical book's names
public ArrayList<Book> getDigitalBooks() {
// TODO
ArrayList<Book> NewBooks6=new ArrayList<Book>();
for (Book book : books) {
if (book.getClass()==EBook.class||book.getClass()== AudioBook.class) {
NewBooks6.add(book);
//Strict requirements for type judgment
}
}
return NewBooks6;
}
//Return all the digital book's names
}
//Task 1
abstract class Book{
String title;
String author;
ArrayList<String> genres;
String type;
//These properties are common properties in these books in package 'oldboookdb'
public Book(String title,
String author,
ArrayList<String> genres){
this.title=title;
this.author=author;
this.genres=genres;
}
//This is a constructor,it can initialize properties of class Book
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public ArrayList<String> getGenres() {
return genres;
}
public String getType(){
return type;
}
public String getDetails(){
return "Title: " + title + "\nAuthor: " + author +
"\nGenres: " +String.join(" ",genres) ;
}
//These methods are common methods in these books in package 'oldboookdb'
}
class EBook extends Book {
String format;
//This is the particular property of EBook
public EBook(String title,
String author,
ArrayList<String> genres,
String format) {
super(title, author, genres);
this.format = format;
//This constructor include particular properties of EBook
}
public String getFormat() {
return format;
}
public String getType() {
return "e-book";
}
public String getDetails() {
return (super.getDetails() + "\nFormat: " + format);
}
//This is the particular method of EBook
}
class AudioBook extends Book{
String format;
int bitrate;
//This is the particular property of AudioBook
public AudioBook(String title,
String author,
ArrayList<String> genres,
String format,
int bitrate){
super(title,author,genres);
this.format=format;
this.bitrate=bitrate;
}
//This constructor include particular properties of AudioBook
public int getBitrate() {
return bitrate;
}
public String getType(){
return "audiobook";
}
public String getDetails(){
return ( super.getDetails() +
"\nFormat: " + format + "\nBitrate: " + bitrate +"kbps");
}
// This is the particular method of AudioBook
}
class PhysicalBook extends Book{
int pages;
//This is the particular property of PhysicalBook
public PhysicalBook(String title,
String author,
ArrayList<String> genres,
int pages){
super(title,author,genres);
if (pages < 0) throw new RuntimeException("Pages cannot be negative");
this.pages=pages;
}
//This constructor include particular properties of PhysicalBook
public int getPages() {
return pages;
}
public String getType(){
return "physical book";
}
@Override
public String getDetails(){
return (super.getDetails() + "\nPages: " + pages);
}
// This is the particular method of PhysicalBook
}
//Task 2
interface GraphicNovel{
public boolean getPalette();
public int getPages();
}
class PhysicalGraphicNovel extends PhysicalBook implements GraphicNovel {
boolean Palette;
//This is the particular property of PhysicalGraphicNovel
public PhysicalGraphicNovel(String title,
String author,
ArrayList<String> genres,
int pages,
boolean Palette){
super(title,author,genres,pages);
this.Palette=Palette;
}
//This constructor include particular properties of PhysicalGraphicNovel
public boolean getPalette(){
return Palette;
}
public int getPages(){
return pages;
}
//Override the original methods
public String getType(){
return "physical graphic novel";
}
public String getDetails(){
return ( super.getDetails() );
}
//These are special methods of physical graphic novel
}
class DigitalGraphicNovel extends EBook implements GraphicNovel{
boolean Palette;
int pages;
//This is the particular property of DigitalGraphicNovel
public DigitalGraphicNovel(String title,
String author,
ArrayList<String> genres,
int pages,
boolean Palette,
String format){
super(title,author,genres,format);
if (pages < 0) throw new RuntimeException("Pages cannot be negative");
//异常语法遗忘
this.pages = pages;
this.Palette=Palette;
}
//This constructor include particular properties of DigitalGraphicNovel
public boolean getPalette(){
return Palette;
}
public int getPages(){
return pages;
}
//Override the original methods
public String getType(){
return "digital graphic novel";
}
public String getDetails(){
return ( super.getDetails() +"\nPages: "+pages);
}
//These are special methods of digital graphic novel
}